object-reader

1.1.4 • Public • Published

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

Package Sidebar

Install

npm i object-reader

Weekly Downloads

2

Version

1.1.4

License

MIT

Last publish

Collaborators

  • leelow