Skip to content

Commit

Permalink
feat(mixin): support lodash chain
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 committed Aug 9, 2017
1 parent 8f05477 commit 0a08614
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/aigle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3765,6 +3765,17 @@ function mixin(sources, opts = {}) {
if (typeof func !== 'function' || Aigle[key] && !override) {
return;
}
// check lodash chain
if (key === 'chain') {
const obj = func();
if (obj && obj.__chain__) {
Aigle.chain = _resolve;
Aigle.prototype.value = function() {
return this;
};
return;
}
}
const Proxy = createProxy(func, promisify);
Aigle[key] = function(value, arg1, arg2, arg3) {
return new Proxy(value, arg1, arg2, arg3)._execute();
Expand All @@ -3773,4 +3784,5 @@ function mixin(sources, opts = {}) {
return addProxy(this, Proxy, arg1, arg2, arg3);
};
});
return Aigle;
}
2 changes: 2 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
parserOptions:
ecmaVersion: 8
rules:
# Best Practices
no-return-assign: "off"
13 changes: 12 additions & 1 deletion test/lib/test.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ parallel('mixin', () => {
it('should execute with function', () => {

const test1 = value => value * 2;
Aigle.mixin({ test1 }, { promisify: false });
const obj = Aigle.mixin({ test1 }, { promisify: false });
assert.strictEqual(obj, Aigle);
return Aigle.resolve(1)
.test1()
.then(value => assert.strictEqual(value, 2));
Expand All @@ -40,6 +41,16 @@ parallel('mixin', () => {
return promise;
});

it('should allow the lodash chain', async () => {

const lo = Aigle.mixin(_);
const array = await lo.chain([1, 2, 3])
.map(async n => Aigle.delay(DELAY, n * 2))
.value();
assert.deepEqual(array, [2, 4, 6]);
assert.strictEqual(await lo.sum(array), 12);
});

it('should override Aigle functions', () => {

Aigle.mixin({ map: _.map }, { override: true, promisify: false });
Expand Down

0 comments on commit 0a08614

Please sign in to comment.