2015-12-02 10:35:51 -08:00
|
|
|
import {
|
|
|
|
IS_DART,
|
|
|
|
StringWrapper,
|
|
|
|
isBlank,
|
|
|
|
isPresent,
|
|
|
|
isString,
|
|
|
|
isArray
|
|
|
|
} from 'angular2/src/facade/lang';
|
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-11 09:56:50 -08:00
|
|
|
var SINGLE_QUOTE_ESCAPE_STRING_RE = /'|\\|\n|\r|\$/g;
|
|
|
|
var DOUBLE_QUOTE_ESCAPE_STRING_RE = /"|\\|\n|\r|\$/g;
|
2015-08-27 16:29:02 -07:00
|
|
|
|
2015-10-01 10:07:49 -07:00
|
|
|
export var MODULE_SUFFIX = IS_DART ? '.dart' : '.js';
|
|
|
|
|
2015-12-02 10:35:51 -08:00
|
|
|
export var CONST_VAR = IS_DART ? 'const' : 'var';
|
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
export function camelCaseToDashCase(input: string): string {
|
|
|
|
return StringWrapper.replaceAllMapped(input, CAMEL_CASE_REGEXP,
|
|
|
|
(m) => { return '-' + m[1].toLowerCase(); });
|
|
|
|
}
|
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export function dashCaseToCamelCase(input: string): string {
|
|
|
|
return StringWrapper.replaceAllMapped(input, DASH_CASE_REGEXP,
|
|
|
|
(m) => { return m[1].toUpperCase(); });
|
|
|
|
}
|
2015-09-11 13:37:05 -07:00
|
|
|
|
|
|
|
export function escapeSingleQuoteString(input: string): string {
|
|
|
|
if (isBlank(input)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return `'${escapeString(input, SINGLE_QUOTE_ESCAPE_STRING_RE)}'`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function escapeDoubleQuoteString(input: string): string {
|
|
|
|
if (isBlank(input)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return `"${escapeString(input, DOUBLE_QUOTE_ESCAPE_STRING_RE)}"`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeString(input: string, re: RegExp): string {
|
|
|
|
return StringWrapper.replaceAllMapped(input, re, (match) => {
|
2015-09-18 10:33:23 -07:00
|
|
|
if (match[0] == '$') {
|
|
|
|
return IS_DART ? '\\$' : '$';
|
|
|
|
} else if (match[0] == '\n') {
|
2015-09-11 13:37:05 -07:00
|
|
|
return '\\n';
|
2015-12-11 09:56:50 -08:00
|
|
|
} else if (match[0] == '\r') {
|
|
|
|
return '\\r';
|
2015-09-11 13:37:05 -07:00
|
|
|
} else {
|
|
|
|
return `\\${match[0]}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-09-14 15:59:09 -07:00
|
|
|
|
2015-11-02 08:39:14 -08:00
|
|
|
export function codeGenExportVariable(name: string): string {
|
2015-10-29 11:24:02 -07:00
|
|
|
if (IS_DART) {
|
2015-11-02 08:39:14 -08:00
|
|
|
return `const ${name} = `;
|
2015-10-29 11:24:02 -07:00
|
|
|
} else {
|
|
|
|
return `var ${name} = exports['${name}'] = `;
|
|
|
|
}
|
2015-09-14 15:59:09 -07:00
|
|
|
}
|
|
|
|
|
2015-11-02 08:39:14 -08:00
|
|
|
export function codeGenConstConstructorCall(name: string): string {
|
2015-09-14 15:59:09 -07:00
|
|
|
if (IS_DART) {
|
2015-11-02 08:39:14 -08:00
|
|
|
return `const ${name}`;
|
2015-09-14 15:59:09 -07:00
|
|
|
} else {
|
2015-11-02 08:39:14 -08:00
|
|
|
return `new ${name}`;
|
2015-09-14 15:59:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 10:07:49 -07:00
|
|
|
export function codeGenValueFn(params: string[], value: string, fnName: string = ''): string {
|
2015-09-14 15:59:09 -07:00
|
|
|
if (IS_DART) {
|
2015-12-02 10:35:51 -08:00
|
|
|
return `${codeGenFnHeader(params, fnName)} => ${value}`;
|
2015-09-14 15:59:09 -07:00
|
|
|
} else {
|
2015-12-02 10:35:51 -08:00
|
|
|
return `${codeGenFnHeader(params, fnName)} { return ${value}; }`;
|
2015-09-14 15:59:09 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-18 10:33:23 -07:00
|
|
|
|
2015-12-02 10:35:51 -08:00
|
|
|
export function codeGenFnHeader(params: string[], fnName: string = ''): string {
|
|
|
|
if (IS_DART) {
|
|
|
|
return `${fnName}(${params.join(',')})`;
|
|
|
|
} else {
|
|
|
|
return `function ${fnName}(${params.join(',')})`;
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 10:30:33 -07:00
|
|
|
export function codeGenToString(expr: string): string {
|
|
|
|
if (IS_DART) {
|
|
|
|
return `'\${${expr}}'`;
|
|
|
|
} else {
|
2015-12-16 15:47:48 +08:00
|
|
|
// JS automatically converts to string...
|
2015-09-28 10:30:33 -07:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 10:35:51 -08:00
|
|
|
|
|
|
|
|
|
|
|
export class Statement {
|
|
|
|
constructor(public statement: string) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Expression {
|
|
|
|
constructor(public expression: string, public isArray = false) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function escapeValue(value: any): string {
|
|
|
|
if (value instanceof Expression) {
|
|
|
|
return value.expression;
|
|
|
|
} else if (isString(value)) {
|
|
|
|
return escapeSingleQuoteString(value);
|
|
|
|
} else if (isBlank(value)) {
|
|
|
|
return 'null';
|
|
|
|
} else {
|
|
|
|
return `${value}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function codeGenArray(data: any[]): string {
|
|
|
|
return `[${data.map(escapeValue).join(',')}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function codeGenFlatArray(values: any[]): string {
|
|
|
|
var result = '([';
|
|
|
|
var isFirstArrayEntry = true;
|
|
|
|
var concatFn = IS_DART ? '.addAll' : 'concat';
|
|
|
|
for (var i = 0; i < values.length; i++) {
|
|
|
|
var value = values[i];
|
|
|
|
if (value instanceof Expression && (<Expression>value).isArray) {
|
|
|
|
result += `]).${concatFn}(${value.expression}).${concatFn}([`;
|
|
|
|
isFirstArrayEntry = true;
|
|
|
|
} else {
|
|
|
|
if (!isFirstArrayEntry) {
|
|
|
|
result += ',';
|
|
|
|
}
|
|
|
|
isFirstArrayEntry = false;
|
|
|
|
result += escapeValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += '])';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function codeGenStringMap(keyValueArray: any[][]): string {
|
|
|
|
return `{${keyValueArray.map(codeGenKeyValue).join(',')}}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function codeGenKeyValue(keyValue: any[]): string {
|
|
|
|
return `${escapeValue(keyValue[0])}:${escapeValue(keyValue[1])}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addAll(source: any[], target: any[]) {
|
|
|
|
for (var i = 0; i < source.length; i++) {
|
|
|
|
target.push(source[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function flattenArray(source: any[], target: any[]): any[] {
|
|
|
|
if (isPresent(source)) {
|
|
|
|
for (var i = 0; i < source.length; i++) {
|
|
|
|
var item = source[i];
|
|
|
|
if (isArray(item)) {
|
|
|
|
flattenArray(item, target);
|
|
|
|
} else {
|
|
|
|
target.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|