const Packhouse = require('packhouse')
const packhouse = new Packhouse()
packhosue.utils === Packhouse.utils // true
const tool = {
myTool: {
install({ store, utils }) {
store.id = utils.generateId()
store.uuid = utils.generateId
},
handler(self) {
console.log(self.store.id)
console.log(self.store.uuid())
}
}
}
const molds = {
uuid(value, { utils }) {
return utils.generateId()
}
}
packhouse.utils.getType([]) // array
packhouse.utils.getType(NaN) // NaN
packhouse.utils.getType(null) // empty
packhouse.utils.getType(undefined) // empty
packhouse.utils.getType(/test/) // regexp
packhouse.utils.getType(new Promise(() => {})) // promise
packhouse.utils.getType(Buffer.from('123')) // buffer
packhouse.utils.getType(new Error()) // error
verify
驗證的type
對象是多個型態,而支援的類型請參閱getType。
let options = {
a: 5,
b: []
}
let data = packhouse.utils.verify(options, {
// [required, allow types, default value]
a: [true, ['number']],
b: [true, ['array']],
c: [false, ['number'], 0]
})
console.log(data.a) // 5
console.log(data.c) // 0
console.log(typeof packhouse.utils.generateId()) // string
let uuid = require('uuid/v4')
let packhouse = require('packhouse')
packhouse.utils.generateId = uuid
let a = {
b: {
c: {
d: 5
}
}
}
console.log(packhouse.utils.peel(a, 'b.c.d')) // 5
console.log(packhouse.utils.peel(a, 'b.c.e')) // undefined
let loader = packhouse.utils.loader((resolve, reject) => {
setTimeout(() => {
resolve('hello')
}, 1000)
})
loader().then(result => {
// 等待一秒才執行
console.log(result) // hello
loader().then(result => {
// 不需等待
console.log(result) // hello
})
})