ts-ioc-container
Version:
Typescript IoC container
49 lines (48 loc) • 1.71 kB
JavaScript
export const pipe = (...mappers) => (value) => mappers.reduce((acc, current) => current(acc), value);
export 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);
}
export const constant = (value) => () => value;
export function lazyProxy(resolveInstance) {
let instance;
return new Proxy({}, {
get: (_, prop) => {
instance = instance ?? resolveInstance();
// @ts-ignore
return instance[prop];
},
});
}
export function toLazyIf(resolveInstance, isLazy = false) {
if (isLazy) {
return lazyProxy(resolveInstance);
}
return resolveInstance();
}
export const promisify = (arg) => (arg instanceof Promise ? arg : Promise.resolve(arg));
export const List = {
lastOf: (arr) => arr[arr.length - 1],
};
export const Filter = {
exclude: (arr) => {
const excludeSet = arr instanceof Array ? new Set(arr) : arr;
return (v) => !excludeSet.has(v);
},
};
export const 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) => Is.object(target) && 'resolve' in target && typeof target['resolve'] === 'function',
DepKey: (key) => {
return typeof key === 'object' && key !== null && 'key' in key;
},
};