Skip to content

Commit

Permalink
swap missing parent assignment of {} to after last target check to av…
Browse files Browse the repository at this point in the history
…oid extraneous assignment and support setter with observable
  • Loading branch information
dkebler committed Jun 12, 2020
1 parent 8353e4d commit 4952147
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ function set(target, path, value, options) {
for (let i = 0; i < len; i++) {
let prop = keys[i];

if (!isObject(target[prop])) {
target[prop] = {};
}

if (i === len - 1) {
result(target, prop, value, merge);
break;
}

if (!isObject(target[prop])) {
target[prop] = {};
}

target = target[prop];
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"test": "mocha"
},
"dependencies": {
"is-plain-object": "^2.0.4"
"is-plain-object": "^2.0.4",
"rxjs": "^6.5.5"
},
"devDependencies": {
"benchmarked": "^2.0.0",
Expand Down
74 changes: 70 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,91 @@ describe('options', function() {

it('should use a custom function to not split inside square brackets', function() {
var o = {};
set(o, "a.[b.c.d].e", 'c', options);
set(o, 'a.[b.c.d].e', 'c', options);
assert.equal(o.a['[b.c.d]'].e, 'c');
});

it('should use a custom function to not split inside parens', function() {
var o = {};
set(o, "a.(b.c.d).e", 'c', options);
set(o, 'a.(b.c.d).e', 'c', options);
assert.equal(o.a['(b.c.d)'].e, 'c');
});

it('should use a custom function to not split inside angle brackets', function() {
var o = {};
set(o, "a.<b.c.d>.e", 'c', options);
set(o, 'a.<b.c.d>.e', 'c', options);
assert.equal(o.a['<b.c.d>'].e, 'c');
});

it('should use a custom function to not split inside curly braces', function() {
var o = {};
set(o, "a.{b.c.d}.e", 'c', options);
set(o, 'a.{b.c.d}.e', 'c', options);
assert.equal(o.a['{b.c.d}'].e, 'c');
});
});

const Rx = require('rxjs');
const opers = require('rxjs/operators');

var _value = 0;
var o = {a: { b: {} } };
var obs = Rx.from(new Rx.BehaviorSubject()).pipe(
opers.skip(1),
);

Object.defineProperty(o.a.b, 'c', {
configurable: true,
get() { return _value; },
set(value) {
_value = value;
obs.next(value);
}
});

describe('Setter with Observable', function() {
// const expected = 11;
var received = [];
const noop = () => {};
it('should only assign/emit once for each call of set', function(done) {
var subs = obs.subscribe(
data => { received.push(data); },
noop,
() => {
assert.equal(received.length, 1);
done();
}
);
set(o, 'a.b.c', 5);
subs.complete();
});

it('should work assignment via setter', function(done) {
received = null;
var subs = obs.subscribe(
data => { received = data; },
noop,
() => {
assert.equal(received, 10);
done();
}
);
set(o, 'a.b.c', 10);
subs.complete();
});

it('should work with merge of object via setter', function(done) {
received = null;
set(o, 'a.b.c', {foo: 'bar'});
var subs = obs.subscribe(
data => { received = data; },
noop,
() => {
assert.deepEqual(o.a.b.c, { foo: 'bar', bing: 'bong' });
done();
}
);
set(o, 'a.b.c', { bing: 'bong'}, {merge: true});
subs.complete();
});

});

0 comments on commit 4952147

Please sign in to comment.