obj2arr

1.0.5 • Public • Published

obj2arr

convert object to array

Installation

npm install obj2arr

Usage

setup

var obj2arr = require('obj2arr');
 
var defs = {
  case1: {
    type: 'array',
    items: [
      {
        id: 'key',
        type: 'string'
      },
      {
        id: 'num',
        type: 'number'
      }
    ]
  }
};
obj2arr.define(defs);

encode

var data = {
  key: 'test1',
  num: 10
};
var encoded = obj2arr.encode('case1', data);
console.log(encoded);
// -- output --
// [ 'test1', 10 ]

decode

var decoded = obj2arr.decode('case1', encoded);
console.log(decoded);
// -- output --
// { key: 'test1', num: 10 }

Tools

toArrSchema

var objSchema = {
  type: 'object',
  properties: {
    key: {
      id: 'key',
      type: 'string'
    },
    num: {
      id: 'num',
      type: 'number'
    }
  }
};
var arrSchema = obj2arr.tools.toArrSchema(objSchema);
console.log(arrSchema);
// -- output --
// { type: 'array',
//   items: [ { id: 'key', type: 'string' }, { id: 'num', type: 'number' } ] }

toObjSchema

var arrSchema = {
  type: 'array',
  items: [
    {
      id: 'key',
      type: 'string'
    },
    {
      id: 'num',
      type: 'number'
    }
  ]
};
var objSchema = obj2arr.tools.toObjSchema(arrSchema);
console.log(objSchema);
// -- output --
// { type: 'object',
//  properties:
//   { key: { id: 'key', type: 'string' },
//     num: { id: 'num', type: 'number' } } }

Contribution

  1. Fork it ( https://github.com/iyu/obj2arr/fork )
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the npm test command and confirm that it passes
  6. Create new Pull Request

Readme

Keywords

Package Sidebar

Install

npm i obj2arr

Weekly Downloads

4

Version

1.0.5

License

MIT

Last publish

Collaborators

  • iyu