This project is part of the @thi.ng/umbrella monorepo.
DAG resolution of vanilla objects & arrays with internally linked values.
It's common practice to use nested JS objects for configuration purposes. Frequently some values in the object are copies or derivatives of other values, which can lead to mistakes during refactoring and/or duplication of effort.
To avoid these issues, this library provides the ability to define single sources of truth, create references (links) to these values and a provide a resolution mechanism to recursively expand their real values and/or compute derived values. Both absolute & relative references are supported.
See source & tests for further details.
yarn add @thi.ng/resolve-map
import { resolveMap } from "@thi.ng/resolve-map";
resolveMap({
colors: {
bg: "white",
text: "black",
selected: "red",
},
main: {
fontsizes: [12, 16, 20]
},
button: {
bg: "->/colors.text",
label: "->/colors.bg",
fontsize: (resolve) => `${resolve("main.fontsizes.0")}px`,
},
buttonPrimary: {
bg: "->/colors.selected",
label: "->/button.label",
fontsize: (resolve) => `${resolve("main.fontsizes.2")}px`,
}
})
// {
// colors: {
// bg: "white",
// text: "black",
// selected: "red"
// },
// main: {
// fontsizes: [ 12, 16, 20 ]
// },
// button: {
// "bg": "black",
// "label": "white",
// "fontsize": "12px"
// },
// buttonPrimary: {
// bg: "red",
// label: "black",
// fontsize: "20px"
// }
// }
- Karsten Schmidt
© 2018 Karsten Schmidt // Apache Software License 2.0