@webkrafters/data-distillery
v0.1.1
Published
Data Distillery - Returns an object constructed using slices of an exisitng object.
Downloads
606
Maintainers
Readme
Data Distillery
Returns an object constructed using slices of an exisitng object.
Install:
npm i -S data-distillery
Alternate: npm i -S @webkrafters/data-distillery
Example
import distill from 'data-distillery';
const source = {
address: {
city: 'Test City',
state: 'My Province'
},
matrix: [
[ [ 0, 3, 1 ], [ 4, 0, 3 ] ],
[ [ 4, 1, 9 ], [ 7, 4, 9 ] ],
[ [ 8, 7, 3 ], [ 0, 3, 1 ] ]
],
registered: {
time: new Date(),
timezone: 'Eastern Time'
},
tags: [ 'test', 'foo', 'bar', 'baz', 'boo', 'tap', 'bak' ]
};
// DEFAULT USAGE
// -------------
distill( source, [
'matrix.1.1',
'matrix[2].0',
'address',
'registered.timezone',
'tags[4]',
'matrix[0][1]'
]);
// returns distilled object => {
// matrix: {
// 0: { 1: [ 4, 0, 3 ] },
// 1: { 1: [ 7, 4, 9 ] },
// 2: { 0: [ 8, 7, 3 ] }
// },
// address: {
// city: 'Test City',
// state: 'My Province'
// },
// registered: {
// timezone: 'Eastern Time'
// },
// tags: {
// 4: 'boo'
// }
// }
An Optional Parameter 3:
This function also accepts an optional third parameter which may either be
Example: Property Tranformation.
// USING SAME SOURCE OBJECT AS ABOVE USAGE
// ---------------------------------------
distill( source, [
'address',
'registered.timezone',
'tags[4]',
'matrix[0][1]'
], function( p ) {
if( typeof p.value === 'string' || p.value instanceof String ) {
return p.value.toUpperCase();
}
return p.value;
} );
// returns distilled object => {
// matrix: { 0: { 1: [ 4, 0, 3 ] } },
// address: {
// city: 'Test City',
// state: 'My Province'
// },
// registered: {
// timezone: 'EASTERN TIME'
// },
// tags: {
// 4: 'BOO'
// }
// }
Example: Array Preservation.
// USING SAME SOURCE OBJECT AS IN PREVIOUS EXAMPLE.
// ------------------------------------------------
distill( source, [
'matrix.1.1',
'matrix[2].0',
'address',
'registered.timezone',
'tags[4]',
'matrix[0][1]'
], {
arrays: {
preserve: true
}
} );
// returns distilled object => {
// matrix: [
// [ <empty>, [ 4, 0, 3 ] ],
// [ <empty>, [ 7, 4, 9 ] ],
// [ [ 8, 7, 3 ] ]
// ],
// address: {
// city: 'Test City',
// state: 'My Province'
// },
// registered: {
// timezone: 'Eastern Time'
// },
// tags: [ <empty>, <empty>, <empty>, <empty>, 'boo' ]
// }
Example: Array Compaction.
// USING SAME SOURCE OBJECT AS IN PREVIOUS EXAMPLE.
// ------------------------------------------------
distill( source, [
'matrix.1.1',
'matrix[2].0',
'address',
'registered.timezone',
'tags[4]',
'matrix[0][1]'
], {
arrays: {
preserve: true,
sparse: false
}
} );
// returns distilled object => {
// matrix: [
// [ [ 4, 0, 3 ] ],
// [ [ 7, 4, 9 ] ],
// [ [ 8, 7, 3 ] ]
// ],
// address: {
// city: 'Test City',
// state: 'My Province'
// },
// registered: {
// timezone: 'Eastern Time'
// },
// tags: [ 'boo' ]
// }
License
MIT