Skip to content

Commit 318637f

Browse files
committed
feat(change_detection): add static append method to Pipes
This change allows creation of a new Pipes object with new pipes appended to pipes of an inherited Pipes. Closes angular#2901
1 parent 669b0e4 commit 318637f

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

modules/angular2/src/change_detection/pipes/pipes.ts

+70-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {List, ListWrapper} from 'angular2/src/facade/collection';
1+
import {ListWrapper, isListLikeIterable, StringMapWrapper} from 'angular2/src/facade/collection';
22
import {isBlank, isPresent, BaseException, CONST} from 'angular2/src/facade/lang';
33
import {Pipe, PipeFactory} from './pipe';
44
import {Injectable} from 'angular2/src/di/decorators';
@@ -7,7 +7,25 @@ import {ChangeDetectorRef} from '../change_detector_ref';
77
@Injectable()
88
@CONST()
99
export class Pipes {
10-
constructor(public config) {}
10+
/**
11+
* Map of {@link Pipe} names to {@link PipeFactory} lists used to configure the
12+
* {@link Pipes} registry.
13+
*
14+
* #Example
15+
*
16+
* ```
17+
* var pipesConfig = {
18+
* 'json': [jsonPipeFactory]
19+
* }
20+
* @Component({
21+
* viewInjector: [
22+
* bind(Pipes).toValue(new Pipes(pipesConfig))
23+
* ]
24+
* })
25+
* ```
26+
*/
27+
config: StringMap<string, PipeFactory[]>;
28+
constructor(config: StringMap<string, PipeFactory[]>) { this.config = config; }
1129

1230
get(type: string, obj, cdRef?: ChangeDetectorRef, existingPipe?: Pipe): Pipe {
1331
if (isPresent(existingPipe) && existingPipe.supports(obj)) return existingPipe;
@@ -20,6 +38,56 @@ export class Pipes {
2038
return factory.create(cdRef);
2139
}
2240

41+
/**
42+
* Takes a {@link Pipes} config object and returns a factory used to append the
43+
* provided config with an existing {@link Pipes} instance to return a new
44+
* {@link Pipes} instance.
45+
*
46+
* If the provided config contains a key that is not yet present in the
47+
* inherited {@link Pipes}' config, a new {@link PipeFactory} list will be created
48+
* for that key. Otherwise, the provided config will be merged with the inherited
49+
* {@link Pipes} instance by appending pipes to their respective keys, without mutating
50+
* the inherited {@link Pipes}.
51+
*
52+
* The following example shows how to append a new {@link PipeFactory} to the
53+
* existing list of `async` factories, which will only be applied to the injector
54+
* for this component and its children. This step is all that's required to make a new
55+
* pipe available to this component's template.
56+
*
57+
* # Example
58+
*
59+
* ```
60+
* @Component({
61+
* viewInjector: [
62+
* bind(Pipes).toFactory(Pipes.append({
63+
* async: [newAsyncPipe]
64+
* })
65+
* ]
66+
* })
67+
* ```
68+
*/
69+
static append(config) {
70+
return (pipes: Pipes): Pipes => {
71+
var mergedConfig: StringMap<string, PipeFactory[]> = <StringMap<string, PipeFactory[]>>{};
72+
73+
// Manual deep copy of existing Pipes config,
74+
// so that lists of PipeFactories don't get mutated.
75+
StringMapWrapper.forEach(pipes.config, (v: PipeFactory[], k: string) => {
76+
var localPipeList: PipeFactory[] = mergedConfig[k] = [];
77+
v.forEach((p: PipeFactory) => { localPipeList.push(p); });
78+
});
79+
80+
StringMapWrapper.forEach(config, (v: PipeFactory[], k: string) => {
81+
if (isListLikeIterable(mergedConfig[k])) {
82+
mergedConfig[k] = ListWrapper.concat(mergedConfig[k], config[k]);
83+
} else {
84+
mergedConfig[k] = config[k];
85+
}
86+
});
87+
return new Pipes(mergedConfig);
88+
};
89+
}
90+
2391
private _getListOfFactories(type: string, obj: any): PipeFactory[] {
2492
var listOfFactories = this.config[type];
2593
if (isBlank(listOfFactories)) {

modules/angular2/test/change_detection/pipes/pipes_spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from 'angular2/test_lib';
1313

1414
import {Pipes} from 'angular2/src/change_detection/pipes/pipes';
15+
import {PipeFactory} from 'angular2/src/change_detection/pipes/pipe';
1516

1617
export function main() {
1718
describe("pipe registry", () => {
@@ -73,5 +74,20 @@ export function main() {
7374
expect(() => r.get("type", "some object"))
7475
.toThrowError(`Cannot find 'type' pipe supporting object 'some object'`);
7576
});
77+
78+
describe('.append()', () => {
79+
it('should create a factory that appends new pipes to old', () => {
80+
firstPipeFactory.spy("supports").andReturn(false);
81+
secondPipeFactory.spy("supports").andReturn(true);
82+
secondPipeFactory.spy("create").andReturn(secondPipe);
83+
var originalPipes: Pipes = new Pipes({'async': [firstPipeFactory]});
84+
var factory = Pipes.append({'async':<PipeFactory[]>[secondPipeFactory]});
85+
var pipes = factory(originalPipes);
86+
87+
expect(pipes.config['async'].length).toBe(2);
88+
expect(originalPipes.config['async'].length).toBe(1);
89+
expect(pipes.get('async', 'second plz')).toBe(secondPipe);
90+
});
91+
});
7692
});
7793
}

0 commit comments

Comments
 (0)