Skip to content

Commit

Permalink
Merge branch 'master' into array-support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert authored Apr 28, 2021
2 parents f37500e + fb3e52d commit 0dc7ff3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
sudo: false
os:
- linux
- osx
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ function isObject(val) {
}

set.memo = {};
module.exports = set;
module.exports = set;
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
85 changes: 81 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ describe('set', function() {
assert.equal(o.a.b, 'c');
});

it('should create a nested array if it does not already exist', function() {
const o = {};
set(o, 'a.0', 'c');
set(o, 'a.1', 'd');
assert(Array.isArray(o.a));
assert.equal(o.a[0], 'c');
let actual = "";
o.a.map(i=> actual +=i);
assert.equal(actual, "cd");
});

it('should merge an existing value with the given value', function() {
var o = {a: {b: {c: 'd'}}};
set(o, 'a.b', {y: 'z'}, { merge: true });
Expand Down Expand Up @@ -199,25 +210,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 0dc7ff3

Please sign in to comment.