Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: When execute filter's callback, add new parameters. #4744

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/extend/filter.js
Expand Up @@ -62,6 +62,7 @@ class Filter {
const ctx = options.context;
const args = options.args || [];

args.push(data);
args.unshift(data);
tcatche marked this conversation as resolved.
Show resolved Hide resolved

return Promise.each(filters, filter => Reflect.apply(Promise.method(filter), ctx, args).then(result => {
Expand All @@ -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++) {
Expand Down
36 changes: 24 additions & 12 deletions test/scripts/extend/filter.js
Expand Up @@ -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';
});

Expand All @@ -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;
});

Expand All @@ -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);
Expand Down Expand Up @@ -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';
});

Expand All @@ -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;
});

Expand All @@ -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);
Expand Down