Takes a list of folders and an array of records and turns it into a tree
npm install @unction/treeify![Tests][BADGE_TRAVIS]
![Stability][BADGE_STABILITY]
![Dependencies][BADGE_DEPENDENCY]
> Array
This takes a list of functions (the folders) and an array of objects or an
object of objects (the collection) to create a tree. Each function in
the list of folders will in some way return a new object. All of the objects
produced are then turned into a final tree.
`` javascript``
const collection = [
{
id: "a1",
type: "resources",
attributes: {
version: "v1",
namespace: "accounts",
}
},
{
id: "a2",
type: "resources",
attributes: {
version: "v1",
namespace: "accounts",
}
},
{
id: "b1",
type: "resources",
attributes: {
version: "v1",
namespace: "profiles",
}
},
{
id: "b1",
type: "resources",
attributes: {
version: "v2",
namespace: "profiles",
}
}
]
The order goes from outer layer to deeper layer, so in this case the outer
level properties will be based on key("type"), and the deepest layerkey("id")
properties will be based on .
` javascript
const functions =
treeify(
[
groupBy(key("type")),
groupBy(keyChain(["attributes", "namespace"])),
groupBy(keyChain(["attributes", "version"])),
indexBy(key("id")),
]
)(
collection
)
`
The resulting object looks like this:
` javascript``
{
resources: {
accounts: {
v1: {
a1: {
id: "a1",
type: "resources",
attributes: {
version: "v1",
namespace: "accounts",
}
},
a2: {
id: "a2",
type: "resources",
attributes: {
version: "v1",
namespace: "accounts",
}
}
}
},
profiles: {
v1: {
b1: {
id: "b1",
type: "resources",
attributes: {
version: "v1",
namespace: "profiles",
}
}
},
v2: {
b1: {
id: "b1",
type: "resources",
attributes: {
version: "v2",
namespace: "profiles",
}
}
}
}
}
}
[BADGE_TRAVIS]: https://img.shields.io/travis/unctionjs/treeify.svg?maxAge=2592000&style=flat-square
[BADGE_STABILITY]: https://img.shields.io/badge/stability-strong-green.svg?maxAge=2592000&style=flat-square
[BADGE_DEPENDENCY]: https://img.shields.io/david/unctionjs/treeify.svg?maxAge=2592000&style=flat-square