Skip to content

Commit 8e7b283

Browse files
committed
Add Pipe remove/replace methods and document them
1 parent 0e078a7 commit 8e7b283

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

docs/plugins.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,16 @@ To complete the plugin, let's add the reverse filter, so numeric deltas can be r
9494
assertSame(right, { population: 400 });
9595
```
9696

97+
Pipe API
98+
------
99+
100+
The following methods are offered to manipulate filters in a pipe.
101+
102+
- `append(filter1, filter2, ...)` - Append one or more filters to the existing list
103+
- `prepend(filter1, filter2, ...)` - Prepend one or more filters to the existing list
104+
- `after(filterName, filter1, filter2, ...)` - Add one ore more filters after the specified filter
105+
- `before(filterName, filter1, filter2, ...)` - Add one ore more filters before the specified filter
106+
- `replace(filterName, filter1, filter2, ...)` - Replace the specified filter with one ore more filters
107+
- `remove(filterName)` - Remove the filter with the specified name
108+
- `clear()` - Remove all filters from this pipe
109+
- `list()` - Return array of ordered filter names for this pipe

src/pipe.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ Pipe.prototype.before = function(filterName) {
8484
return this;
8585
};
8686

87+
Pipe.prototype.replace = function(filterName) {
88+
var index = this.indexOf(filterName);
89+
var params = Array.prototype.slice.call(arguments, 1);
90+
if (!params.length) {
91+
throw new Error('a filter is required');
92+
}
93+
params.unshift(index, 1);
94+
Array.prototype.splice.apply(this.filters, params);
95+
return this;
96+
};
97+
98+
Pipe.prototype.remove = function(filterName) {
99+
var index = this.indexOf(filterName);
100+
this.filters.splice(index, 1);
101+
return this;
102+
};
103+
87104
Pipe.prototype.clear = function() {
88105
this.filters.length = 0;
89106
return this;

test/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,32 @@ describe('DiffPatcher', function() {
334334

335335
});
336336

337+
describe('removing and replacing pipe filters', function(){
338+
it('removes specified filter', function(){
339+
expect(this.instance.processor.pipes.diff.list()).to.be.deepEqual([
340+
'collectChildren', 'numeric', 'trivial', 'dates', 'texts', 'objects', 'arrays'
341+
]);
342+
this.instance.processor.pipes.diff.remove('dates');
343+
expect(this.instance.processor.pipes.diff.list()).to.be.deepEqual([
344+
'collectChildren', 'numeric', 'trivial', 'texts', 'objects', 'arrays'
345+
]);
346+
});
347+
348+
it('replaces specified filter', function(){
349+
function fooFilter(context) {
350+
context.setResult(['foo']).exit();
351+
}
352+
fooFilter.filterName = 'foo';
353+
expect(this.instance.processor.pipes.diff.list()).to.be.deepEqual([
354+
'collectChildren', 'numeric', 'trivial', 'texts', 'objects', 'arrays'
355+
]);
356+
this.instance.processor.pipes.diff.replace('trivial', fooFilter);
357+
expect(this.instance.processor.pipes.diff.list()).to.be.deepEqual([
358+
'collectChildren', 'numeric', 'foo', 'texts', 'objects', 'arrays'
359+
]);
360+
});
361+
});
362+
337363
});
338364

339365
describe('formatters', function () {

0 commit comments

Comments
 (0)