Skip to content

Commit 740b8bb

Browse files
committed
feat: add mergeContext method
1 parent 360fa00 commit 740b8bb

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/core/Service/libs/BaseService.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ class BaseService {
197197
hasKey(name) {
198198
return !!this[name];
199199
}
200+
201+
/**
202+
* 扩充合并 context
203+
* @param {Array|String} ctx 上下文
204+
* @return {Object} context
205+
*/
206+
mergeContext(ctx = []) {
207+
const args = [].concat(ctx);
208+
const newCtx = yParser(args);
209+
return smartMerge(this.context, newCtx);
210+
}
200211
}
201212

202213
module.exports = BaseService;

src/core/Service/libs/BaseService.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ describe('BaseService', () => {
3636

3737
});
3838

39+
it('mergeContext', () => {
40+
process.env.NODE_ENV = 'test';
41+
const base = new BaseService();
42+
const newCtx = base.mergeContext([ '--abc', '--abc-bc' ]);
43+
44+
expect(newCtx.abc).toBeTruthy();
45+
expect(newCtx.abcBc).toBeTruthy();
46+
});
47+
3948
});

0 commit comments

Comments
 (0)