2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-02-17 15:55:55 -05:00
|
|
|
import {Pipe, Type, resolveForwardRef, ɵReflectorReader, ɵreflector} from '@angular/core';
|
2016-04-28 20:50:03 -04:00
|
|
|
|
2016-11-18 18:17:44 -05:00
|
|
|
import {ListWrapper} from './facade/collection';
|
2017-01-04 16:59:43 -05:00
|
|
|
import {stringify} from './facade/lang';
|
2016-12-15 12:12:40 -05:00
|
|
|
import {CompilerInjectable} from './injectable';
|
2015-08-07 14:41:38 -04:00
|
|
|
|
2015-10-23 02:35:53 -04:00
|
|
|
function _isPipeMetadata(type: any): boolean {
|
2016-09-12 22:14:17 -04:00
|
|
|
return type instanceof Pipe;
|
2015-10-23 02:35:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:38 -04:00
|
|
|
/**
|
2016-09-12 22:14:17 -04:00
|
|
|
* Resolve a `Type` for {@link Pipe}.
|
2015-08-07 14:41:38 -04:00
|
|
|
*
|
|
|
|
* This interface can be overridden by the application developer to create custom behavior.
|
|
|
|
*
|
|
|
|
* See {@link Compiler}
|
|
|
|
*/
|
2016-12-15 12:12:40 -05:00
|
|
|
@CompilerInjectable()
|
2015-08-07 14:41:38 -04:00
|
|
|
export class PipeResolver {
|
2017-02-17 15:55:55 -05:00
|
|
|
constructor(private _reflector: ɵReflectorReader = ɵreflector) {}
|
2016-03-24 16:32:47 -04:00
|
|
|
|
2016-11-10 17:07:30 -05:00
|
|
|
isPipe(type: Type<any>) {
|
|
|
|
const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
|
|
|
|
return typeMetadata && typeMetadata.some(_isPipeMetadata);
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:38 -04:00
|
|
|
/**
|
2016-09-12 22:14:17 -04:00
|
|
|
* Return {@link Pipe} for a given `Type`.
|
2015-08-07 14:41:38 -04:00
|
|
|
*/
|
2016-09-12 22:14:17 -04:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): Pipe {
|
2016-11-12 08:08:58 -05:00
|
|
|
const metas = this._reflector.annotations(resolveForwardRef(type));
|
2017-01-04 16:59:43 -05:00
|
|
|
if (metas) {
|
2016-11-18 18:17:44 -05:00
|
|
|
const annotation = ListWrapper.findLast(metas, _isPipeMetadata);
|
2017-01-04 16:59:43 -05:00
|
|
|
if (annotation) {
|
2015-10-23 02:35:53 -04:00
|
|
|
return annotation;
|
2015-08-07 14:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 06:50:31 -04:00
|
|
|
if (throwIfNotFound) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(`No Pipe decorator found on ${stringify(type)}`);
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|
|
|
|
return null;
|
2015-08-07 14:41:38 -04:00
|
|
|
}
|
2016-07-18 06:50:31 -04:00
|
|
|
}
|