UNPKG

@ibnlanre/builder

Version:

Creates a builder object for defining keys and values.

57 lines (53 loc) 1.48 kB
// src/utilities/is-dictionary/index.ts function isDictionary(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } // src/utilities/is-function/index.ts function isFunction(value) { return typeof value === "function"; } // src/core/create-branches/index.ts function createBranches(register, prefix = []) { const entries = Object.entries(register); const branches = entries.reduce( (acc, [key, value]) => { const newPath = prefix.concat([key]); if (isFunction(value)) { return { ...acc, [key]: { $get: (...args) => [...newPath, ...args], $use: (...args) => [...newPath, ...args] } }; } const root = { $get: (...args) => [...newPath, ...args], $use: () => newPath }; return { ...acc, [key]: isDictionary(value) ? Object.assign(root, createBranches(value, newPath)) : root }; }, {} ); return branches; } // src/core/create-builder/index.ts function createBuilder(register, options) { const { prefix = [], separator = "." } = { ...options }; const branches = createBranches(register, prefix); return Object.assign(branches, { $get(...path) { if (path.length) return path.join(separator); return prefix; }, get $use() { return register; } }); } export { createBuilder }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map