0.1.1 • Published 7 years ago

joc v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

npm version Build Status

Object schema converter for JavaScript objects.

Joc

Installation

npm install joc -g

Example

var template = {
  name: [
    '$.firstname',
    '$.lastname',
    function(firstname, lastname) {
      return firstname + ' ' + lastname;
    },
  ],
  email: ['$.email', Joc.func.callObjectFunc(String, 'trim')],
  phone: ['$.phone', Joc.func.toString('+86%s')],
  friends: '$.friends',
  other: {
    age: ['$.age', Joc.func.toNumber()],
    height: ['$.height', Joc.func.toNumber('float')],
    weight: ['$.weight', Joc.func.toNumber('float')]
  }
};

var json = {
  firstname: 'Sheep',
  lastname: 'X',
  email: '\t  xsheep@x.com  \t',
  phone: '1234567890',
  age: '18',
  height: '180',
  weight: '70',
  introduction: 'Good boy',
  friends: [
    'Dog A',
    'Cat B'
  ]
};

var conv_json = Joc.convert({
  template: template,
  json: json
});

console.log(conv_json);
/*
{
  name: 'Sheep X',
  email: 'xsheep@x.com',
  phone: '+861234567890',
  friends: [
    'Dog A',
    'Cat B'
  ],
  other: {
    age: 18,
    height: 180.0,
    weight: 70.0
  }
};
*/

License

MIT