Skip to content

Instantly share code, notes, and snippets.

@mlowen
Created May 13, 2022 23:04
Show Gist options
  • Save mlowen/d166770ec96815949aa24aaa1f71f9c2 to your computer and use it in GitHub Desktop.
Save mlowen/d166770ec96815949aa24aaa1f71f9c2 to your computer and use it in GitHub Desktop.
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