2.0.1 • Published 7 years ago

object-binder v2.0.1

Weekly downloads
26
License
-
Repository
github
Last release
7 years ago

Object Binder

Bind a preset list of allowed props from one object to another

This works as an extends function, but allows to preset what properties will be replaced and, when dealing with nested object, do not replace the whole object, just the selected properties. It also can map some property to be bound to another path.

I created this mainly to bind req.body in to models on POST/PUT requests.

1. Install & configure

$ npm install --save object_binder

2. Create a Binder instance

const BinderFactory = require( 'object_binder' );
const binder = BinderFactory.build( 'someprop', 'some.deep.property', { 'some.prop': 'to.place.on.another.prop' } );

3. Now can extends one object in another

const binder = BinderFactory.build( 'engine.output.torque', 'engine.output.power', 'engine.displacement', { 'engine.output.power': 'engine.bhp' } );
const model = { model: 'BMW M3', year: 1988, engine: { cylinders: 4 } };
const params = { engine: { output: { power: 197, torque: 177 }, displacement: 2303 } };

binder.bind(model, params);

// model will be:
// {
//   model: 'BMW M3',
//   year: 1988,
//   engine: {
//     bhp : 197,
//     cylinders: 4,
//     displacement: 2303,
//     output: {
//       power: 197,
//       torque: 177
//     }
//   }
// }

The is also a wilcard, meaning all props hould be bind.

const binder = BinderFactory.build( '*' );
const model = { model: 'BMW M3', year: 1988, engine: { cylinders: 4 } };
const params = { model: 'BMW M5', year: 1989, engine: { cylinders: 6, output: { power: 315, torque: 360 }, displacement: 3535 } };

binder.bind( model, params );

// model will be:
// {
//   model: 'BMW M5',
//   year: 1989,
//   engine: {
//     cylinders: 6
//   }
// }
2.0.1

7 years ago

2.0.0

7 years ago

1.0.3

7 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago