Skip to content

Commit

Permalink
Create Arrays w/ Numeric keys (#9)
Browse files Browse the repository at this point in the history
* Add array creation support; Add UMD support

* Update readme.md

* Reverting build tool change

* revert `dist` changes

* 🏌️‍♂️⛳️

* update test syntax

* revert version change

* update readme docs

* update bytesize
  • Loading branch information
bwendt-mylo authored and lukeed committed Sep 7, 2018
1 parent f8ce2f6 commit e6fea6f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dset",
"version": "1.0.1",
"repository": "lukeed/dset",
"description": "A tiny (135B) utility for safely writing deep Object values~!",
"description": "A tiny (144B) utility for safely writing deep Object values~!",
"module": "dist/dset.es.js",
"main": "dist/dset.js",
"license": "MIT",
Expand Down
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dset [![Build Status](https://travis-ci.org/lukeed/dset.svg?branch=master)](https://travis-ci.org/lukeed/dset)

> A tiny (135B) utility for safely writing deep Object values~!
> A tiny (144B) utility for safely writing deep Object values~!
This module exposes two module definitions:

Expand All @@ -24,6 +24,7 @@ const dset = require('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 = { };

dset(foo, 'd.e.f', 'hello');
// or ~> dset(foo, ['d', 'e', 'f'], 'hello');
Expand All @@ -39,6 +40,11 @@ dset(baz, 'b.x.j.k', 'mundo');
dset(baz, 'b.x.y.z', 'hola');
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] }] }
```

## API
Expand All @@ -61,7 +67,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.
> **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!
#### value

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export default function (obj, keys, val) {
var i=0, l=keys.length, t=obj, x;
for (; i < l; ++i) {
x = t[keys[i]];
t = t[keys[i]] = (i === l - 1 ? val : (x == null ? {} : x));
t = t[keys[i]] = (i === l - 1 ? val : (x == null ? (+keys[i + 1] > -1 ? [] : {}) : x));
}
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ test('dset', t => {
fn(foo, ['d', 'a', 'b'], 123); // change via array
t.is(foo.d.a.b, 123, 'mutates; changes the value via array-type keys');

fn(foo, 'e.0.0', 1); // create arrays instead of objects
t.is(foo.e[0][0], 1, 'mutates; can create arrays via flag and when next key is numeric');

fn(foo, 'd.a.x.y', 456); // preserve existing structure
t.same(foo.d, { a:{ b:123, x:{y:456} }}, 'mutates; writes into/preserves existing object');

Expand Down

0 comments on commit e6fea6f

Please sign in to comment.