ts-ioc-container
Version:
Typescript IoC container
37 lines (36 loc) • 1.18 kB
JavaScript
export const isConstructor = (T) => typeof T === 'function' && !!T.prototype;
export function isInstance(target) {
return Object.prototype.hasOwnProperty.call(target, 'constructor');
}
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 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);
},
};