UNPKG

@maxio/tsed-configure

Version:

Npm TypeScript Package Boilerplate

152 lines 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Greeter = (name) => `Hello ${name}`; function getSegment(path) { const pathArray = path.split('.'); const parts = []; for (let i = 0; i < pathArray.length; i++) { let p = pathArray[i]; while (p[p.length - 1] === '\\' && pathArray[i + 1] !== undefined) { p = p.slice(0, -1) + '.'; p += pathArray[++i]; } parts.push(p); } return parts; } ; const hasOwn = Object.prototype.hasOwnProperty; function forIn(obj, fn, thisArg) { for (const key in obj) { if (fn.call(thisArg, obj[key], key, obj) === false) { break; } } } ; function forOwn(obj, fn, thisArg) { forIn(obj, (val, key) => { if (hasOwn.call(obj, key)) { return fn.call(thisArg, obj[key], key, obj); } }); } ; const Configure = { create: (options, config, extend = false) => { if (!Configure.isObject(config)) { return options; } forOwn(options, (value, key) => { if (key === '__proto__') { return; } const val = config[key]; // add the missing property, or allow a null property to be updated if (Configure.isObject(val) && Configure.isObject(value)) { Configure.create(val, value); } else if (val) { options[key] = val; } }); return options; }, cut: (key, obj, value) => { if (!Configure.isObject(obj) || typeof key !== 'string') { return value === undefined ? obj : value; } const pathArray = getSegment(key); let result = null; for (let i = 0; i < pathArray.length; i++) { const p = pathArray[i]; if (i === pathArray.length - 1) { result = obj[p]; delete obj[p]; return result; } obj = obj[p]; if (!Configure.isObject(obj)) { return result; } } }, delete: (key, obj) => { if (!Configure.isObject(obj) || typeof key !== 'string') { return; } const pathArray = getSegment(key); for (let i = 0; i < pathArray.length; i++) { const p = pathArray[i]; if (i === pathArray.length - 1) { delete obj[p]; return; } obj = obj[p]; if (!Configure.isObject(obj)) { return; } } }, get: (key, obj, value = null) => { if (!Configure.isObject(obj) || typeof key !== 'string') { return value === undefined ? obj : value; } const pathArray = getSegment(key); for (let i = 0; i < pathArray.length; i++) { if (!Object.prototype.propertyIsEnumerable.call(obj, pathArray[i])) { return value; } obj = obj[pathArray[i]]; if (obj === undefined || obj === null) { if (i !== pathArray.length - 1) { return value; } break; } } return obj; }, has: (key, obj) => { if (!Configure.isObject(obj) || typeof key !== 'string') { return false; } const pathArray = getSegment(key); for (let i = 0; i < pathArray.length; i++) { if (Configure.isObject(obj)) { if (!(pathArray[i] in obj)) { return false; } obj = obj[pathArray[i]]; } else { return false; } } return true; }, isObject: (obj) => { const type = typeof obj; return obj !== null && (type === 'object' || type === 'function'); }, set(key, obj, value) { if (!Configure.isObject(obj) || typeof key !== 'string') { return obj; } const root = obj; const pathArray = getSegment(key); for (let i = 0; i < pathArray.length; i++) { const p = pathArray[i]; if (!Configure.isObject(obj[p])) { obj[p] = {}; } if (i === pathArray.length - 1) { obj[p] = value; } obj = obj[p]; } return root; } }; exports.Configure = Configure; //# sourceMappingURL=index.js.map