Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

set-value

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

set-value - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

56

index.js

@@ -10,3 +10,3 @@ /*!

var toPath = require('to-object-path');
var split = require('split-string');
var extend = require('extend-shallow');

@@ -16,3 +16,3 @@ var isPlainObject = require('is-plain-object');

module.exports = function(obj, path, val) {
module.exports = function(obj, prop, val) {
if (!isObject(obj)) {

@@ -22,45 +22,33 @@ return obj;

if (Array.isArray(path)) {
path = toPath(path);
if (Array.isArray(prop)) {
prop = [].concat.apply([], prop).join('.');
}
if (typeof path !== 'string') {
if (typeof prop !== 'string') {
return obj;
}
var segs = path.split('.');
var len = segs.length, i = -1;
var res = obj;
var last;
var keys = split(prop, {sep: '.', brackets: true});
var len = keys.length;
var idx = -1;
var current = obj;
while (++i < len) {
var key = segs[i];
while (key[key.length - 1] === '\\') {
key = key.slice(0, -1) + '.' + segs[++i];
while (++idx < len) {
var key = keys[idx];
if (idx !== len - 1) {
if (!isObject(current[key])) {
current[key] = {};
}
current = current[key];
continue;
}
if (i === len - 1) {
last = key;
break;
if (isPlainObject(current[key]) && isPlainObject(val)) {
current[key] = extend({}, current[key], val);
} else {
current[key] = val;
}
if (!isObject(obj[key])) {
obj[key] = {};
}
obj = obj[key];
}
if (obj.hasOwnProperty(last) && isObject(obj[last])) {
if (isPlainObject(val)) {
extend(obj[last], val);
} else {
obj[last] = val;
}
} else {
obj[last] = val;
}
return res;
return obj;
};
{
"name": "set-value",
"description": "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.",
"version": "1.0.0",
"version": "2.0.0",
"homepage": "https://github.com/jonschlinkert/set-value",

@@ -30,8 +30,8 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"is-extendable": "^0.1.1",
"is-plain-object": "^2.0.1",
"to-object-path": "^0.3.0"
"is-plain-object": "^2.0.3",
"split-string": "^3.0.1"
},
"devDependencies": {
"gulp-format-md": "^0.1.12",
"mocha": "^3.4.1"
"mocha": "^3.4.2"
},

@@ -38,0 +38,0 @@ "keywords": [

@@ -17,9 +17,78 @@ # set-value [![NPM version](https://img.shields.io/npm/v/set-value.svg?style=flat)](https://www.npmjs.com/package/set-value) [![NPM monthly downloads](https://img.shields.io/npm/dm/set-value.svg?style=flat)](https://npmjs.org/package/set-value) [![NPM total downloads](https://img.shields.io/npm/dt/set-value.svg?style=flat)](https://npmjs.org/package/set-value) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/set-value.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/set-value)

var set = require('set-value');
set(object, prop, value);
```
### Params
* `object` **{object}**: The object to set `value` on
* `prop` **{string}**: The property to set. Dot-notation may be used.
* `value` **{any}**: The value to set on `object[prop]`
## Examples
Updates and returns the given object:
```js
var obj = {};
set(obj, 'a.b.c', 'd');
console.log(obj);
//=> {a: {b: c: 'd'}}
//=> { a: { b: { c: 'd' } } }
```
### Escaping
**Escaping with backslashes**
Prevent set-value from splitting on a dot by prefixing it with backslashes:
```js
console.log(set({}, 'a\\.b.c', 'd'));
//=> { 'a.b': { c: 'd' } }
console.log(set({}, 'a\\.b\\.c', 'd'));
//=> { 'a.b.c': 'd' }
```
**Escaping with double-quotes or single-quotes**
Wrap double or single quotes around the string, or part of the string, that should not be split by set-value:
```js
console.log(set({}, '"a.b".c', 'd'));
//=> { 'a.b': { c: 'd' } }
console.log(set({}, "'a.b'.c", "d"));
//=> { 'a.b': { c: 'd' } }
console.log(set({}, '"this/is/a/.file.path"', 'd'));
//=> { 'this/is/a/file.path': 'd' }
```
### Bracket support
set-value does not split inside brackets or braces:
```js
console.log(set({}, '[a.b].c', 'd'));
//=> { '[a.b]': { c: 'd' } }
console.log(set({}, "(a.b).c", "d"));
//=> { '(a.b)': { c: 'd' } }
console.log(set({}, "<a.b>.c", "d"));
//=> { '<a.b>': { c: 'd' } }
console.log(set({}, "{a..b}.c", "d"));
//=> { '{a..b}': { c: 'd' } }
```
## History
### v2.0.0
* Adds support for escaping with double or single quotes. See [escaping](#escaping) for examples.
* Will no longer split inside brackets or braces. See [bracket support](#bracket-support) for examples.
If there are any regressions please create a [bug report](../../issues/new). Thanks!
## About

@@ -46,3 +115,3 @@

| --- | --- |
| 56 | [jonschlinkert](https://github.com/jonschlinkert) |
| 59 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [vadimdemedes](https://github.com/vadimdemedes) |

@@ -83,2 +152,2 @@ | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 19, 2017._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 21, 2017._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc