nested-props
Advanced tools
Comparing version 1.0.0 to 1.0.1
var nested = require('./index') | ||
var obj = {} | ||
nested.set(obj, 'user.name', 'kamiky') | ||
var obj = { | ||
coordinates: [{ | ||
value: [11, 22] | ||
}, { | ||
value: [33, 44, 55] | ||
}] | ||
} | ||
nested.set(obj, 'user.name', 'username') | ||
nested.set(obj, 'user.id', '666') | ||
@@ -9,4 +15,5 @@ nested.set(obj, 'user.info.city', 'paris') | ||
/* | ||
{ user: | ||
{ name: 'kamiky', | ||
{ coordinates: [ { value: [Object] }, { value: [Object] } ], | ||
user: | ||
{ name: 'username', | ||
id: '666', | ||
@@ -18,4 +25,5 @@ info: { city: 'paris', country: 'france' } } } | ||
/* | ||
{ user: | ||
{ name: 'kamiky', | ||
{ coordinates: [ { value: [Object] }, { value: [Object] } ], | ||
user: | ||
{ name: 'username', | ||
id: { value: '666' }, | ||
@@ -27,4 +35,5 @@ info: { city: 'paris', country: 'france' } } } | ||
/* | ||
{ user: | ||
{ name: 'kamiky', | ||
{ coordinates: [ { value: [Object] }, { value: [Object] } ], | ||
user: | ||
{ name: 'username', | ||
id: { value: '777' }, | ||
@@ -34,3 +43,3 @@ info: { city: 'paris', country: 'france' } } } | ||
var country = nested.get(obj, 'user.test.test') | ||
var country = nested.get(obj, 'user.info.country') | ||
/* | ||
@@ -40,4 +49,3 @@ france | ||
/* use this method to prevent | ||
TypeError: Cannot read property 'test' of undefined */ | ||
/* use this method to prevent : TypeError: Cannot read property 'test' of undefined */ | ||
var undefinedTest = nested.get(obj, 'user.test.test') | ||
@@ -48,2 +56,13 @@ /* | ||
console.log(obj) | ||
var coordinate1 = nested.get(obj, 'coordinates[0].value') | ||
/* [11, 22] */ | ||
var x2 = nested.get(obj, 'coordinates[1].value[0]') | ||
/* 33 */ | ||
var y2 = nested.get(obj, 'coordinates[1].value[1]') | ||
/* 44 */ | ||
var x3 = nested.get(obj, 'coordinates[2].value[0]') | ||
/* null */ | ||
32
index.js
function setNestedProperty (obj, name, value) { | ||
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) { | ||
throw new Error('nestedProps first argument should be a javascript object') | ||
if (!(typeof obj === 'object' || !obj || Array.isArray(obj))) { | ||
throw new Error('nestedProps first argument type is not supported') | ||
} | ||
@@ -21,12 +21,28 @@ var props = name.split('.') | ||
function getNestedProperty (obj, property) { | ||
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) { | ||
throw new Error('nestedProps first argument should be a javscript object') | ||
if (!(typeof obj === 'object' || !obj || Array.isArray(obj))) { | ||
throw new Error('nestedProps first argument type is not supported') | ||
} | ||
if (!obj) return null | ||
var props = property.split('.') | ||
var regex = new RegExp('\\[(.*?)\\]', 'gi') | ||
var result, prop, objectProperty, index | ||
props.forEach(function (prop, index) { | ||
if (obj) { | ||
obj = obj[prop] | ||
for (var i = 0; i < props.length; ++i) { | ||
prop = props[i] | ||
objectProperty = prop.substr(0, prop.indexOf('[')) | ||
if (prop.indexOf('[') === -1 && !objectProperty) { | ||
objectProperty = prop | ||
} | ||
}) | ||
if (obj && objectProperty && objectProperty.length > 0) { | ||
obj = obj[objectProperty] | ||
} | ||
while ((result = regex.exec(prop))) { | ||
index = result[1] | ||
if (obj && Array.isArray(obj) && obj.length > index && index >= 0) { | ||
obj = obj[index] | ||
} else { | ||
return null | ||
} | ||
} | ||
} | ||
return obj | ||
@@ -33,0 +49,0 @@ } |
{ | ||
"name": "nested-props", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Define nested properties value", | ||
@@ -21,3 +21,7 @@ "main": "index.js", | ||
"author": "kamiky", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
@@ -13,4 +13,10 @@ # nested-props | ||
var obj = {} | ||
nested.set(obj, 'user.name', 'kamiky') | ||
var obj = { | ||
coordinates: [{ | ||
value: [11, 22] | ||
}, { | ||
value: [33, 44, 55] | ||
}] | ||
} | ||
nested.set(obj, 'user.name', 'username') | ||
nested.set(obj, 'user.id', '666') | ||
@@ -20,18 +26,23 @@ nested.set(obj, 'user.info.city', 'paris') | ||
/* | ||
{ user: | ||
{ name: 'kamiky', | ||
{ coordinates: [ { value: [Object] }, { value: [Object] } ], | ||
user: | ||
{ name: 'username', | ||
id: '666', | ||
info: { city: 'paris', country: 'france' } } } | ||
*/ | ||
nested.set(obj, 'user.id.value', '666') | ||
/* | ||
{ user: | ||
{ name: 'kamiky', | ||
{ coordinates: [ { value: [Object] }, { value: [Object] } ], | ||
user: | ||
{ name: 'username', | ||
id: { value: '666' }, | ||
info: { city: 'paris', country: 'france' } } } | ||
*/ | ||
nested.set(obj, 'user.id.value', '777') | ||
/* | ||
{ user: | ||
{ name: 'kamiky', | ||
{ coordinates: [ { value: [Object] }, { value: [Object] } ], | ||
user: | ||
{ name: 'username', | ||
id: { value: '777' }, | ||
@@ -41,3 +52,3 @@ info: { city: 'paris', country: 'france' } } } | ||
var country = nested.get(obj, 'user.test.test') | ||
var country = nested.get(obj, 'user.info.country') | ||
/* | ||
@@ -47,4 +58,3 @@ france | ||
/* use this method to prevent | ||
TypeError: Cannot read property 'test' of undefined */ | ||
/* use this method to prevent : TypeError: Cannot read property 'test' of undefined */ | ||
var undefinedTest = nested.get(obj, 'user.test.test') | ||
@@ -55,4 +65,14 @@ /* | ||
console.log(obj) | ||
var coordinate1 = nested.get(obj, 'coordinates[0].value') | ||
/* [11, 22] */ | ||
var x2 = nested.get(obj, 'coordinates[1].value[0]') | ||
/* 33 */ | ||
var y2 = nested.get(obj, 'coordinates[1].value[1]') | ||
/* 44 */ | ||
var x3 = nested.get(obj, 'coordinates[2].value[0]') | ||
/* null */ | ||
``` |
7679
5
198
74
2