Repository URL to install this package:
|
Version:
1.1.16 ▾
|
import { camelCase } from 'chain'
import { fromMapToObj } from 'exotic'
class Snippet {
// @LINT @TODO @JAMES @FIXME - disabled only to use in POC - will replace with full chain instead
// eslint-disable-next-line
constructor(parent) {
// @NOTE declared here ONLY to write offline to assign methods that would be bound (which require babel)
// eslint-disable-next-line
let store, get, set, wrap, addMethod, extendGetSet, extend, entries, end
store = new Map()
get = key => this.store.get(key)
set = (key, val) => {
this.store.set(key, val)
return this
}
entries = () => fromMapToObj(this.store)
wrap = fn => methods => {
fn.apply(this, [methods])
return this
}
// curry, support obj again, copy simple into snippet until it uses full chain eh
addMethod = (name, fn) => {
if (typeof name === 'string') {
this[name] = fn
} else {
// object
Object.keys(name).forEach(method => {
this[method] = function() {
const returned = name[method].apply(this, arguments)
// default to this
if (returned === undefined) {
return this
} else {
return returned
}
}
})
}
return this
}
// @NOTE arrow functions have no arguments object
let self = this
extend = function(methodsAsArray) {
// default to an array as single argument
let methods = methodsAsArray
// then not using array
if (arguments.length > 1) {
methods = Array.from(arguments)
}
methods.forEach(method => {
self[method] = val => self.set(method, val)
})
}
end = () => parent
extendGetSet = methods =>
methods.forEach(method => {
this[camelCase('get-' + method)] = () => this.get(method)
this[camelCase('set-' + method)] = val => this.set(method, val)
this.extend([method])
})
// add methods
Object.assign(this, {
store,
get,
set,
addMethod,
extendGetSet,
extend,
entries,
end,
})
}
setDebug(shouldDebug = true) {
return this.set('debug', shouldDebug)
}
getDebug() {
return this.get('debug')
}
}
// module.exports = Snippet
// module.exports.Snippet = Snippet
// module.exports.default = Snippet
export { Snippet }
export default Snippet