smart-factory
Version:
functional programming-ready dependancy injector/ioc container library
59 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const errors_1 = require("./errors");
exports.checkCyclicReference = (arr) => {
const map = new Map();
const visit = new Map();
arr.map((c) => map.set(c.key, c));
arr.map((c) => visit.set(c.key, false));
arr.map((c) => {
exports.checkNodeCycle(map, visit, c.key);
});
};
exports.checkNodeCycle = (map, visit, start) => {
Array.from(visit.keys()).map((k) => visit.set(k, false));
const stack = [start];
while (stack.length > 0) {
const node = map.get(stack.pop());
if (visit.get(node.key) === false) {
visit.set(node.key, true);
node.deps.map((d) => {
if (d === start)
throw new errors_1.CyclicReferenceError(`cyclic reference found: ${node.key} <-> ${d}`);
stack.push(d);
});
}
}
};
exports.checkDepNotfound = (arr) => {
const keys = arr.map((c) => c.key);
arr.map((c) => c.deps.map((d) => {
if (lodash_1.includes(keys, d) === false)
throw new errors_1.DependancyNotfoundError(`dependency not found: ${d}`);
}));
};
exports.checkSelfReference = (arr) => {
let moduleName = null;
const selfs = arr.map((elem) => {
const selfRef = exports.selfReference(elem);
if (selfRef === true)
moduleName = elem.key;
return selfRef;
});
if (lodash_1.some(selfs))
throw new errors_1.SelfReferenceError(`self reference found: ${moduleName}`);
};
exports.checkKeyDuplicates = (arr) => {
const duplciates = exports.duplicateKeys(arr);
if (duplciates.length > 0) {
throw new errors_1.DuplicateModuleKeyError(`duplicated key found: ${duplciates[0]}`);
}
};
exports.duplicateKeys = (arr) => lodash_1.uniq(arr.map((elem) => elem.key)).map((key) => lodash_1.filter(arr, (elem) => elem.key === key).length === 1 ? null : key)
.filter((elem) => (elem));
exports.selfReference = (a) => lodash_1.includes(a.deps, a.key);
exports.crossReference = (a, b) => lodash_1.includes(a.deps, b.key) && lodash_1.includes(b.deps, a.key);
exports.subsets = (arr, n) => arr
.reduce((subsets, value) => subsets.concat(subsets.map((set) => [value, ...set])), [[]]).filter((subset) => subset.length === n);
//# sourceMappingURL=container-helpers.js.map