1 | # 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)
|
---|
2 |
|
---|
3 | > Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.
|
---|
4 |
|
---|
5 | ## Install
|
---|
6 |
|
---|
7 | Install with [npm](https://www.npmjs.com/):
|
---|
8 |
|
---|
9 | ```sh
|
---|
10 | $ npm install --save set-value
|
---|
11 | ```
|
---|
12 |
|
---|
13 | ## Usage
|
---|
14 |
|
---|
15 | ```js
|
---|
16 | var set = require('set-value');
|
---|
17 | set(object, prop, value);
|
---|
18 | ```
|
---|
19 |
|
---|
20 | ### Params
|
---|
21 |
|
---|
22 | * `object` **{object}**: The object to set `value` on
|
---|
23 | * `prop` **{string}**: The property to set. Dot-notation may be used.
|
---|
24 | * `value` **{any}**: The value to set on `object[prop]`
|
---|
25 |
|
---|
26 | ## Examples
|
---|
27 |
|
---|
28 | Updates and returns the given object:
|
---|
29 |
|
---|
30 | ```js
|
---|
31 | var obj = {};
|
---|
32 | set(obj, 'a.b.c', 'd');
|
---|
33 | console.log(obj);
|
---|
34 | //=> { a: { b: { c: 'd' } } }
|
---|
35 | ```
|
---|
36 |
|
---|
37 | ### Escaping
|
---|
38 |
|
---|
39 | **Escaping with backslashes**
|
---|
40 |
|
---|
41 | Prevent set-value from splitting on a dot by prefixing it with backslashes:
|
---|
42 |
|
---|
43 | ```js
|
---|
44 | console.log(set({}, 'a\\.b.c', 'd'));
|
---|
45 | //=> { 'a.b': { c: 'd' } }
|
---|
46 |
|
---|
47 | console.log(set({}, 'a\\.b\\.c', 'd'));
|
---|
48 | //=> { 'a.b.c': 'd' }
|
---|
49 | ```
|
---|
50 |
|
---|
51 | **Escaping with double-quotes or single-quotes**
|
---|
52 |
|
---|
53 | Wrap double or single quotes around the string, or part of the string, that should not be split by set-value:
|
---|
54 |
|
---|
55 | ```js
|
---|
56 | console.log(set({}, '"a.b".c', 'd'));
|
---|
57 | //=> { 'a.b': { c: 'd' } }
|
---|
58 |
|
---|
59 | console.log(set({}, "'a.b'.c", "d"));
|
---|
60 | //=> { 'a.b': { c: 'd' } }
|
---|
61 |
|
---|
62 | console.log(set({}, '"this/is/a/.file.path"', 'd'));
|
---|
63 | //=> { 'this/is/a/file.path': 'd' }
|
---|
64 | ```
|
---|
65 |
|
---|
66 | ### Bracket support
|
---|
67 |
|
---|
68 | set-value does not split inside brackets or braces:
|
---|
69 |
|
---|
70 | ```js
|
---|
71 | console.log(set({}, '[a.b].c', 'd'));
|
---|
72 | //=> { '[a.b]': { c: 'd' } }
|
---|
73 |
|
---|
74 | console.log(set({}, "(a.b).c", "d"));
|
---|
75 | //=> { '(a.b)': { c: 'd' } }
|
---|
76 |
|
---|
77 | console.log(set({}, "<a.b>.c", "d"));
|
---|
78 | //=> { '<a.b>': { c: 'd' } }
|
---|
79 |
|
---|
80 | console.log(set({}, "{a..b}.c", "d"));
|
---|
81 | //=> { '{a..b}': { c: 'd' } }
|
---|
82 | ```
|
---|
83 |
|
---|
84 | ## History
|
---|
85 |
|
---|
86 | ### v2.0.0
|
---|
87 |
|
---|
88 | * Adds support for escaping with double or single quotes. See [escaping](#escaping) for examples.
|
---|
89 | * Will no longer split inside brackets or braces. See [bracket support](#bracket-support) for examples.
|
---|
90 |
|
---|
91 | If there are any regressions please create a [bug report](../../issues/new). Thanks!
|
---|
92 |
|
---|
93 | ## About
|
---|
94 |
|
---|
95 | ### Related projects
|
---|
96 |
|
---|
97 | * [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://github.com/jonschlinkert/assign-value) | [homepage](https://github.com/jonschlinkert/assign-value "Assign a value or extend a deeply nested property of an object using object path notation.")
|
---|
98 | * [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 "Use property paths (`a.b.c`) to get a nested value from an object.")
|
---|
99 | * [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://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.")
|
---|
100 | * [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 "Similar to assign-value but deeply merges object values or nested values using object path/dot notation.")
|
---|
101 | * [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://github.com/jonschlinkert/omit-value) | [homepage](https://github.com/jonschlinkert/omit-value "Omit properties from an object or deeply nested property of an object using object path notation.")
|
---|
102 | * [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 "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
|
---|
103 | * [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://github.com/jonschlinkert/union-value) | [homepage](https://github.com/jonschlinkert/union-value "Set an array of unique values as the property of an object. Supports setting deeply nested properties using using object-paths/dot notation.")
|
---|
104 | * [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 "Delete nested properties from an object using dot notation.")
|
---|
105 |
|
---|
106 | ### Contributing
|
---|
107 |
|
---|
108 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
---|
109 |
|
---|
110 | ### Contributors
|
---|
111 |
|
---|
112 | | **Commits** | **Contributor** |
|
---|
113 | | --- | --- |
|
---|
114 | | 59 | [jonschlinkert](https://github.com/jonschlinkert) |
|
---|
115 | | 1 | [vadimdemedes](https://github.com/vadimdemedes) |
|
---|
116 | | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
|
---|
117 |
|
---|
118 | ### Building docs
|
---|
119 |
|
---|
120 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
|
---|
121 |
|
---|
122 | To generate the readme, run the following command:
|
---|
123 |
|
---|
124 | ```sh
|
---|
125 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
---|
126 | ```
|
---|
127 |
|
---|
128 | ### Running tests
|
---|
129 |
|
---|
130 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
|
---|
131 |
|
---|
132 | ```sh
|
---|
133 | $ npm install && npm test
|
---|
134 | ```
|
---|
135 |
|
---|
136 | ### Author
|
---|
137 |
|
---|
138 | **Jon Schlinkert**
|
---|
139 |
|
---|
140 | * [github/jonschlinkert](https://github.com/jonschlinkert)
|
---|
141 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
---|
142 |
|
---|
143 | ### License
|
---|
144 |
|
---|
145 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
---|
146 | Released under the [MIT License](LICENSE).
|
---|
147 |
|
---|
148 | ***
|
---|
149 |
|
---|
150 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 21, 2017._ |
---|