2016-06-23 16:47:54 +00: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-12-15 21:07:12 +00:00
|
|
|
import {BaseError} from './facade/errors';
|
2016-11-03 00:40:15 +00:00
|
|
|
import {isPrimitive, isStrictStringMap} from './facade/lang';
|
2016-07-31 02:31:25 +00:00
|
|
|
export const MODULE_SUFFIX = '';
|
2015-08-25 22:36:02 +00:00
|
|
|
|
2016-10-19 20:42:39 +00:00
|
|
|
const CAMEL_CASE_REGEXP = /([A-Z])/g;
|
2016-11-08 23:45:30 +00:00
|
|
|
const DASH_CASE_REGEXP = /-+([a-z0-9])/g;
|
2015-12-02 18:35:51 +00:00
|
|
|
|
2015-08-27 23:29:02 +00:00
|
|
|
export function camelCaseToDashCase(input: string): string {
|
2016-10-06 22:10:27 +00:00
|
|
|
return input.replace(CAMEL_CASE_REGEXP, (...m: any[]) => '-' + m[1].toLowerCase());
|
2016-11-08 23:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function dashCaseToCamelCase(input: string): string {
|
|
|
|
|
return input.replace(DASH_CASE_REGEXP, (...m: any[]) => m[1].toUpperCase());
|
2015-08-27 23:29:02 +00:00
|
|
|
}
|
2015-09-11 20:37:05 +00:00
|
|
|
|
2015-09-18 17:33:23 +00:00
|
|
|
export function splitAtColon(input: string, defaultValues: string[]): string[] {
|
2016-09-23 20:37:04 +00: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 17:33:23 +00:00
|
|
|
}
|
2016-04-21 01:10:19 +00:00
|
|
|
|
2016-04-30 23:13:03 +00:00
|
|
|
export function visitValue(value: any, visitor: ValueVisitor, context: any): any {
|
2016-10-19 20:42:39 +00:00
|
|
|
if (Array.isArray(value)) {
|
2016-04-30 23:13:03 +00:00
|
|
|
return visitor.visitArray(<any[]>value, context);
|
2016-10-19 20:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isStrictStringMap(value)) {
|
2016-04-30 23:13:03 +00:00
|
|
|
return visitor.visitStringMap(<{[key: string]: any}>value, context);
|
2016-10-19 20:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-03 00:40:15 +00:00
|
|
|
if (value == null || isPrimitive(value)) {
|
2016-04-30 23:13:03 +00:00
|
|
|
return visitor.visitPrimitive(value, context);
|
|
|
|
|
}
|
2016-10-19 20:42:39 +00:00
|
|
|
|
|
|
|
|
return visitor.visitOther(value, context);
|
2016-04-30 23:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-11-12 13:08:58 +00:00
|
|
|
const result: {[key: string]: any} = {};
|
2016-10-08 00:36:08 +00:00
|
|
|
Object.keys(map).forEach(key => { result[key] = visitValue(map[key], this, context); });
|
2016-04-30 23:13:03 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
visitPrimitive(value: any, context: any): any { return value; }
|
|
|
|
|
visitOther(value: any, context: any): any { return value; }
|
|
|
|
|
}
|
2016-04-29 00:50:03 +00:00
|
|
|
|
2016-06-28 16:54:42 +00:00
|
|
|
export class SyncAsyncResult<T> {
|
|
|
|
|
constructor(public syncResult: T, public asyncResult: Promise<T> = null) {
|
|
|
|
|
if (!asyncResult) {
|
2016-07-12 20:55:06 +00:00
|
|
|
this.asyncResult = Promise.resolve(syncResult);
|
2016-06-28 16:54:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-15 21:07:12 +00:00
|
|
|
|
|
|
|
|
export class SyntaxError extends BaseError {}
|