From e6fea6f0509d2f2df805e9c4b4e364a1d7023db9 Mon Sep 17 00:00:00 2001 From: Brian Wendt Date: Fri, 7 Sep 2018 00:19:45 -0500 Subject: [PATCH] Create Arrays w/ Numeric keys (#9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- package.json | 2 +- readme.md | 10 ++++++++-- src/index.js | 2 +- test/index.js | 3 +++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 63c3fcd..a083fbc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index ce88ef7..ba21921 100644 --- a/readme.md +++ b/readme.md @@ -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: @@ -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'); @@ -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 @@ -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.
When numerical-types are encounted, Arrays are created instead! #### value diff --git a/src/index.js b/src/index.js index 5a9c911..e46f5b4 100644 --- a/src/index.js +++ b/src/index.js @@ -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)); } } diff --git a/test/index.js b/test/index.js index ec9d2bd..13fffdd 100644 --- a/test/index.js +++ b/test/index.js @@ -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');