Weekly downloads
Readme
treem converts flat data (like SQL rows) into an object tree. The tree structure is determined by the columns' names.
Flat data :
const movies = [{
name: 'Rogue one',
'+actors.firstName': 'Felicity',
'+actors.lastName': 'Jones'
}, {
name: 'Rogue one',
'+actors.firstName': 'Diego',
'+actors.lastName': 'Luna'
}, {
name: 'Star Wars: The Force Awakens',
'+actors.firstName': 'Harrison',
'+actors.lastName': 'Ford'
}, {
name: 'Star Wars: The Force Awakens',
'+actors.firstName': 'Daisy',
'+actors.lastName': 'Ridley'
}];
Result :
[ { name: 'Rogue one',
actors:
[ { firstName: 'Felicity', lastName: 'Jones' },
{ firstName: 'Diego', lastName: 'Luna' } ] },
{ name: 'Star Wars: The Force Awakens',
actors:
[ { firstName: 'Harrison', lastName: 'Ford' },
{ firstName: 'Daisy', lastName: 'Ridley' } ] } ]
npm install treem
import Treem from 'treem';
// ES5: var Treem = require('treem').default;
const rows = [{
name: 'Rogue one',
'+actors.firstName': 'Felicity'
...
}];
const treem = new Treem();
treem.fill(rows);
treem.data === [{
name: 'Rogue one',
actors: [{
firstName: 'Felicity'
...
]];
treem takes flat data to produce an object tree. Flat data can be :
Orthogonally, flat data can be :
The structure of the tree is determined by a convention on the columns' naming. The convention defines, by default, 3 symbols :
.
: defines childrenbook.author.name
+
: defines an array instead of a single object.+movies.+actors
#
: defines keys of the object. If no key is provided, all properties of the object are considered as a key. Defining a key improves performances and allows object reuse (see the reuseObject option).actor.#id
The result produced by treem can be :
There are 2 functions to provide data to a treem instance :
const treem = new Treem({
symbols: {
key: '#',
collection: '+',
separator: '.'
},
root: 'collection',
prune: true,
reuseObject : true,
overwriteObject : false,
headers: {
id: 'id',
name: 'name',
petName: 'pet.name'
},
wrap: {
single: node => new Model(),
collection: node => new ModelCollection()
},
detect: {
key: node => { return {
check: node === 'id',
name : 'id'
}},
collection: node => { return {
check: node.startsWith('*'),
name : node.substring(1)
}},
}
});
symbols
accepted values: object with collection, key, and separator properties
Redefines the symbols used by treem to detect collection, key, and children. Example:
new Treem({
symbols: {
key: '#',
collection: '+',
separator: '.'
}
});
prune
accepted values: true (default), false or a callback
Removes null or undefined elements. prune can also accept a callback. Example :
new Treem({
prune: element => element === undefined || element === null
});
root
accepted values: 'collection' (default) or 'single'
Defines treem.data as either an array or an object.
reuseObject
accepted values: 'key' (default), true or false
Reuses an object if multiple instances of it are detected. Example :
const movies = [{
name: 'Rogue one',
'+actors.#id': 1,
'+actors.firstName': 'Felicity',
'+actors.lastName': 'Jones'
}, {
name: 'Rogue one',
'+actors.#id': 2,
'+actors.firstName': 'Anthony',
'+actors.lastName': 'Daniels'
}, {
name: 'Star Wars: The Force Awakens',
'+actors.#id': 3,
'+actors.firstName': 'Harrison',
'+actors.lastName': 'Ford'
}, {
name: 'Star Wars: The Force Awakens',
'+actors.#id': 2,
'+actors.firstName': 'Anthony',
'+actors.lastName': 'Daniels'
}];
Here the actor 'Anthony Daniels' appear in both movies. treem will use the same object instead of duplicate it.
ReuseObject can have 3 values :
overwriteObject
accepted values: false (default) or true
Overwrites single objects if values are different on subsequent rows.
headers
accepted values: object
Defines the columns headers and aliases. Useful if you can't change the columns' names. If this option is not defined, headers are taken from the first row of the flat data. Example:
const movies = [{
name: 'Rogue one',
id: 1,
firstName: 'Felicity',
lastName: 'Jones'
...
}];
new Treem({ headers: { name: 'name', id: '+actors.#id', firstName: '+actors.firstName', lastName: '+actors.lastName', } });
wrap
accepted values: {single: function, collection: function}
Wraps custom classes about single elements and collection elements. The function to be provided takes the current node as a parameter and returns an instance of the custom class. The custom class for single elements shall let its properties be set and gotten. The custom class for collection elements shall have a push function.
See the wiki for an example.
detect
accepted values: {key: function, collection: function}
Defines a custom way to detect keys and collections by providing a callback. The callback takes the current node as a parameter and returns an object with 2 properties :
See the wiki for an example with pluralization.
FAQs
High performance conversion of flat data (like SQL result rows) into nested objects.
The npm package treem receives a total of 385 weekly downloads. As such, treem popularity was classified as not popular.
We found that treem demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.