Skip to content

Instantly share code, notes, and snippets.

@mlowen
Created May 13, 2022
Embed
What would you like to do?
Original Factory Builder Implementation
function dependantProperty(func, deps) {
func.dependencies = deps;
return func;
}
function build(factory, overrides = {}) {
let keys = Object.keys(factory);
let result = {};
do {
const nextKeys = [];
for (let i in keys) {
const key = keys[i];
if (key in overrides) {
result[key] = overrides[key];
} else if (factory[key] instanceof Function) {
if (!factory[key].dependencies || factory[key].dependencies.every((k) => k in result)) {
result[key] = factory[key](result);
} else {
nextKeys.push(key);
}
} else {
result[key] = factory[key];
}
}
keys = nextKeys;
} while(keys.length > 0);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment