Skip to content

Commit

Permalink
feat(change_detection): add static append method to Pipes
Browse files Browse the repository at this point in the history
This change allows creation of a new Pipes object with new pipes appended
to pipes of an inherited Pipes.

Closes angular#2901
  • Loading branch information
jeffbcross committed Jul 9, 2015
1 parent 669b0e4 commit 9c70da4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
26 changes: 25 additions & 1 deletion modules/angular2/src/change_detection/pipes/pipes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {ListWrapper, isListLikeIterable, StringMapWrapper} from 'angular2/src/facade/collection';
import {isBlank, isPresent, BaseException, CONST} from 'angular2/src/facade/lang';
import {Pipe, PipeFactory} from './pipe';
import {Injectable} from 'angular2/src/di/decorators';
Expand All @@ -20,6 +20,30 @@ export class Pipes {
return factory.create(cdRef);
}

static append (config) {
return (pipes:Pipes):Pipes => {
var mergedConfig:StringMap<string,PipeFactory[]> = <StringMap<string,PipeFactory[]>>{};

// Manual deep copy of existing Pipes config,
// so that lists of PipeFactories don't get mutated.
StringMapWrapper.forEach(pipes.config, (v:PipeFactory[], k:string) => {
mergedConfig[k] = [];
v.forEach((p:PipeFactory) => {
(<PipeFactory[]>mergedConfig[k]).push(p);
});
});

StringMapWrapper.forEach(config, (v:PipeFactory[], k:string) => {
if (isListLikeIterable(mergedConfig[k])) {
mergedConfig[k] = ListWrapper.concat(mergedConfig[k], config[k]);
} else {
mergedConfig[k] = config[k];
}
});
return new Pipes(mergedConfig);
};
}

private _getListOfFactories(type: string, obj: any): PipeFactory[] {
var listOfFactories = this.config[type];
if (isBlank(listOfFactories)) {
Expand Down
20 changes: 20 additions & 0 deletions modules/angular2/test/change_detection/pipes/pipes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from 'angular2/test_lib';

import {Pipes} from 'angular2/src/change_detection/pipes/pipes';
import {PipeFactory} from 'angular2/src/change_detection/pipes/pipe';

export function main() {
describe("pipe registry", () => {
Expand Down Expand Up @@ -73,5 +74,24 @@ export function main() {
expect(() => r.get("type", "some object"))
.toThrowError(`Cannot find 'type' pipe supporting object 'some object'`);
});

describe('.append()', () => {
it('should create a factory that appends new pipes to old', () => {
firstPipeFactory.spy("supports").andReturn(false);
secondPipeFactory.spy("supports").andReturn(true);
secondPipeFactory.spy("create").andReturn(secondPipe);
var originalPipes:Pipes = new Pipes({
'async': [firstPipeFactory]
});
var factory = Pipes.append({
'async': <PipeFactory[]>[secondPipeFactory]
});
var pipes = factory(originalPipes);

expect(pipes.config['async'].length).toBe(2);
expect(originalPipes.config['async'].length).toBe(1);
expect(pipes.get('async', 'second plz')).toBe(secondPipe);
});
});
});
}

0 comments on commit 9c70da4

Please sign in to comment.