obj-chain-core
Version:
fluent chaining for obj with dot-prop access
38 lines (35 loc) • 1.02 kB
JavaScript
const dotProp = require('dot-prop')
const DotPropPlugin = {
middleware: {
set(key, val) {
// this.middleware.val.forEach()
if (key.includes('.')) dotProp.set(this.data, key, val)
else this.data[key] = val
this.current = val
return this
},
get(key = null) {
if (key === null) return this.current
if (key.includes('.')) return dotProp.get(this.data, key)
return this.data[key]
},
// setIfNotEmpty(key, val) {
// // this.middleware.val.forEach()
// if (this.has(key)) return this
// this.set(key, val)
// return this
// },
has(key) {
// this.middleware.has.forEach()
if (key.includes('.')) return dotProp.has(this.data, key)
return Object.prototype.hasOwnProperty.call(this.data, key)
},
del(key) {
// this.middleware.del.forEach()
if (key.includes('.')) dotProp.delete(this.data, key)
else delete this.data[key]
return this
},
},
}
module.exports = DotPropPlugin