README
Declarative style JSON objects converter
Features
- Self descriptive template language
- Simple syntax for flattening hierarchical objects
- Possibility to mix declarative and imperative styles
Install
npm install t-objects
Usage example
var T = require('t-objects');
var company = {
Departments: [
{
Name: 'Quality assurance',
Staff: [
{
FirstName: 'Panic',
LastName: 'Generator',
Position: 'QA-Engineer'
},
{
FirstName: 'Ivanov',
LastName: 'Ivan',
Position: 'QA-Engineer'
}
]
},
{
Name: 'Development',
Staff: [
{
FirstName: 'Bydlo',
LastName: 'Coder',
Position: 'Junior Developer'
},
{
FirstName: 'Nebydlo',
LastName: 'Coder',
Position: 'Senior Developer'
}
]
}
]
};
var t = T({
'->': true,
$company: ':external',
'{$dep}': [ '$company', function(c){ return c.Departments }],
'{$person}': [ '$dep', function(d){ return d.Staff }],
FirstName: [ '$person', function(p){ return p.FirstName }],
LastName: [ '$person', function(p){ return p.LastName }],
Position: [ '$person', function(p){ return p.Position }],
Department: [ '$dep', function(d){ return d.Name }]
});
console.log(t.build({ $company: company }));
Output
[
{
FirstName: 'Panic',
LastName: 'Generator',
Position: 'QA-Engineer',
Department: 'Quality assurance'
},
{
FirstName: 'Ivanov',
LastName: 'Ivan',
Position: 'QA-Engineer',
Department: 'Quality assurance'
},
{
FirstName: 'Bydlo',
LastName: 'Coder',
Position: 'Junior Developer',
Department: 'Development'
},
{
FirstName: 'Nebydlo',
LastName: 'Coder',
Position: 'Senior Developer',
Department: 'Development'
}
]
The same template in ECMA 6
template = T({
'->': true,
$company: ':external',
'{$dep}': [ '$company', c => c.Departments ],
'{$person}': [ '$dep', d => d.Staff ],
FirstName: [ '$person', p => p.FirstName ],
LastName: [ '$person', p => p.LastName ],
Position: [ '$person', p => p.Position ],
Department: [ '$dep', d => d.Name ]
});
API reference
T Class
Constructor
Params
- template
Object
Template object
Returns T
Class instance
build
Params
- data
Object
Input data object
Returns Object
Output data object
Language reference
Special keys
->
Template marker or initial queue. Any object in template that has this marker will be processed bybuild
method. Marker may be set inboolean
true
value or predefined keys queue. Predefined keys queue will be corrected or appended by properties dependencies.