UNPKG

ts-ioc-container

Version:
58 lines (57 loc) 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Is = exports.Filter = exports.List = exports.promisify = exports.constant = exports.pipe = void 0; exports.fillEmptyIndexes = fillEmptyIndexes; exports.lazyProxy = lazyProxy; exports.toLazyIf = toLazyIf; const pipe = (...mappers) => (value) => mappers.reduce((acc, current) => current(acc), value); exports.pipe = pipe; function fillEmptyIndexes(baseArr, insertArr) { const a = [...baseArr]; const b = [...insertArr]; for (let i = 0; i < a.length; i++) { if (a[i] === undefined) { a[i] = b.shift(); } } return a.concat(b); } const constant = (value) => () => value; exports.constant = constant; function lazyProxy(resolveInstance) { let instance; return new Proxy({}, { get: (_, prop) => { instance = instance ?? resolveInstance(); // @ts-ignore return instance[prop]; }, }); } function toLazyIf(resolveInstance, isLazy = false) { if (isLazy) { return lazyProxy(resolveInstance); } return resolveInstance(); } const promisify = (arg) => (arg instanceof Promise ? arg : Promise.resolve(arg)); exports.promisify = promisify; exports.List = { lastOf: (arr) => arr[arr.length - 1], }; exports.Filter = { exclude: (arr) => { const excludeSet = arr instanceof Array ? new Set(arr) : arr; return (v) => !excludeSet.has(v); }, }; exports.Is = { object: (target) => target !== null && typeof target === 'object', instance: (target) => Object.prototype.hasOwnProperty.call(target, 'constructor'), constructor: (target) => typeof target === 'function' && !!target.prototype, dependencyKey: (target) => ['string', 'symbol'].includes(typeof target), injectBuilder: (target) => exports.Is.object(target) && 'resolve' in target && typeof target['resolve'] === 'function', DepKey: (key) => { return typeof key === 'object' && key !== null && 'key' in key; }, };