Skip to content

Commit

Permalink
Merge pull request #24 from dkebler/setter-observable
Browse files Browse the repository at this point in the history
swap missing parent assignment of {} to after last target check to av…
  • Loading branch information
jonschlinkert committed Apr 28, 2021
2 parents db9602f + 8a69dfe commit fb3e52d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
10 changes: 4 additions & 6 deletions index.js
Expand Up @@ -37,17 +37,15 @@ function set(target, path, value, options) {
for (let i = 0; i < len; i++) {
let prop = keys[i];

if (!isObject(target[prop])) {
let nextProp = i + 1 < len ? keys[i + 1] : "";
let shouldBeArray = !isNaN(+nextProp);
target[prop] = shouldBeArray ? [] : {};
}

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
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
Expand Up @@ -199,25 +199,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 fb3e52d

Please sign in to comment.