1.1.4 • Published 9 years ago

object-reader v1.1.4

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

object-reader

npm version NPM downloads Travis Status Appveyor Status Coverage Status Dependencies Dev-dependencies JavaScript Style Guide Codacy Badge

This module allows you to access deep properties and make operations on objects using a path.

What is a "path" ?

A path is a string which describes how an object will be read.

Install

Node.js

npm install object-reader --save

Usage

var obj = {
  a: {
    b: 0,
    c: [1, 2]
  },
  d: [
    {
      e: 0,
      f: 1,
      g: 2,
    },
    {
      e: 0,
      f: 2,
      g: 2,
      h: 3
    }
  ],
  g: [ [0, 1], [1, 2] ],
  "b.c": "escaping works !"
};

var objectReader = require("object-reader");

// get property
objectReader.read(obj, "a.b") // 0
objectReader.read(obj, "a.['b']") // 0
objectReader.read(obj, "a.c.[0]") // 1
objectReader.read(obj, "a.c.0") // 1

// get filtered array of objects
objectReader.read(obj, "d.(e='0', f='1')") // [ {e: 0, f: 1, g: 2 } ], get array of objects where e = 0 and f = 1
objectReader.read(obj, "d.(g='2')") // [ {e: 0, f: 1, g: 2 }, {e: 0, f: 2, g: 2, h: 3 } ], get array of objects where g = 2
objectReader.read(obj, "d.(e='1')") // [], return an empty array because there is no object having e = 1

// apply operation
objectReader.read(obj, "g.@concat") // [0, 1, 1, 2], concat array keeping duplicates
objectReader.read(obj, "d.@merge") // {e: [0, 0], f: [1, 2], g: [2, 2], h: [3] }, make array with object fields

// escaping dots
objectReader.read(obj, "a.b\\.c") // "escaping works !"

Immutability

Reading operations are guarantee without side effects.

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test

License

MIT

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago