Skip to content

Utility to manage an object property using simple 'a.b.c' paths.

License

Notifications You must be signed in to change notification settings

deltavi/easy-object-selector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

easy-object-selector Easy Object Selector Build Status easy-object-selector

Utility to manage an object property using simple 'a.b.c' paths (with dot–notation).

Install

npm install easy-object-selector

Paths

Path Description
a.b.c The value of 'c'
["a", "b", "c"] The value of 'c'
a.d.1.e The second value of 'e'
a.d.*.e All values of 'e'
a.d.first().e The first value of 'e'
a.d.last().e The last value of 'e'

Examples

const selector = require("easy-object-selector");
const obj = {
     a: {
         b: {
             c: "val1"
         },
         d: [
                 {
                     e: "val2"
                 },
                 {
                     e: "val3"
                 }
         ]
     }
};
const select = selector.select;
select(obj, "a.b.c"); // => "val1"
select(obj, ["a", "b", "c"]); // => "val1"
select(obj, "a.b.x", "defValue"); // => "defValue"
select(obj, "a.d.0.e"); // => "val2"
select(obj, "a.d.*.e"); // => ["val2", "val3"]
select(obj, "a.d.first().e"); // => "val2"
select(obj, "a.d.last().e"); // => "val3"

const has = selector.has;
has(obj, "a.b.c"); // => true

const keys = selector.keys;
keys(obj, "a"); // => ["b", "d"]

const put = selector.put;
const o = {};
put(o, "a.b.c", "val1") // => {a:{b:{c:"val1"}}}

const wrapper = selector.wrap(obj);
wrapper.get("a.b.c"); // => "val1"
wrapper.has("a.b.c"); // => true
wrapper.keys("a"); // => ["b", "d"]

const o2 = {};
const wrapper2 = selector.wrap(o2);
wrapper2.put("a.b.c", "val1"); // => {a:{b:{c:"val1"}}}

API

Table of Contents

select

Get the value of the selected property if it exists.

Parameters

Examples

const selector = require("easy-object-selector");
const select = selector.select;
const obj = {
     a: {
         b: {
             c: "val1"
         },
         d: [
                 {
                     e: "val2"
                 },
                 {
                     e: "val3"
                 }
         ]
     }
};
select(obj, "a.b.c"); // => "val1"
select(obj, ["a", "b", "c"]); // => "val1"
select(obj, "a.b.x"); // => undefined
select(obj, "a.b.x", "defValue"); // => "defValue"
select(obj, "a.d.0.e"); // => "val2"
select(obj, "a.d.1.e"); // => "val3"
select(obj, "a.d.*.e"); // => ["val2", "val3"]
select(obj, "a.d.first().e"); // => "val2"
select(obj, "a.d.last().e"); // => "val3"

Returns (Object | Array | String | Number | Boolean | undefined)

has

Check if the selected property exists.

Parameters

Examples

const selector = require("easy-object-selector");
const has = selector.has;
const obj = {
     a : {
         b : {
             c: "val1"
         }
     }
};
has(obj, "a.b.c"); // => true
has(obj, ["a", "b", "c"]); // => true
has(obj, "a.b.x"); // => false

Returns Boolean

keys

Returns an array of keys.

Parameters

Examples

const selector = require("easy-object-selector");
const keys = selector.keys;
const obj = {
     a : {
         b : {
             c: "val1",
             d: "val2",
             e: "val3",
         }
     }
};
keys(obj, "a.b"); // => ["c", "d", "e"]
keys(obj, ["a", "b"]); // => ["c", "d", "e"]
keys(obj, "a.b.x"); // => []
keys(obj, "a.b.x", ["a", "b"]); // => ["a", "b"]

Returns Array

put

Put the value in the object property.

Parameters

Examples

const selector = require("easy-object-selector");
const put = selector.put;
const o = {};
put(o, "a.b.c", "val1") // => {a:{b:{c:"val1"}}}
const o2 = {};
put(o2, ["a", "b", "c"], "val1") // => {a:{b:{c:"val1"}}}

Returns Object

wrap

Creates a new ObjectWrapper

Parameters

Examples

const selector = require("easy-object-selector");
const obj = {
     a: {
         b: {
             c: "val1"
         }
     }
};
const wrapper = selector.wrap(obj);
wrapper.get("a.b.c"); // => "val1"
wrapper.get(["a", "b", "c"]); // => "val1"

Returns ObjectWrapper wrapper

ObjectWrapper

Object wrapper

Parameters

get

Get the value of the selected property if it exists.

Parameters

Examples

const selector = require("easy-object-selector");
const obj = {
     a: {
         b: {
             c: "val1"
         },
         d: [
                 {
                     e: "val2"
                 },
                 {
                     e: "val3"
                 }
         ]
     }
};
const wrapper = selector.wrap(obj);
wrapper.get("a.b.c"); // => "val1"
wrapper.get(["a", "b", "c"]); // => "val1"
wrapper.get("a.b.x"); // => undefined
wrapper.get("a.b.x", "defValue"); // => "defValue"
wrapper.get("a.d.0.e"); // => "val2"
wrapper.get("a.d.1.e"); // => "val3"
wrapper.get("a.d.*.e"); // => ["val2", "val3"]
wrapper.get("a.d.first().e"); // => "val2"
wrapper.get("a.d.last().e"); // => "val3"

Returns (Object | Array | String | Number | Boolean | undefined)

has

Check if the selected property exists.

Parameters

Examples

const selector = require("easy-object-selector");
const obj = {
     a : {
         b : {
             c: "val1"
         }
     }
};
const wrapper = selector.wrap(obj);
wrapper.has("a.b.c"); // => true
wrapper.has(["a", "b", "c"]); // => true
wrapper.has("a.b.x"); // => false

Returns Boolean

keys

Returns an array of keys.

Parameters

Examples

const selector = require("easy-object-selector");
const obj = {
     a : {
         b : {
             c: "val1",
             d: "val2",
             e: "val3",
         }
     }
};
const wrapper = selector.wrap(obj);
wrapper.keys("a.b"); // => ["c", "d", "e"]
wrapper.keys(["a", "b"]); // => ["c", "d", "e"]
wrapper.keys("a.b.x"); // => []
wrapper.keys("a.b.x", ["a", "b"]); // => ["a", "b"]

Returns Array

put

Put the value in the object property.

Parameters

Examples

const selector = require("easy-object-selector");
const o = {};
const wrapper = selector.wrap(o);
wrapper.put("a.b.c", "val1") // => {a:{b:{c:"val1"}}}

Returns Object

About

Utility to manage an object property using simple 'a.b.c' paths.

Resources

License

Stars

Watchers

Forks

Packages

No packages published