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

break: 3.0 #23

Merged
merged 13 commits into from
Jan 28, 2021
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ jobs:
if: matrix.nodejs >= 14
run: npm install -g c8

# - name: Build
# run: npm run build
- name: Build
run: npm run build

- name: Test
run: npm test
if: matrix.nodejs < 14

- name: (coverage) Test
# run: c8 --include=src npm test
run: c8 npm test
run: c8 --include=src npm test
if: matrix.nodejs >= 14

- name: (coverage) Report
Expand Down
2 changes: 1 addition & 1 deletion bench/immutable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('uvu/assert');
const { Suite } = require('benchmark');
const { klona } = require('klona/json');
const dset = require('../dist/dset');
const { dset } = require('../dist');

const contenders = {
'clean-set': require('clean-set'),
Expand Down
2 changes: 1 addition & 1 deletion bench/mutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const contenders = {
'deep-set': require('deep-set'),
'set-value': require('set-value'),
'lodash/set': require('lodash/set'),
'dset': require('../dist/dset'),
'dset': require('../dist').dset,
};

console.log('Validation: ');
Expand Down
8 changes: 4 additions & 4 deletions bench/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Validation:
✔ dset

Benchmark:
deep-set x 1,894,926 ops/sec ±2.51% (88 runs sampled)
set-value x 2,208,207 ops/sec ±2.79% (92 runs sampled)
lodash/set x 1,271,022 ops/sec ±1.34% (90 runs sampled)
dset x 2,217,614 ops/sec ±0.55% (96 runs sampled)
deep-set x 1,545,526 ops/sec ±1.49% (89 runs sampled)
set-value x 1,704,871 ops/sec ±2.81% (92 runs sampled)
lodash/set x 995,789 ops/sec ±1.66% (91 runs sampled)
dset x 1,757,022 ops/sec ±0.12% (97 runs sampled)
```

#### Immutable
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function <T extends object, V>(obj: T, keys: string | ArrayLike<string>, value: V): void;
export function dset<T extends object, V>(obj: T, keys: string | ArrayLike<string | number>, value: V): void;
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
"name": "dset",
"version": "2.1.0",
"repository": "lukeed/dset",
"description": "A tiny (190B) utility for safely writing deep Object values~!",
"unpkg": "dist/dset.min.js",
"umd:main": "dist/dset.min.js",
"module": "dist/dset.es.js",
"main": "dist/dset.js",
"description": "A tiny (196B) utility for safely writing deep Object values~!",
"unpkg": "dist/index.min.js",
"umd:main": "dist/index.min.js",
"module": "dist/index.mjs",
"main": "dist/index.js",
"types": "index.d.ts",
"license": "MIT",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"author": {
"name": "Luke Edwards",
"email": "luke.edwards05@gmail.com",
Expand All @@ -19,8 +26,7 @@
},
"scripts": {
"build": "bundt",
"pretest": "npm run build",
"test": "uvu test"
"test": "uvu -r esm test"
},
"files": [
"*.d.ts",
Expand All @@ -37,6 +43,7 @@
],
"devDependencies": {
"bundt": "1.1.2",
"esm": "3.2.25",
"uvu": "0.5.1"
}
}
94 changes: 62 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,79 @@
# dset [![CI](https://github.com/lukeed/dset/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/lukeed/dset/actions)

> A tiny (190B) utility for safely writing deep Object values~!

This module exposes two module definitions:

* **ES Module**: `dist/dset.es.js`
* **CommonJS**: `dist/dset.js`
* **UMD**: `dist/dset.min.js`
> A tiny (196B) utility for safely writing deep Object values~!

For _accessing_ deep object properties, please see [`dlv`](https://github.com/developit/dlv).

## Install

```
```sh
$ npm install --save dset
```


## Usage

```js
const dset = require('dset');
import { dset } from 'dset';

let foo = { a:1, b:2 };
let bar = { foo:123, bar:[4, 5, 6], baz:{} };
let baz = { a:1, b:{ x:{ y:{ z:999 } } }, c:3 };
let qux = { };
let foo = { abc: 123 };
dset(foo, 'foo.bar', 'hello');
// or: dset(foo, ['foo', 'bar'], 'hello');
console.log(foo);
//=> {
//=> abc: 123,
//=> foo: { bar: 'hello' },
//=> }

dset(foo, 'd.e.f', 'hello');
// or ~> dset(foo, ['d', 'e', 'f'], 'hello');
dset(foo, 'abc.hello', 'world');
// or: dset(foo, ['abc', 'hello'], 'world');
console.log(foo);
//=> { a:1, b:2, d:{ e:{ f:'hello' } } };
//=> {
//=> abc: { hello: 'world' },
//=> foo: { bar: 'hello' },
//=> }

let bar = { a: { x: 7 }, b:[1, 2, 3] };
dset(bar, 'bar.1', 999);
// or ~> dset(bar, ['bar', 1], 999);
// or: dset(bar, ['bar', 1], 999);
// or: dset(bar, ['bar', '1'], 999);
console.log(bar);
//=> { foo:123, bar:[4, 999, 6], baz:{} };

dset(baz, 'b.x.j.k', 'mundo');
dset(baz, 'b.x.y.z', 'hola');
//=> {
//=> a: { x: 7 },
//=> bar: [1, 999, 3],
//=> }

dset(bar, 'a.y.0', 8);
// or: dset(bar, ['a', 'y', 0], 8);
// or: dset(bar, ['a', 'y', '0'], 8);
console.log(bar);
//=> {
//=> a: {
//=> x: 7,
//=> y: [8],
//=> },
//=> bar: [1, 999, 3],
//=> }

let baz = {};
dset(baz, 'a.0.b.0', 1);
dset(baz, 'a.0.b.1', 2);
console.log(baz);
//=> { a:1, b:{ x:{ y:{ z:'hola' }, j:{ k:'mundo' } } }, c:3 }

dset(qux, 'a.0.b.0', 1);
dset(qux, 'a.0.b.1', 2);
console.log(qux);
//=> { a: [{ b: [1, 2] }] }
//=> {
//=> a: [{ b: [1, 2] }]
//=> }
```

## Mutability
## Immutability

As shown in the examples above, all `dset` interactions mutate the source object.

If you need immutable writes, please visit [`clean-set`](https://github.com/fwilkerson/clean-set) (182B).<br>
Alternatively, you may pair `dset` with [`klona`](https://github.com/lukeed/klona), a 366B utility to clone your source(s). Here's an example pairing:

```js
import klona from 'klona';
import dset from 'dset';
import { klona } from 'klona';
import { dset } from 'dset';

export function deepset(obj, path, val) {
let copy = klona(obj);
Expand Down Expand Up @@ -87,7 +103,7 @@ The key path that should receive the value. May be in `x.y.z` or `['x', 'y', 'z'

> **Note:** Please be aware that only the _last_ key actually receives the value!

> **Important:** New Objects are created at each segment if there is not an existing structure.<br>When numerical-types are encounted, Arrays are created instead!
> **Important:** New Objects are created at each segment if there is not an existing structure.<br>However, when integers are encounted, Arrays are created instead!

#### value

Expand All @@ -98,7 +114,21 @@ The value that you want to set. Can be of any type!

## Benchmarks

For benchmark results, check out the [`bench`](/bench) directory!
For benchmarks and full results, check out the [`bench`](/bench) directory!

```
# Node 10.13.0

Validation:
✔ set-value
✔ lodash/set
✔ dset

Benchmark:
set-value x 1,701,821 ops/sec ±1.81% (93 runs sampled)
lodash/set x 975,530 ops/sec ±0.96% (91 runs sampled)
dset x 1,797,922 ops/sec ±0.32% (94 runs sampled)
```


## Related
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export default function (obj, keys, val) {
export function dset(obj, keys, val) {
keys.split && (keys=keys.split('.'));
var i=0, l=keys.length, t=obj, x, k;
for (; i < l;) {
k = keys[i++];
if (k === '__proto__' || k === 'constructor' || k === 'prototype') continue;
t = t[k] = (i === l ? val : ((x=t[k]) != null ? x : (keys[i]*0 !== 0 || !!~keys[i].indexOf('.')) ? {} : []));
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
t = t[k] = (i === l) ? val : (typeof(x=t[k])===typeof(keys)) ? x : (keys[i]*0 !== 0 || !!~(''+keys[i]).indexOf('.')) ? {} : [];
}
}
Loading