Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add insert option #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions .verb.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 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.

Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.

## 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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) |
Expand All @@ -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._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on October 06, 2021._
15 changes: 5 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down