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-17 15:39:08 -07:00
|
|
|
import {ɵisPromise as isPromise} from '@angular/core';
|
|
|
|
|
2017-05-16 16:30:37 -07:00
|
|
|
import * as o from './output/output_ast';
|
2017-06-09 14:50:57 -07:00
|
|
|
import {ParseError} from './parse_util';
|
2017-05-16 16:30:37 -07:00
|
|
|
|
2016-07-30 19:31:25 -07:00
|
|
|
export const MODULE_SUFFIX = '';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
2016-10-19 13:42:39 -07:00
|
|
|
const CAMEL_CASE_REGEXP = /([A-Z])/g;
|
2016-11-08 15:45:30 -08:00
|
|
|
const DASH_CASE_REGEXP = /-+([a-z0-9])/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());
|
2016-11-08 15:45:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function dashCaseToCamelCase(input: string): string {
|
|
|
|
return input.replace(DASH_CASE_REGEXP, (...m: any[]) => m[1].toUpperCase());
|
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
|
|
|
|
2016-04-30 16:13:03 -07:00
|
|
|
export function visitValue(value: any, visitor: ValueVisitor, context: any): any {
|
2016-10-19 13:42:39 -07:00
|
|
|
if (Array.isArray(value)) {
|
2016-04-30 16:13:03 -07:00
|
|
|
return visitor.visitArray(<any[]>value, context);
|
2016-10-19 13:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isStrictStringMap(value)) {
|
2016-04-30 16:13:03 -07:00
|
|
|
return visitor.visitStringMap(<{[key: string]: any}>value, context);
|
2016-10-19 13:42:39 -07:00
|
|
|
}
|
|
|
|
|
2017-03-02 09:37:01 -08:00
|
|
|
if (value == null || typeof value == 'string' || typeof value == 'number' ||
|
|
|
|
typeof value == 'boolean') {
|
2016-04-30 16:13:03 -07:00
|
|
|
return visitor.visitPrimitive(value, context);
|
|
|
|
}
|
2016-10-19 13:42:39 -07:00
|
|
|
|
|
|
|
return visitor.visitOther(value, context);
|
2016-04-30 16:13:03 -07:00
|
|
|
}
|
|
|
|
|
2017-03-24 09:59:58 -07:00
|
|
|
export function isDefined(val: any): boolean {
|
|
|
|
return val !== null && val !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function noUndefined<T>(val: T | undefined): T {
|
|
|
|
return val === undefined ? null ! : val;
|
|
|
|
}
|
|
|
|
|
2016-04-30 16:13:03 -07: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 14:08:58 +01:00
|
|
|
const result: {[key: string]: any} = {};
|
2016-10-07 17:36:08 -07:00
|
|
|
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
|
|
|
|
2017-05-17 15:39:08 -07:00
|
|
|
export type SyncAsync<T> = T | Promise<T>;
|
|
|
|
|
|
|
|
export const SyncAsync = {
|
|
|
|
assertSync: <T>(value: SyncAsync<T>): T => {
|
|
|
|
if (isPromise(value)) {
|
|
|
|
throw new Error(`Illegal state: value cannot be a promise`);
|
2016-06-28 09:54:42 -07:00
|
|
|
}
|
2017-05-17 15:39:08 -07:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
then: <T, R>(value: SyncAsync<T>, cb: (value: T) => R | Promise<R>| SyncAsync<R>):
|
|
|
|
SyncAsync<R> => { return isPromise(value) ? value.then(cb) : cb(value);},
|
|
|
|
all: <T>(syncAsyncValues: SyncAsync<T>[]): SyncAsync<T[]> => {
|
|
|
|
return syncAsyncValues.some(isPromise) ? Promise.all(syncAsyncValues) : syncAsyncValues as T[];
|
2016-06-28 09:54:42 -07:00
|
|
|
}
|
2017-06-09 14:50:57 -07:00
|
|
|
};
|
2016-12-15 13:07:12 -08:00
|
|
|
|
2017-06-09 14:50:57 -07:00
|
|
|
export function syntaxError(msg: string, parseErrors?: ParseError[]): Error {
|
|
|
|
const error = Error(msg);
|
|
|
|
(error as any)[ERROR_SYNTAX_ERROR] = true;
|
|
|
|
if (parseErrors) (error as any)[ERROR_PARSE_ERRORS] = parseErrors;
|
|
|
|
return error;
|
|
|
|
}
|
2017-01-27 13:19:00 -08:00
|
|
|
|
|
|
|
const ERROR_SYNTAX_ERROR = 'ngSyntaxError';
|
2017-06-09 14:50:57 -07:00
|
|
|
const ERROR_PARSE_ERRORS = 'ngParseErrors';
|
2017-01-27 13:19:00 -08:00
|
|
|
|
|
|
|
export function isSyntaxError(error: Error): boolean {
|
|
|
|
return (error as any)[ERROR_SYNTAX_ERROR];
|
|
|
|
}
|
2017-03-02 09:37:01 -08:00
|
|
|
|
2017-06-09 14:50:57 -07:00
|
|
|
export function getParseErrors(error: Error): ParseError[] {
|
|
|
|
return (error as any)[ERROR_PARSE_ERRORS] || [];
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:37:01 -08:00
|
|
|
export function escapeRegExp(s: string): string {
|
|
|
|
return s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
|
|
|
|
}
|
|
|
|
|
|
|
|
const STRING_MAP_PROTO = Object.getPrototypeOf({});
|
|
|
|
function isStrictStringMap(obj: any): boolean {
|
|
|
|
return typeof obj === 'object' && obj !== null && Object.getPrototypeOf(obj) === STRING_MAP_PROTO;
|
|
|
|
}
|
2017-03-14 17:13:39 -07:00
|
|
|
|
|
|
|
export function utf8Encode(str: string): string {
|
|
|
|
let encoded = '';
|
|
|
|
for (let index = 0; index < str.length; index++) {
|
|
|
|
let codePoint = str.charCodeAt(index);
|
|
|
|
|
|
|
|
// decode surrogate
|
|
|
|
// see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
|
|
|
|
if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > (index + 1)) {
|
|
|
|
const low = str.charCodeAt(index + 1);
|
|
|
|
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
|
|
index++;
|
|
|
|
codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codePoint <= 0x7f) {
|
|
|
|
encoded += String.fromCharCode(codePoint);
|
|
|
|
} else if (codePoint <= 0x7ff) {
|
|
|
|
encoded += String.fromCharCode(((codePoint >> 6) & 0x1F) | 0xc0, (codePoint & 0x3f) | 0x80);
|
|
|
|
} else if (codePoint <= 0xffff) {
|
|
|
|
encoded += String.fromCharCode(
|
|
|
|
(codePoint >> 12) | 0xe0, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
|
|
|
|
} else if (codePoint <= 0x1fffff) {
|
|
|
|
encoded += String.fromCharCode(
|
|
|
|
((codePoint >> 18) & 0x07) | 0xf0, ((codePoint >> 12) & 0x3f) | 0x80,
|
|
|
|
((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoded;
|
|
|
|
}
|
2017-05-16 16:30:37 -07:00
|
|
|
|
|
|
|
export interface OutputContext {
|
|
|
|
genFilePath: string;
|
|
|
|
statements: o.Statement[];
|
|
|
|
importExpr(reference: any, typeParams?: o.Type[]|null): o.Expression;
|
|
|
|
}
|