set-value
Advanced tools
Comparing version 0.2.0 to 0.3.0
71
index.js
@@ -10,26 +10,33 @@ /*! | ||
var isObject = require('isobject'); | ||
var nc = require('noncharacters'); | ||
var utils = require('./utils'); | ||
module.exports = function setValue(obj, path, val) { | ||
if (path == null) { | ||
return obj; | ||
module.exports = function (obj, path, val) { | ||
if (typeof obj !== 'object') return obj; | ||
if (Array.isArray(path)) { | ||
path = utils.toPath(path); | ||
} | ||
path = escape(path); | ||
var seg = (/^(.+)\.(.+)$/).exec(path); | ||
if (seg !== null) { | ||
create(obj, seg[1], seg[2], val); | ||
if (typeof path !== 'string') { | ||
return obj; | ||
} | ||
obj[unescape(path)] = val; | ||
return obj; | ||
}; | ||
function create(obj, path, child, val) { | ||
if (!path) return obj; | ||
var arr = path.split('.'); | ||
var len = arr.length, i = 0; | ||
while (len--) { | ||
var key = unescape(arr[i++]); | ||
if (!obj[key] || !isObject(obj[key])) { | ||
var segs = path.split('.'); | ||
var len = segs.length, i = -1; | ||
var res = obj; | ||
var last; | ||
while (++i < len) { | ||
var key = segs[i]; | ||
while (key[key.length - 1] === '\\') { | ||
key = key.slice(0, -1) + '.' + segs[++i]; | ||
} | ||
if (i === len - 1) { | ||
last = key; | ||
break; | ||
} | ||
if (typeof obj[key] !== 'object') { | ||
obj[key] = {}; | ||
@@ -39,21 +46,9 @@ } | ||
} | ||
if (typeof child === 'string') { | ||
child = unescape(child); | ||
if (obj.hasOwnProperty(last) && typeof obj[last] === 'object') { | ||
utils.extend(obj[last], val); | ||
} else { | ||
obj[last] = val; | ||
} | ||
return (obj[child] = val); | ||
} | ||
/** | ||
* Escape => `\\.` | ||
*/ | ||
function escape(str) { | ||
return str.split('\\.').join(nc[1]); | ||
} | ||
/** | ||
* Unescaped dots | ||
*/ | ||
function unescape(str) { | ||
return str.split(nc[1]).join('.'); | ||
} | ||
return res; | ||
}; |
{ | ||
"name": "set-value", | ||
"description": "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/jonschlinkert/set-value", | ||
"author": { | ||
"name": "Jon Schlinkert", | ||
"url": "https://github.com/jonschlinkert" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/jonschlinkert/set-value.git" | ||
}, | ||
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"repository": "jonschlinkert/set-value", | ||
"bugs": { | ||
"url": "https://github.com/jonschlinkert/set-value/issues" | ||
}, | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/set-value/blob/master/LICENSE" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"utils.js" | ||
], | ||
@@ -32,7 +24,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"extend-shallow": "^2.0.1", | ||
"isobject": "^1.0.0", | ||
"noncharacters": "^1.1.0" | ||
"lazy-cache": "^0.2.4", | ||
"to-object-path": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"mocha": "^2.3.3", | ||
"should": "^5.2.0" | ||
@@ -56,3 +50,18 @@ }, | ||
"values" | ||
] | ||
], | ||
"verb": { | ||
"related": { | ||
"description": "", | ||
"list": [ | ||
"assign-value", | ||
"get-value", | ||
"set-value", | ||
"has-value", | ||
"merge-value", | ||
"omit-value", | ||
"unset-value", | ||
"union-value" | ||
] | ||
} | ||
} | ||
} |
@@ -5,6 +5,8 @@ # set-value [![NPM version](https://badge.fury.io/js/set-value.svg)](http://badge.fury.io/js/set-value) [![Build Status](https://travis-ci.org/jonschlinkert/set-value.svg)](https://travis-ci.org/jonschlinkert/set-value) | ||
## Install | ||
Install with [npm](https://www.npmjs.com/) | ||
```bash | ||
npm i set-value --save | ||
```sh | ||
$ npm i set-value --save | ||
``` | ||
@@ -25,8 +27,10 @@ | ||
* [any](https://github.com/jonschlinkert/any): Returns `true` if a value exists in the given string, array or object. | ||
* [get-value](https://github.com/jonschlinkert/get-value): Use property paths (` a.b.c`) get a nested value from an object. | ||
* [has-own-deep](https://github.com/jonschlinkert/has-own-deep): Returns true if an object has an own, nested property using dot notation paths ('a.b.c'). | ||
* [has-value](https://github.com/jonschlinkert/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | ||
* [has-any](https://github.com/jonschlinkert/has-any): Returns true if an object has any of the specified keys. | ||
* [has-any-deep](https://github.com/jonschlinkert/has-any-deep): Return true if `key` exists deeply on the given object. | ||
* [assign-value](https://www.npmjs.com/package/assign-value): Assign a value or extend a deeply nested property of an object using object path… [more](https://www.npmjs.com/package/assign-value) | [homepage](https://github.com/jonschlinkert/assign-value) | ||
* [get-value](https://www.npmjs.com/package/get-value): Use property paths (` a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value) | ||
* [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://www.npmjs.com/package/has-value) | [homepage](https://github.com/jonschlinkert/has-value) | ||
* [merge-value](https://www.npmjs.com/package/merge-value): Similar to assign-value but deeply merges object values or nested values using object path/dot notation. | [homepage](https://github.com/jonschlinkert/merge-value) | ||
* [omit-value](https://www.npmjs.com/package/omit-value): Omit properties from an object or deeply nested property of an object using object path… [more](https://www.npmjs.com/package/omit-value) | [homepage](https://github.com/jonschlinkert/omit-value) | ||
* [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value) | ||
* [union-value](https://www.npmjs.com/package/union-value): Set an array of unique values as the property of an object. Supports setting deeply… [more](https://www.npmjs.com/package/union-value) | [homepage](https://github.com/jonschlinkert/union-value) | ||
* [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value) | ||
@@ -37,4 +41,4 @@ ## Running tests | ||
```bash | ||
npm i -d && npm test | ||
```sh | ||
$ npm i -d && npm test | ||
``` | ||
@@ -44,3 +48,3 @@ | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/set-value/issues/new) | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/set-value/issues/new). | ||
@@ -56,3 +60,3 @@ ## Author | ||
Copyright (c) 2015 Jon Schlinkert | ||
Copyright © 2015 Jon Schlinkert | ||
Released under the MIT license. | ||
@@ -62,4 +66,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 12, 2015._ | ||
<!-- deps: mocha should --> | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 30, 2015._ |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7033
5
72
61
4
1
+ Addedextend-shallow@^2.0.1
+ Addedlazy-cache@^0.2.4
+ Addedto-object-path@^0.2.0
+ Addedarr-flatten@1.1.0(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedextend-shallow@2.0.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedlazy-cache@0.2.7(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedto-object-path@0.2.0(transitive)
- Removednoncharacters@^1.1.0
- Removednoncharacters@1.1.0(transitive)