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
|
|
|
|
*/
|
|
|
|
|
2016-06-28 09:54:42 -07:00
|
|
|
import {CompileTokenMetadata} from './compile_metadata';
|
2016-10-06 15:10:27 -07:00
|
|
|
import {isArray, isBlank, isPresent, isPrimitive, isStrictStringMap} from './facade/lang';
|
2016-06-28 09:54:42 -07:00
|
|
|
import * as o from './output/output_ast';
|
2016-01-06 14:13:44 -08:00
|
|
|
|
2016-07-30 19:31:25 -07:00
|
|
|
export const MODULE_SUFFIX = '';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
var CAMEL_CASE_REGEXP = /([A-Z])/g;
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
export function camelCaseToDashCase(input: string): string {
|
2016-10-06 15:10:27 -07:00
|
|
|
return input.replace(CAMEL_CASE_REGEXP, (...m: any[]) => '-' + m[1].toLowerCase());
|
2015-08-27 16:29:02 -07:00
|
|
|
}
|
2015-09-11 13:37:05 -07:00
|
|
|
|
2015-09-18 10:33:23 -07:00
|
|
|
export function splitAtColon(input: string, defaultValues: string[]): string[] {
|
2016-09-23 16:37:04 -04:00
|
|
|
return _splitAt(input, ':', defaultValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function splitAtPeriod(input: string, defaultValues: string[]): string[] {
|
|
|
|
return _splitAt(input, '.', defaultValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _splitAt(input: string, character: string, defaultValues: string[]): string[] {
|
|
|
|
const characterIndex = input.indexOf(character);
|
|
|
|
if (characterIndex == -1) return defaultValues;
|
|
|
|
return [input.slice(0, characterIndex).trim(), input.slice(characterIndex + 1).trim()];
|
2015-09-18 10:33:23 -07:00
|
|
|
}
|
2016-04-20 18:10:19 -07:00
|
|
|
|
|
|
|
export function sanitizeIdentifier(name: string): string {
|
2016-10-06 15:10:27 -07:00
|
|
|
return name.replace(/\W/g, '_');
|
2016-04-20 18:10:19 -07:00
|
|
|
}
|
2016-04-30 16:13:03 -07:00
|
|
|
|
|
|
|
export function visitValue(value: any, visitor: ValueVisitor, context: any): any {
|
|
|
|
if (isArray(value)) {
|
|
|
|
return visitor.visitArray(<any[]>value, context);
|
|
|
|
} else if (isStrictStringMap(value)) {
|
|
|
|
return visitor.visitStringMap(<{[key: string]: any}>value, context);
|
|
|
|
} else if (isBlank(value) || isPrimitive(value)) {
|
|
|
|
return visitor.visitPrimitive(value, context);
|
|
|
|
} else {
|
|
|
|
return visitor.visitOther(value, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ValueVisitor {
|
|
|
|
visitArray(arr: any[], context: any): any;
|
|
|
|
visitStringMap(map: {[key: string]: any}, context: any): any;
|
|
|
|
visitPrimitive(value: any, context: any): any;
|
|
|
|
visitOther(value: any, context: any): any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ValueTransformer implements ValueVisitor {
|
|
|
|
visitArray(arr: any[], context: any): any {
|
|
|
|
return arr.map(value => visitValue(value, this, context));
|
|
|
|
}
|
|
|
|
visitStringMap(map: {[key: string]: any}, context: any): any {
|
2016-10-07 17:36:08 -07:00
|
|
|
var result: {[key: string]: any} = {};
|
|
|
|
Object.keys(map).forEach(key => { result[key] = visitValue(map[key], this, context); });
|
2016-04-30 16:13:03 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
visitPrimitive(value: any, context: any): any { return value; }
|
|
|
|
visitOther(value: any, context: any): any { return value; }
|
|
|
|
}
|
2016-04-28 17:50:03 -07:00
|
|
|
|
2016-05-01 22:50:37 -07:00
|
|
|
export function assetUrl(pkg: string, path: string = null, type: string = 'src'): string {
|
2016-07-30 19:31:25 -07:00
|
|
|
if (path == null) {
|
|
|
|
return `asset:@angular/lib/${pkg}/index`;
|
2016-04-28 17:50:03 -07:00
|
|
|
} else {
|
2016-07-30 19:31:25 -07:00
|
|
|
return `asset:@angular/lib/${pkg}/src/${path}`;
|
2016-04-28 17:50:03 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-28 09:54:42 -07:00
|
|
|
|
|
|
|
export function createDiTokenExpression(token: CompileTokenMetadata): o.Expression {
|
|
|
|
if (isPresent(token.value)) {
|
|
|
|
return o.literal(token.value);
|
|
|
|
} else if (token.identifierIsInstance) {
|
|
|
|
return o.importExpr(token.identifier)
|
|
|
|
.instantiate([], o.importType(token.identifier, [], [o.TypeModifier.Const]));
|
|
|
|
} else {
|
|
|
|
return o.importExpr(token.identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SyncAsyncResult<T> {
|
|
|
|
constructor(public syncResult: T, public asyncResult: Promise<T> = null) {
|
|
|
|
if (!asyncResult) {
|
2016-07-12 13:55:06 -07:00
|
|
|
this.asyncResult = Promise.resolve(syncResult);
|
2016-06-28 09:54:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|