Skip to content

Set and get values on objects via dot-notation strings

Notifications You must be signed in to change notification settings

0xvoidmain/deep-obj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DeepObject

Set and get values on objects via dot-notation strings.

Example

var DeepObject = require('deep-obj');
var get = DeepObject.get;
var set = DeepObject.set;

var testObject = {
	a: {
		b: {
			c: [{
				ae: 1
			}, {
				ae2: ['demoo']
			}]
		},
		d: [1,2,3]
	},
	'test-abc': {
		a: {
			b: [[1, 2, 3], [4, 5, 6]]
		}
	},
	'abc"de"': 1,
	'[abc"de"]': 3
}

console.log(get(testObject, ''), testObject);

console.log(get(testObject), testObject);

console.log(get(testObject, 'a.b.c'), testObject.a.b.c);

console.log(get(testObject, '["a.b"]'), testObject['a.b']);

console.log(get(testObject, '[\'abc"de"\']'), testObject['abc"de"']);

console.log(get(testObject, '[\'[abc"de"]\']'), testObject['[abc"de"]']);

console.log(get(testObject, 'a.d[0]'), testObject.a.d[0]);

console.log(get(testObject, 'a.b.c[0]'), testObject.a.b.c[0]);

console.log(get(testObject, ['a', 'd', 0]), testObject.a.d[0]);

console.log(get(testObject, 'a.b.c[0]["ae"]'), testObject.a.b.c[0]["ae"]);

console.log(get(testObject, 'a.b.c[0].ae'), testObject.a.b.c[0].ae);

console.log(get(testObject, 'a.b.c[1].ae2[0]'), testObject.a.b.c[1].ae2[0]);

console.log(get(testObject, '["test-abc"].a.b[0][2]'), testObject["test-abc"].a.b[0][2]);

console.log(get(testObject.arr, '[0].a.b'), testObject.arr[0].a.b));

console.log(get(testObject.arr, '[0].a.b.d.e'), undefined));

set(testObject, '["test-abc"].a.b[0][2]', 5);
console.log(get(testObject, '["test-abc"].a.b[0][2]'), 5);

set(testObject, 'a.b.c[0].ae', 10);
console.log(get(testObject, 'a.b.c[0].ae'), 10);

set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);
console.log(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);

console.log('Test performance');
console.time();
for (var i = 0; i < 1000; i++) {
	get(testObject, 'a.b.c');
	get(testObject, '[\'abc"de"\']');
	get(testObject, '[\'[abc"de"]\']');
	get(testObject, 'a.d[0]');
	get(testObject, 'a.b.c[0]');
	get(testObject, 'a.b.c[0]["ae"]');
	get(testObject, 'a.b.c[0].ae');
	get(testObject, 'a.b.c[1].ae2[0]');
	get(testObject, '["test-abc"].a.b[0][2]');
	get(testObject.arr, '[0].a.b');
	get(testObject.arr, '[0].a.b.d.e');
}
console.timeEnd();

API

DeepObject.get(object, path)

Where path is a string like foo.bar or foo.bar[0][1] or [0][1].foo['bar'].

The path also can be an array: ['foo', 'bar', 0]

The function will return an VALUE or undefined

DeepObject.set(object, path, value)

Where path is a string like foo.bar or foo.bar[0][1] or [0][1].foo['bar'].

The path also can be an array: ['foo', 'bar', 0]

The function return TRUE or FALSE. Return TRUE if it can set value with the path

DeepObject.parse(path)

Where path is a string like foo.bar or foo.bar[0][1] or [0][1].foo['bar'].

The function will return an array of path;

Ex:

foo.bar => ['foo', 'bar']

foo.bar[0][1] => ['foo', 'bar', 0, 1]

[0][1].foo['bar'] => [0, 1, 'foo', 'bar']

Installation

With npm do:

npm install deep-obj

License

MIT