UNPKG

flipchain

Version:

core chaining library, heavily based on [webpack-chain](https://github.com/mozilla-rpweb/webpack-chain)

118 lines (105 loc) 2.15 kB
const {inspectorGadget} = require('inspector-gadget') /** * @type {Chainable} * @property {Chainable | any} parent */ class Chainable { /** * @param {any} parent */ constructor(parent) { this.parent = parent this.inspect = inspectorGadget(this, ['parent', 'workflow']) if (this.onConstructor) this.onConstructor(parent) } /** * @see Chainable.parent * @return {Chainable | any} */ end() { return this.parent } /** * @param {any} key * @param {Function} [trueBrancher=Function.prototype] * @param {Function} [falseBrancher=Function.prototype] * @return {ChainedMap} */ whenHas( key, trueBrancher = Function.prototype, falseBrancher = Function.prototype ) { if (this.has(key) === true) { trueBrancher(this.get(key), this) } else { falseBrancher(false, this) } return this } /** * @description * when the condition is true, * trueBrancher is called, * else, falseBrancher is called * * @param {boolean} condition * @param {Function} [trueBrancher=Function.prototype] * @param {Function} [falseBrancher=Function.prototype] * @return {ChainedMap} */ when( condition, trueBrancher = Function.prototype, falseBrancher = Function.prototype ) { if (condition) { trueBrancher(this) } else { falseBrancher(this) } return this } /** * @type {generator} * https://github.com/sindresorhus/quick-lru/blob/master/index.js */ * [Symbol.iterator]() { for (const item of this.store) { yield item } } /** * @see ChainedMap.store * @return {number} */ get length() { return this.store.size } /** * @return {Chainable} */ clear() { this.store.clear() return this } /** * @description calls .delete on this.store.map * @param {string | any} key * @return {Chainable} */ delete(key) { this.store.delete(key) return this } /** * @param {any} value * @return {boolean} */ has(value) { return this.store.has(value) } } module.exports = Chainable