0.1.0 • Published 9 years ago

deser v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

deser.js

Data (de)serialization

Example

var mapper = deser({
  fields: {
    // key - prop name after deserialization,
    // value - prop name after serialization
    eventId: 'id_event',
    fooBar: 'foo_bar',
  },
  deserialize: function(doc) {
    return {
      items: doc.things.map(function(thing) {
        return {val: thing};
      }),
    };
  },
  serialize: function(doc) {
    return {
      things: doc.items.map(function(item) {
        return item.val;
      }),
    };
  };
});

mapper.deserialize({
  id_event: 123,
  foo_bar: 'baz',
  things: ['a', 'b', 'c'],
});
// => {
//   eventId: 123,
//   fooBar: 'baz',
//   items: [
//     {val: 'a'},
//     {val: 'b'},
//     {val: 'c'},
//   ],
// }

mapper.serialize({
  eventId: 55,
  fooBar: 'baz',
  items: [
    {val: 1},
    {val: 2},
  ],
});
// => {
//   id_event: 55,
//   foo_bar: 'baz'
//   things: [1, 2],
// }