0.1.1 • Published 6 months ago

@webkrafters/data-distillery v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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

0.1.0-rc.5

6 months ago

0.1.0-rc.6

6 months ago

0.1.0

6 months ago

0.1.0-rc.3

6 months ago

0.1.0-rc.4

6 months ago

0.1.1

6 months ago

0.1.0-rc.2

6 months ago

0.1.0-rc.0

6 months ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago