2016-06-23 09:47:54 -07: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-05-18 13:46:51 -07:00
|
|
|
import {Pipe, Type, resolveForwardRef, ɵstringify as stringify} from '@angular/core';
|
|
|
|
|
|
|
|
import {CompileReflector} from './compile_reflector';
|
2017-03-01 14:10:59 -08:00
|
|
|
import {findLast} from './directive_resolver';
|
2016-12-15 09:12:40 -08:00
|
|
|
import {CompilerInjectable} from './injectable';
|
2015-08-07 11:41:38 -07:00
|
|
|
|
2015-10-22 23:35:53 -07:00
|
|
|
function _isPipeMetadata(type: any): boolean {
|
2016-09-12 19:14:17 -07:00
|
|
|
return type instanceof Pipe;
|
2015-10-22 23:35:53 -07:00
|
|
|
}
|
|
|
|
|
2015-08-07 11:41:38 -07:00
|
|
|
/**
|
2016-09-12 19:14:17 -07:00
|
|
|
* Resolve a `Type` for {@link Pipe}.
|
2015-08-07 11:41:38 -07:00
|
|
|
*
|
|
|
|
* This interface can be overridden by the application developer to create custom behavior.
|
|
|
|
*
|
|
|
|
* See {@link Compiler}
|
|
|
|
*/
|
2016-12-15 09:12:40 -08:00
|
|
|
@CompilerInjectable()
|
2015-08-07 11:41:38 -07:00
|
|
|
export class PipeResolver {
|
2017-05-18 13:46:51 -07:00
|
|
|
constructor(private _reflector: CompileReflector) {}
|
2016-03-24 13:32:47 -07:00
|
|
|
|
2016-11-10 14:07:30 -08:00
|
|
|
isPipe(type: Type<any>) {
|
|
|
|
const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
|
|
|
|
return typeMetadata && typeMetadata.some(_isPipeMetadata);
|
|
|
|
}
|
|
|
|
|
2015-08-07 11:41:38 -07:00
|
|
|
/**
|
2016-09-12 19:14:17 -07:00
|
|
|
* Return {@link Pipe} for a given `Type`.
|
2015-08-07 11:41:38 -07:00
|
|
|
*/
|
2017-03-24 09:59:58 -07:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): Pipe|null {
|
2016-11-12 14:08:58 +01:00
|
|
|
const metas = this._reflector.annotations(resolveForwardRef(type));
|
2017-01-04 13:59:43 -08:00
|
|
|
if (metas) {
|
2017-03-01 14:10:59 -08:00
|
|
|
const annotation = findLast(metas, _isPipeMetadata);
|
2017-01-04 13:59:43 -08:00
|
|
|
if (annotation) {
|
2015-10-22 23:35:53 -07:00
|
|
|
return annotation;
|
2015-08-07 11:41:38 -07:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
if (throwIfNotFound) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(`No Pipe decorator found on ${stringify(type)}`);
|
2016-07-18 03:50:31 -07:00
|
|
|
}
|
|
|
|
return null;
|
2015-08-07 11:41:38 -07:00
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
}
|