From 005a3d40945b184e87b4b701c9cb931eacfbb2a0 Mon Sep 17 00:00:00 2001 From: tcatche Date: Thu, 22 Jul 2021 13:32:37 +0800 Subject: [PATCH] Feature: When execute filter's callback, add the data params as last param --- lib/extend/filter.js | 2 ++ test/scripts/extend/filter.js | 36 +++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/extend/filter.js b/lib/extend/filter.js index 520b22668e..91cdc8206e 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -62,6 +62,7 @@ class Filter { const ctx = options.context; const args = options.args || []; + args.push(data); args.unshift(data); return Promise.each(filters, filter => Reflect.apply(Promise.method(filter), ctx, args).then(result => { @@ -78,6 +79,7 @@ class Filter { const ctx = options.context; const args = options.args || []; + args.push(data); args.unshift(data); for (let i = 0, len = filtersLen; i < len; i++) { diff --git a/test/scripts/extend/filter.js b/test/scripts/extend/filter.js index 3f03ee74b6..53ea502e19 100644 --- a/test/scripts/extend/filter.js +++ b/test/scripts/extend/filter.js @@ -94,13 +94,15 @@ describe('Filter', () => { it('exec()', async () => { const f = new Filter(); - const filter1 = spy(data => { + const filter1 = spy((data, origin) => { data.should.eql(''); + origin.should.eql(''); return data + 'foo'; }); - const filter2 = spy(data => { + const filter2 = spy((data, origin) => { data.should.eql('foo'); + origin.should.eql(''); return data + 'bar'; }); @@ -117,13 +119,15 @@ describe('Filter', () => { it('exec() - pointer', async () => { const f = new Filter(); - const filter1 = spy(data => { + const filter1 = spy((data, origin) => { data.should.eql({}); + origin.should.eql({}); data.foo = 1; }); - const filter2 = spy(data => { + const filter2 = spy((data, origin) => { data.should.eql({foo: 1}); + origin.should.eql({foo: 1}); data.bar = 2; }); @@ -140,14 +144,16 @@ describe('Filter', () => { it('exec() - args', async () => { const f = new Filter(); - const filter1 = spy((data, arg1, arg2) => { + const filter1 = spy((data, arg1, arg2, arg3) => { arg1.should.eql(1); arg2.should.eql(2); + arg3.should.eql({}); }); - const filter2 = spy((data, arg1, arg2) => { + const filter2 = spy((data, arg1, arg2, arg3) => { arg1.should.eql(1); arg2.should.eql(2); + arg3.should.eql({}); }); f.register('test', filter1); @@ -180,13 +186,15 @@ describe('Filter', () => { it('execSync()', () => { const f = new Filter(); - const filter1 = spy(data => { + const filter1 = spy((data, origin) => { data.should.eql(''); + origin.should.eql(''); return data + 'foo'; }); - const filter2 = spy(data => { + const filter2 = spy((data, origin) => { data.should.eql('foo'); + origin.should.eql(''); return data + 'bar'; }); @@ -202,13 +210,15 @@ describe('Filter', () => { it('execSync() - pointer', () => { const f = new Filter(); - const filter1 = spy(data => { + const filter1 = spy((data, origin) => { data.should.eql({}); + origin.should.eql({}); data.foo = 1; }); - const filter2 = spy(data => { + const filter2 = spy((data, origin) => { data.should.eql({foo: 1}); + origin.should.eql({foo: 1}); data.bar = 2; }); @@ -224,14 +234,16 @@ describe('Filter', () => { it('execSync() - args', () => { const f = new Filter(); - const filter1 = spy((data, arg1, arg2) => { + const filter1 = spy((data, arg1, arg2, arg3) => { arg1.should.eql(1); arg2.should.eql(2); + arg3.should.eql({}); }); - const filter2 = spy((data, arg1, arg2) => { + const filter2 = spy((data, arg1, arg2, arg3) => { arg1.should.eql(1); arg2.should.eql(2); + arg3.should.eql({}); }); f.register('test', filter1);