Created
May 13, 2022 23:04
-
-
Save mlowen/d166770ec96815949aa24aaa1f71f9c2 to your computer and use it in GitHub Desktop.
Original Factory Builder Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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