2016-04-30 16:13:03 -07:00
|
|
|
import {
|
|
|
|
IS_DART,
|
|
|
|
StringWrapper,
|
|
|
|
Math,
|
|
|
|
isBlank,
|
|
|
|
isArray,
|
|
|
|
isStrictStringMap,
|
|
|
|
isPrimitive
|
2016-04-28 17:50:03 -07:00
|
|
|
} from './facade/lang';
|
|
|
|
import {StringMapWrapper} from './facade/collection';
|
2016-01-06 14:13:44 -08:00
|
|
|
|
|
|
|
export var MODULE_SUFFIX = IS_DART ? '.dart' : '';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
var CAMEL_CASE_REGEXP = /([A-Z])/g;
|
2015-08-25 15:36:02 -07:00
|
|
|
var DASH_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-04-12 09:40:37 -07:00
|
|
|
return StringWrapper.replaceAllMapped(input, CAMEL_CASE_REGEXP,
|
|
|
|
(m) => { return '-' + m[1].toLowerCase(); });
|
2015-08-27 16:29:02 -07:00
|
|
|
}
|
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export function dashCaseToCamelCase(input: string): string {
|
2016-04-12 09:40:37 -07:00
|
|
|
return StringWrapper.replaceAllMapped(input, DASH_CASE_REGEXP,
|
|
|
|
(m) => { return m[1].toUpperCase(); });
|
2015-08-25 15:36: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[] {
|
|
|
|
var parts = StringWrapper.split(input.trim(), /\s*:\s*/g);
|
|
|
|
if (parts.length > 1) {
|
|
|
|
return parts;
|
|
|
|
} else {
|
|
|
|
return defaultValues;
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 18:10:19 -07:00
|
|
|
|
|
|
|
export function sanitizeIdentifier(name: string): string {
|
|
|
|
return StringWrapper.replaceAll(name, /\W/g, '_');
|
|
|
|
}
|
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 {
|
|
|
|
var result = {};
|
|
|
|
StringMapWrapper.forEach(map,
|
|
|
|
(value, key) => { result[key] = visitValue(value, this, context); });
|
|
|
|
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-04-28 17:50:03 -07:00
|
|
|
if (IS_DART) {
|
|
|
|
if (path == null) {
|
|
|
|
return `asset:angular2/${pkg}/${pkg}.dart`;
|
|
|
|
} else {
|
|
|
|
return `asset:angular2/lib/${pkg}/src/${path}.dart`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (path == null) {
|
|
|
|
return `asset:@angular/lib/${pkg}/index`;
|
|
|
|
} else {
|
|
|
|
return `asset:@angular/lib/${pkg}/src/${path}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|