diff --git a/.verb.md b/.verb.md index 11f41e8..455730a 100644 --- a/.verb.md +++ b/.verb.md @@ -104,6 +104,22 @@ set(obj, 'foo.bar.fez', 'zzz', { merge: true }); //=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } } ``` +### options.insert + +Allows you to insert values to array, instead of overwriting them. + +**Type**: `boolean` + +**Default**: `undefined` + +**Example** + +```js +const obj = { path: { to: { array: ['b', 'c'] } } }; +set(obj, 'path.to.array.0', 'a', { insert: true }); +//=> { path: { to: { array: ['a', 'b', 'c'] } } } +``` + ## Benchmarks Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3. diff --git a/README.md b/README.md index 854ec8d..be08f5c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 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) [![Tests](https://github.com/jonschlinkert/set-value/actions/workflows/main.yml/badge.svg)](https://github.com/jonschlinkert/set-value/actions/workflows/main.yml) +# 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) > Set nested properties on an object using dot notation. @@ -6,7 +6,7 @@ Please consider following this project's author, [Jon Schlinkert](https://github ## Install -Install with [npm](https://www.npmjs.com/) (requires [Node.js](https://nodejs.org/en/) >=11.0): +Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save set-value @@ -116,6 +116,22 @@ set(obj, 'foo.bar.fez', 'zzz', { merge: true }); //=> { foo: { bar: { baz: 'qux', fez: 'zzz' } } } ``` +### options.insert + +Allows you to insert values to array, instead of overwriting them. + +**Type**: `boolean` + +**Default**: `undefined` + +**Example** + +```js +const obj = { path: { to: { array: ['b', 'c'] } } }; +set(obj, 'path.to.array.0', 'a', { insert: true }); +//=> { path: { to: { array: ['a', 'b', 'c'] } } } +``` + ## Benchmarks Benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3. @@ -273,7 +289,7 @@ You might also be interested in these projects: | **Commits** | **Contributor** | | --- | --- | -| 87 | [jonschlinkert](https://github.com/jonschlinkert) | +| 90 | [jonschlinkert](https://github.com/jonschlinkert) | | 4 | [doowb](https://github.com/doowb) | | 2 | [mbelsky](https://github.com/mbelsky) | | 1 | [dkebler](https://github.com/dkebler) | @@ -299,4 +315,4 @@ Released under the [MIT License](LICENSE). *** -_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on September 12, 2021._ \ No newline at end of file +_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on October 06, 2021._ \ No newline at end of file diff --git a/index.js b/index.js index ecf8371..fcee155 100644 --- a/index.js +++ b/index.js @@ -40,6 +40,7 @@ const createMemoKey = (input, options) => { if (options.separator !== undefined) key += `separator=${options.separator};`; if (options.split !== undefined) key += `split=${options.split};`; if (options.merge !== undefined) key += `merge=${options.merge};`; + if (options.insert !== undefined) key += `insert=${options.insert};`; if (options.preservePaths !== undefined) key += `preservePaths=${options.preservePaths};`; return key; }; @@ -110,17 +111,11 @@ const assignProp = (obj, prop, value, options) => { // Delete property when "value" is undefined if (value === undefined) { deleteProperty(obj, prop); - - } else if (options && options.merge) { + } else if (options && options.merge && isPlainObject(obj[prop]) && isPlainObject(value)) { const merge = options.merge === 'function' ? options.merge : Object.assign; - - // Only merge plain objects - if (merge && isPlainObject(obj[prop]) && isPlainObject(value)) { - obj[prop] = merge(obj[prop], value); - } else { - obj[prop] = value; - } - + obj[prop] = merge(obj[prop], value); + } else if (options && options.insert && Array.isArray(obj)) { + obj.splice(prop, 0, value) } else { obj[prop] = value; } diff --git a/test/test.js b/test/test.js index 92e200b..242e99e 100644 --- a/test/test.js +++ b/test/test.js @@ -238,6 +238,18 @@ describe('set-value', () => { }); }); + describe('options.insert', () => { + it('should update an array by inserting the given value', () => { + const o = { a: ['a', 'b', 'd'] }; + set(o, 'a.2', 'c', { insert: true }); + assert.deepEqual(o.a, ['a', 'b', 'c', 'd']); + + const obj = { path: { to: { array: ['b', 'c'] } } }; + set(obj, 'path.to.array.0', 'a', { insert: true, merge: true }); + assert.deepEqual(obj, { path: { to: { array: ['a', 'b', 'c'] } } }); + }); + }); + describe('options.preservePaths', () => { it('should split properties with a forward slash when preservePaths is false', () => { const obj = {};