lukeed / dset

A tiny (194B) utility for safely writing deep Object values~!
MIT License
754 stars 22 forks source link

Non-integers treated as valid indices for created arrays #11

Closed Andarist closed 5 years ago

Andarist commented 5 years ago
var obj = {}
dset(obj, ['foo', '10.2', 'baz'], 'bar')

Produces an array with 10.2 property.

lukeed commented 5 years ago

What would you expect it to be? This is the same result as 1.x branch. If you pass an array of keys (instead of dot-notation), it accepts each item as is, which is ideal for pre-formatting keys instead of risking hello.world to be split up for whatever reason.

Andarist commented 5 years ago
Array.isArray(obj.foo)
// dset@2 - true
// dset@1 - false
// _.set - false
// R.assocPath - false

I didn't expect the key to be split, but rather that non-integer (non-positive) would not create an array.

lukeed commented 5 years ago

I see it now 😄 Bug with tape.js actually, I think

foo = {};
fn(foo, ['x', '10.2', 'z'], 123);
t.same(foo, { x:{ '10.2':{ z:123 } } });

^ Passed, despite the result being an array as you've pointed out.