Comparing version 0.0.1 to 0.1.0
0.1.0 / 2013-12-28 | ||
================== | ||
* API BREAKING! `get` has been changed, see the README for migration path | ||
* Add `set` method | ||
* Start using simple-assert (closes #2) | ||
0.0.1 / 2013-11-24 | ||
@@ -3,0 +10,0 @@ ================== |
/** | ||
* ### .getPathValue(path, object) | ||
* ### .get(obj, path) | ||
* | ||
* This allows the retrieval of values in an | ||
* object given a string path. | ||
* Retrieve the value in an object given a string path. | ||
* | ||
* var obj = { | ||
* prop1: { | ||
* arr: ['a', 'b', 'c'] | ||
* , str: 'Hello' | ||
* } | ||
* , prop2: { | ||
* arr: [ { nested: 'Universe' } ] | ||
* , str: 'Hello again!' | ||
* } | ||
* ```js | ||
* var obj = { | ||
* prop1: { | ||
* arr: ['a', 'b', 'c'] | ||
* , str: 'Hello' | ||
* } | ||
* , prop2: { | ||
* arr: [ { nested: 'Universe' } ] | ||
* , str: 'Hello again!' | ||
* } | ||
* }; | ||
* ``` | ||
* | ||
* The following would be the results. | ||
* | ||
* getPathValue('prop1.str', obj); // Hello | ||
* getPathValue('prop1.att[2]', obj); // b | ||
* getPathValue('prop2.arr[0].nested', obj); // Universe | ||
* ```js | ||
* var properties = require('tea-properties'); | ||
* properties.get(obj, 'prop1.str'); // Hello | ||
* properties.get(obj, 'prop1.att[2]'); // b | ||
* properties.get(obj, 'prop2.arr[0].nested'); // Universe | ||
* ``` | ||
* | ||
* @param {Object} object | ||
* @param {String} path | ||
* @return {Object} value or `undefined` | ||
*/ | ||
exports.get = function(obj, path) { | ||
var parsed = parsePath(path); | ||
return getPathValue(parsed, obj); | ||
}; | ||
/** | ||
* ### .set(path, value, object) | ||
* | ||
* Define the value in an object at a given string path. | ||
* | ||
* ```js | ||
* var obj = { | ||
* prop1: { | ||
* arr: ['a', 'b', 'c'] | ||
* , str: 'Hello' | ||
* } | ||
* , prop2: { | ||
* arr: [ { nested: 'Universe' } ] | ||
* , str: 'Hello again!' | ||
* } | ||
* }; | ||
* ``` | ||
* | ||
* The following would be acceptable. | ||
* | ||
* ```js | ||
* var properties = require('tea-properties'); | ||
* properties.set(obj, 'prop1.str', 'Hello Universe!'); | ||
* properties.set(obj, 'prop1.arr[2]', 'B'); | ||
* properties.set(obj, 'prop2.arr[0].nested.value', { hello: 'universe' }); | ||
* ``` | ||
* | ||
* @param {Object} object | ||
* @returns {Object} value or `undefined` | ||
* @name getPathValue | ||
* @param {String} path | ||
* @param {Mixed} value | ||
* @api public | ||
*/ | ||
var getPathValue = module.exports = function(path, obj) { | ||
exports.set = function(obj, path, val) { | ||
var parsed = parsePath(path); | ||
return _getPathValue(parsed, obj); | ||
setPathValue(parsed, val, obj); | ||
}; | ||
/*! | ||
* ## parsePath(path) | ||
* | ||
* Helper function used to parse string object | ||
* paths. Use in conjunction with `_getPathValue`. | ||
* paths. Use in conjunction with `getPathValue`. | ||
* | ||
* var parsed = parsePath('myobject.property.subprop'); | ||
* var parsed = parsePath('myobject.property.subprop'); | ||
* | ||
@@ -51,10 +89,9 @@ * ### Paths: | ||
* @returns {Object} parsed | ||
* @api private | ||
*/ | ||
function parsePath(path) { | ||
var str = path.replace(/\[/g, '.['); | ||
var parts = str.match(/(\\\.|[^.]+?)+/g); | ||
var str = (path || '').replace(/\[/g, '.[') | ||
, parts = str.match(/(\\\.|[^.]+?)+/g); | ||
return parts.map(function (value) { | ||
return parts.map(function(value) { | ||
var re = /\[(\d+)\]$/ | ||
@@ -65,11 +102,9 @@ , mArr = re.exec(value) | ||
}); | ||
} | ||
}; | ||
/*! | ||
* ## _getPathValue(parsed, obj) | ||
* | ||
* Helper companion function for `.parsePath` that returns | ||
* Companion function for `parsePath` that returns | ||
* the value located at the parsed address. | ||
* | ||
* var value = getPathValue(parsed, obj); | ||
* var value = getPathValue(parsed, obj); | ||
* | ||
@@ -79,8 +114,7 @@ * @param {Object} parsed definition from `parsePath`. | ||
* @returns {Object|Undefined} value | ||
* @api private | ||
*/ | ||
function _getPathValue(parsed, obj) { | ||
function getPathValue(parsed, obj) { | ||
var tmp = obj | ||
var res; | ||
, res; | ||
@@ -90,6 +124,4 @@ for (var i = 0, l = parsed.length; i < l; i++) { | ||
if (tmp) { | ||
if ('undefined' !== typeof part.p) | ||
tmp = tmp[part.p]; | ||
else if ('undefined' !== typeof part.i) | ||
tmp = tmp[part.i]; | ||
if (defined(part.p)) tmp = tmp[part.p]; | ||
else if (defined(part.i)) tmp = tmp[part.i]; | ||
if (i == (l - 1)) res = tmp; | ||
@@ -100,3 +132,60 @@ } else { | ||
} | ||
return res; | ||
}; | ||
/*! | ||
* Companion function for `parsePath` that sets | ||
* the value located at a parsed address. | ||
* | ||
* setPathValue(parsed, 'value', obj); | ||
* | ||
* @param {Object} parsed definition from `parsePath` | ||
* @param {*} value to use upon set | ||
* @param {Object} object to search and define on | ||
* @api private | ||
*/ | ||
function setPathValue(parsed, val, obj) { | ||
var tmp = obj; | ||
var i = 0; | ||
var l = parsed.length; | ||
var part; | ||
for (; i < l; i++) { | ||
part = parsed[i]; | ||
if (defined(tmp) && i == (l - 1)) { | ||
var x = defined(part.p) ? part.p : part.i; | ||
tmp[x] = val; | ||
} else if (defined(tmp)) { | ||
if (defined(part.p) && tmp[part.p]) { | ||
tmp = tmp[part.p]; | ||
} else if (defined(part.i) && tmp[part.i]) { | ||
tmp = tmp[part.i]; | ||
} else { | ||
var next = parsed[i + 1]; | ||
var x = defined(part.p) ? part.p : part.i; | ||
var y = defined(next.p) ? {} : []; | ||
tmp[x] = y; | ||
tmp = tmp[x]; | ||
} | ||
} else { | ||
if (i == (l - 1)) tmp = val; | ||
else if (defined(part.p)) tmp = {}; | ||
else if (defined(part.i)) tmp = []; | ||
} | ||
} | ||
}; | ||
/*! | ||
* Check if `val` is defined. | ||
* | ||
* @param {Mixed} val | ||
* @returns {Boolean} `true` if defined | ||
* @api private | ||
*/ | ||
function defined(val) { | ||
return !(!val && 'undefined' === typeof val); | ||
} |
{ | ||
"name": "pathval", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Object value retrieval given a string path", | ||
@@ -8,5 +8,5 @@ "main": "./lib/pathval.js", | ||
"scripts": { | ||
"test": "mocha test/*.test.js", | ||
"coverage": "istanbul cover ./node_modules/.bin/_mocha test/*.test.js", | ||
"coveralls": "istanbul cover ./node_modules/.bin/_mocha test/*.test.js --report lcovonly && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"test": "hydro", | ||
"coverage": "istanbul cover _hydro", | ||
"coveralls": "istanbul cover _hydro --report lcovonly && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"test-readme": "jsmd README.md" | ||
@@ -19,7 +19,10 @@ }, | ||
"devDependencies": { | ||
"mocha": "~1.13.0", | ||
"chai": "~1.8.1", | ||
"coveralls": "~2.3.0", | ||
"istanbul": "~0.1.44", | ||
"jsmd": "~0.2.0" | ||
"jsmd": "~0.2.0", | ||
"simple-assert": "~1.0.0", | ||
"hydro": "~0.8.7", | ||
"hydro-file-suite": "0.0.1", | ||
"hydro-doc": "0.0.2", | ||
"hydro-bdd": "0.0.3" | ||
}, | ||
@@ -26,0 +29,0 @@ "author": "Veselin Todorov <hi@vesln.com>", |
@@ -10,2 +10,6 @@ [![NPM version](https://badge.fury.io/js/pathval.png)](http://badge.fury.io/js/pathval) | ||
``` | ||
var props = require('pathval'); | ||
``` | ||
Given: | ||
@@ -31,16 +35,28 @@ | ||
<!-- js | ||
var getPathValue = require('./'); | ||
var props = require('./'); | ||
--> | ||
#### get | ||
```js | ||
getPathValue('prop1.str', obj); // => "Hello" | ||
getPathValue('prop1.arr[2]', obj); // => "c" | ||
getPathValue('prop2.arr[0].nested', obj); // => "Universe" | ||
props.get(obj, 'prop1.str'); // => "Hello" | ||
props.get(obj, 'prop1.arr[2]'); // => "c" | ||
props.get(obj, 'prop2.arr[0].nested'); // => "Universe" | ||
getPathValue('[0].foo', arr); // => "bar" | ||
props.get(arr, '[0].foo'); // => "bar" | ||
getPathValue('doesnt.matter', undefined); // => undefined | ||
getPathValue('doesnt.exist', {}); // => undefined | ||
props.get(undefined, 'doesnt.matter'); // => undefined | ||
props.get({}, 'doesnt.exist'); // => undefined | ||
``` | ||
#### set | ||
```js | ||
props.set(obj, 'hello.universe', 'properties'); | ||
props.set(obj, 'hello.universe[1].properties', 'galaxy'); | ||
props.set(obj, 'hello', 'universe'); | ||
props.set(obj, 'hello[0]', 1); | ||
props.set(obj, 'hello[2]', 3); | ||
``` | ||
## Installation | ||
@@ -66,2 +82,8 @@ | ||
### Readme tests | ||
```bash | ||
$ npm run-script test-readme | ||
``` | ||
## License | ||
@@ -68,0 +90,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9610
8
200
104
8
1