tree-to-flat-map
Version:
Convert a tree to a flat map with dot-separated keys
29 lines (21 loc) • 1.01 kB
JavaScript
var _treeToFlatMap = function _treeToFlatMap(tree, separator, path, result) {
if (typeof tree !== "object") {
var _Object$assign;
if (path in result) {
throw new Error("Tree has multiple leaves for path \"" + path + "\": " + JSON.stringify(result[path]) + " and " + JSON.stringify(tree) + ".");
}
return Object.assign((_Object$assign = {}, _Object$assign[path] = tree, _Object$assign), result);
}
return Object.keys(tree).reduce(function (accumulator, key) {
return _treeToFlatMap(tree[key], separator, path ? "" + path + separator + key : key, accumulator);
}, result);
};
/** Converts `tree` to a flat map with dot-separated keys. */
var treeToFlatMap = function treeToFlatMap(tree, _temp) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$separator = _ref.separator,
separator = _ref$separator === void 0 ? "." : _ref$separator;
return _treeToFlatMap(tree, separator, "", {});
};
export { treeToFlatMap };
//# sourceMappingURL=tree-to-flat-map.esm.js.map