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-02-01 10:29:42 -08:00
|
|
|
export interface BrowserNodeGlobal {
|
|
|
|
Object: typeof Object;
|
|
|
|
Array: typeof Array;
|
|
|
|
Map: typeof Map;
|
|
|
|
Set: typeof Set;
|
|
|
|
Date: DateConstructor;
|
|
|
|
RegExp: RegExpConstructor;
|
|
|
|
JSON: typeof JSON;
|
|
|
|
Math: any; // typeof Math;
|
|
|
|
assert(condition: any): void;
|
|
|
|
Reflect: any;
|
|
|
|
getAngularTestability: Function;
|
|
|
|
getAllAngularTestabilities: Function;
|
2016-02-18 13:51:20 -08:00
|
|
|
getAllAngularRootElements: Function;
|
2016-02-01 10:29:42 -08:00
|
|
|
frameworkStabilizers: Array<Function>;
|
|
|
|
setTimeout: Function;
|
|
|
|
clearTimeout: Function;
|
|
|
|
setInterval: Function;
|
|
|
|
clearInterval: Function;
|
2016-03-14 11:45:14 -07:00
|
|
|
encodeURI: Function;
|
2016-02-01 10:29:42 -08:00
|
|
|
}
|
|
|
|
|
2015-08-27 10:39:39 -07:00
|
|
|
// TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
|
2016-06-08 15:45:15 -07:00
|
|
|
declare var WorkerGlobalScope: any /** TODO #9100 */;
|
2016-04-28 17:50:03 -07:00
|
|
|
// CommonJS / Node have global context exposed as "global" variable.
|
|
|
|
// We don't want to include the whole node.d.ts this this compilation unit so we'll just fake
|
|
|
|
// the global "global" var for now.
|
2016-06-08 15:45:15 -07:00
|
|
|
declare var global: any /** TODO #9100 */;
|
2016-04-28 17:50:03 -07:00
|
|
|
|
2015-08-27 10:39:39 -07:00
|
|
|
var globalScope: BrowserNodeGlobal;
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
|
|
|
|
// TODO: Replace any with WorkerGlobalScope from lib.webworker.d.ts #3492
|
|
|
|
globalScope = <any>self;
|
|
|
|
} else {
|
|
|
|
globalScope = <any>global;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
globalScope = <any>window;
|
2016-02-01 10:29:42 -08:00
|
|
|
}
|
2015-08-27 10:39:39 -07:00
|
|
|
|
2016-02-25 14:24:17 -08:00
|
|
|
export function scheduleMicroTask(fn: Function) {
|
|
|
|
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:07:57 -08:00
|
|
|
export const IS_DART = false;
|
|
|
|
|
2015-08-27 10:39:39 -07:00
|
|
|
// Need to declare a new variable for global here since TypeScript
|
|
|
|
// exports the original value of the symbol.
|
|
|
|
var _global: BrowserNodeGlobal = globalScope;
|
|
|
|
|
2015-04-01 10:45:56 -07:00
|
|
|
export {_global as global};
|
|
|
|
|
2015-07-28 14:41:36 -07:00
|
|
|
/**
|
2015-09-21 12:19:51 -07:00
|
|
|
* Runtime representation a type that a Component or other object is instances of.
|
2015-07-28 14:41:36 -07:00
|
|
|
*
|
2015-09-21 12:19:51 -07:00
|
|
|
* An example of a `Type` is `MyCustomComponent` class, which in JavaScript is be represented by
|
|
|
|
* the `MyCustomComponent` constructor function.
|
2016-06-27 12:27:23 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-07-28 14:41:36 -07:00
|
|
|
*/
|
2016-06-28 11:01:35 -07:00
|
|
|
export var Type = Function;
|
|
|
|
|
|
|
|
|
2015-10-06 06:53:39 -07:00
|
|
|
export interface Type extends Function {}
|
2015-12-03 15:49:09 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runtime representation of a type that is constructable (non-abstract).
|
|
|
|
*/
|
2016-06-24 08:46:43 -07:00
|
|
|
export interface ConcreteType<T> extends Type { new (...args: any[]): T; }
|
2016-06-22 14:06:23 -07:00
|
|
|
|
2015-07-13 14:22:43 -07:00
|
|
|
export function getTypeNameForDebugging(type: Type): string {
|
2016-03-09 12:05:15 -08:00
|
|
|
if (type['name']) {
|
|
|
|
return type['name'];
|
|
|
|
}
|
|
|
|
return typeof type;
|
2015-07-13 14:22:43 -07:00
|
|
|
}
|
|
|
|
|
2015-07-14 19:53:04 -05:00
|
|
|
|
2015-04-01 10:45:56 -07:00
|
|
|
export var Math = _global.Math;
|
|
|
|
export var Date = _global.Date;
|
|
|
|
|
2015-05-20 17:19:46 -07:00
|
|
|
// TODO: remove calls to assert in production environment
|
|
|
|
// Note: Can't just export this and import in in other files
|
|
|
|
// as `assert` is a reserved keyword in Dart
|
2015-06-19 10:18:44 -07:00
|
|
|
_global.assert = function assert(condition) {
|
2015-10-28 18:21:47 +01:00
|
|
|
// TODO: to be fixed properly via #2830, noop for now
|
2015-06-19 10:18:44 -07:00
|
|
|
};
|
2015-06-19 12:14:12 -07:00
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isPresent(obj: any): boolean {
|
2015-04-01 10:45:56 -07:00
|
|
|
return obj !== undefined && obj !== null;
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isBlank(obj: any): boolean {
|
2015-04-01 10:45:56 -07:00
|
|
|
return obj === undefined || obj === null;
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:36:16 -07:00
|
|
|
export function isBoolean(obj: any): boolean {
|
2016-06-08 16:38:52 -07:00
|
|
|
return typeof obj === 'boolean';
|
2016-04-10 19:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isNumber(obj: any): boolean {
|
2016-06-08 16:38:52 -07:00
|
|
|
return typeof obj === 'number';
|
2016-04-10 19:36:16 -07:00
|
|
|
}
|
|
|
|
|
2016-06-10 16:31:34 -07:00
|
|
|
export function isString(obj: any): obj is String {
|
2016-06-08 16:38:52 -07:00
|
|
|
return typeof obj === 'string';
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isFunction(obj: any): boolean {
|
2016-06-08 16:38:52 -07:00
|
|
|
return typeof obj === 'function';
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isType(obj: any): boolean {
|
2015-04-15 17:56:14 -07:00
|
|
|
return isFunction(obj);
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:44:05 -07:00
|
|
|
export function isStringMap(obj: any): obj is Object {
|
2015-05-29 14:19:10 -07:00
|
|
|
return typeof obj === 'object' && obj !== null;
|
|
|
|
}
|
|
|
|
|
2016-04-30 16:13:03 -07:00
|
|
|
const STRING_MAP_PROTO = Object.getPrototypeOf({});
|
|
|
|
export function isStrictStringMap(obj: any): boolean {
|
|
|
|
return isStringMap(obj) && Object.getPrototypeOf(obj) === STRING_MAP_PROTO;
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isPromise(obj: any): boolean {
|
2015-06-11 19:32:55 +02:00
|
|
|
return obj instanceof (<any>_global).Promise;
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isArray(obj: any): boolean {
|
2015-06-11 19:32:55 +02:00
|
|
|
return Array.isArray(obj);
|
|
|
|
}
|
|
|
|
|
2016-06-11 21:23:37 -07:00
|
|
|
export function isDate(obj: any): obj is Date {
|
2015-07-04 22:34:54 +04:30
|
|
|
return obj instanceof Date && !isNaN(obj.valueOf());
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:57:28 -07:00
|
|
|
export function noop() {}
|
|
|
|
|
2016-06-11 21:23:37 -07:00
|
|
|
export function stringify(token: any): string {
|
2015-04-01 10:45:56 -07:00
|
|
|
if (typeof token === 'string') {
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token === undefined || token === null) {
|
|
|
|
return '' + token;
|
|
|
|
}
|
|
|
|
|
2015-12-02 10:35:51 -08:00
|
|
|
if (token.overriddenName) {
|
|
|
|
return token.overriddenName;
|
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
if (token.name) {
|
|
|
|
return token.name;
|
|
|
|
}
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-07-22 11:59:16 -07:00
|
|
|
var res = token.toString();
|
2016-06-08 16:38:52 -07:00
|
|
|
var newLineIndex = res.indexOf('\n');
|
2015-07-22 11:59:16 -07:00
|
|
|
return (newLineIndex === -1) ? res : res.substring(0, newLineIndex);
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 16:09:18 -07:00
|
|
|
// serialize / deserialize enum exist only for consistency with dart API
|
|
|
|
// enums in typescript don't need to be serialized
|
|
|
|
|
2016-06-11 21:23:37 -07:00
|
|
|
export function serializeEnum(val: any): number {
|
2015-07-10 16:09:18 -07:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2016-06-11 21:23:37 -07:00
|
|
|
export function deserializeEnum(val: any, values: Map<number, any>): any {
|
2015-07-10 16:09:18 -07:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2016-06-11 21:23:37 -07:00
|
|
|
export function resolveEnumToken(enumValue: any, val: any): string {
|
2016-03-08 16:38:50 -08:00
|
|
|
return enumValue[val];
|
|
|
|
}
|
|
|
|
|
2015-04-01 10:45:56 -07:00
|
|
|
export class StringWrapper {
|
2015-08-20 16:25:34 -07:00
|
|
|
static fromCharCode(code: number): string { return String.fromCharCode(code); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-08-20 16:25:34 -07:00
|
|
|
static charCodeAt(s: string, index: number): number { return s.charCodeAt(index); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
static split(s: string, regExp: RegExp): string[] { return s.split(regExp); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
|
|
|
static equals(s: string, s2: string): boolean { return s === s2; }
|
|
|
|
|
2015-12-05 02:21:38 -08:00
|
|
|
static stripLeft(s: string, charVal: string): string {
|
|
|
|
if (s && s.length) {
|
|
|
|
var pos = 0;
|
|
|
|
for (var i = 0; i < s.length; i++) {
|
|
|
|
if (s[i] != charVal) break;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
s = s.substring(pos);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static stripRight(s: string, charVal: string): string {
|
|
|
|
if (s && s.length) {
|
|
|
|
var pos = s.length;
|
|
|
|
for (var i = s.length - 1; i >= 0; i--) {
|
|
|
|
if (s[i] != charVal) break;
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
s = s.substring(0, pos);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-04-01 10:45:56 -07:00
|
|
|
static replace(s: string, from: string, replace: string): string {
|
|
|
|
return s.replace(from, replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
static replaceAll(s: string, from: RegExp, replace: string): string {
|
|
|
|
return s.replace(from, replace);
|
|
|
|
}
|
|
|
|
|
2015-08-30 17:04:15 -07:00
|
|
|
static slice<T>(s: string, from: number = 0, to: number = null): string {
|
|
|
|
return s.slice(from, to === null ? undefined : to);
|
|
|
|
}
|
|
|
|
|
2016-06-20 13:18:49 -07:00
|
|
|
static replaceAllMapped(s: string, from: RegExp, cb: (m: string[]) => string): string {
|
2016-06-11 21:23:37 -07:00
|
|
|
return s.replace(from, function(...matches: any[]) {
|
2015-04-01 10:45:56 -07:00
|
|
|
// Remove offset & string from the result array
|
|
|
|
matches.splice(-2, 2);
|
|
|
|
// The callback receives match, p1, ..., pn
|
|
|
|
return cb(matches);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static contains(s: string, substr: string): boolean { return s.indexOf(substr) != -1; }
|
2015-07-20 13:37:50 -07:00
|
|
|
|
2015-08-20 16:25:34 -07:00
|
|
|
static compare(a: string, b: string): number {
|
2015-07-20 13:37:50 -07:00
|
|
|
if (a < b) {
|
|
|
|
return -1;
|
|
|
|
} else if (a > b) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class StringJoiner {
|
2016-06-11 21:23:37 -07:00
|
|
|
constructor(public parts: string[] = []) {}
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-06-09 16:51:40 +02:00
|
|
|
add(part: string): void { this.parts.push(part); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
toString(): string { return this.parts.join(''); }
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
2015-09-10 15:25:36 -07:00
|
|
|
export class NumberParseError extends Error {
|
2015-04-01 10:45:56 -07:00
|
|
|
name: string;
|
|
|
|
|
2015-10-01 19:49:45 -07:00
|
|
|
constructor(public message: string) { super(); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-06-26 11:10:52 -07:00
|
|
|
toString(): string { return this.message; }
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class NumberWrapper {
|
2015-08-20 16:25:34 -07:00
|
|
|
static toFixed(n: number, fractionDigits: number): string { return n.toFixed(fractionDigits); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
static equal(a: number, b: number): boolean { return a === b; }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-08-20 16:25:34 -07:00
|
|
|
static parseIntAutoRadix(text: string): number {
|
|
|
|
var result: number = parseInt(text);
|
2015-04-01 10:45:56 -07:00
|
|
|
if (isNaN(result)) {
|
2016-06-08 16:38:52 -07:00
|
|
|
throw new NumberParseError('Invalid integer literal when parsing ' + text);
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:25:34 -07:00
|
|
|
static parseInt(text: string, radix: number): number {
|
2015-04-01 10:45:56 -07:00
|
|
|
if (radix == 10) {
|
|
|
|
if (/^(\-|\+)?[0-9]+$/.test(text)) {
|
|
|
|
return parseInt(text, radix);
|
|
|
|
}
|
|
|
|
} else if (radix == 16) {
|
|
|
|
if (/^(\-|\+)?[0-9ABCDEFabcdef]+$/.test(text)) {
|
|
|
|
return parseInt(text, radix);
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-20 16:25:34 -07:00
|
|
|
var result: number = parseInt(text, radix);
|
2015-04-01 10:45:56 -07:00
|
|
|
if (!isNaN(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 16:38:52 -07:00
|
|
|
throw new NumberParseError(
|
|
|
|
'Invalid integer literal when parsing ' + text + ' in base ' + radix);
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: NaN is a valid literal but is returned by parseFloat to indicate an error.
|
|
|
|
static parseFloat(text: string): number { return parseFloat(text); }
|
|
|
|
|
|
|
|
static get NaN(): number { return NaN; }
|
|
|
|
|
2016-06-10 00:32:36 +03:00
|
|
|
static isNumeric(value: any): boolean { return !isNaN(value - parseFloat(value)); }
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
static isNaN(value: any): boolean { return isNaN(value); }
|
2015-04-01 10:45:56 -07:00
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
static isInteger(value: any): boolean { return Number.isInteger(value); }
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export var RegExp = _global.RegExp;
|
|
|
|
|
|
|
|
export class RegExpWrapper {
|
2015-07-07 20:03:00 -07:00
|
|
|
static create(regExpStr: string, flags: string = ''): RegExp {
|
2015-04-01 10:45:56 -07:00
|
|
|
flags = flags.replace(/g/g, '');
|
|
|
|
return new _global.RegExp(regExpStr, flags + 'g');
|
|
|
|
}
|
2015-10-01 19:49:45 -07:00
|
|
|
static firstMatch(regExp: RegExp, input: string): RegExpExecArray {
|
2015-04-01 10:45:56 -07:00
|
|
|
// Reset multimatch regex state
|
|
|
|
regExp.lastIndex = 0;
|
|
|
|
return regExp.exec(input);
|
|
|
|
}
|
2015-07-28 18:38:35 -07:00
|
|
|
static test(regExp: RegExp, input: string): boolean {
|
|
|
|
regExp.lastIndex = 0;
|
|
|
|
return regExp.test(input);
|
|
|
|
}
|
2016-06-08 16:38:52 -07:00
|
|
|
static matcher(regExp: RegExp, input: string): {re: RegExp; input: string} {
|
2015-04-01 10:45:56 -07:00
|
|
|
// Reset regex state for the case
|
|
|
|
// someone did not loop over all matches
|
|
|
|
// last time.
|
|
|
|
regExp.lastIndex = 0;
|
|
|
|
return {re: regExp, input: input};
|
|
|
|
}
|
2016-03-23 13:41:18 -07:00
|
|
|
static replaceAll(regExp: RegExp, input: string, replace: Function): string {
|
|
|
|
let c = regExp.exec(input);
|
|
|
|
let res = '';
|
|
|
|
regExp.lastIndex = 0;
|
|
|
|
let prev = 0;
|
|
|
|
while (c) {
|
|
|
|
res += input.substring(prev, c.index);
|
|
|
|
res += replace(c);
|
|
|
|
prev = c.index + c[0].length;
|
|
|
|
regExp.lastIndex = prev;
|
|
|
|
c = regExp.exec(input);
|
|
|
|
}
|
|
|
|
res += input.substring(prev);
|
|
|
|
return res;
|
|
|
|
}
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class RegExpMatcherWrapper {
|
2016-06-08 16:38:52 -07:00
|
|
|
static next(matcher: {re: RegExp; input: string}): RegExpExecArray {
|
2015-07-07 20:03:00 -07:00
|
|
|
return matcher.re.exec(matcher.input);
|
|
|
|
}
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class FunctionWrapper {
|
2015-07-07 20:03:00 -07:00
|
|
|
static apply(fn: Function, posArgs: any): any { return fn.apply(null, posArgs); }
|
2016-05-13 13:22:29 -07:00
|
|
|
|
|
|
|
static bind(fn: Function, scope: any): Function { return fn.bind(scope); }
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// JS has NaN !== NaN
|
2016-06-11 21:23:37 -07:00
|
|
|
export function looseIdentical(a: any, b: any): boolean {
|
2016-06-08 16:38:52 -07:00
|
|
|
return a === b || typeof a === 'number' && typeof b === 'number' && isNaN(a) && isNaN(b);
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// JS considers NaN is the same as NaN for map Key (while NaN !== NaN otherwise)
|
|
|
|
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
|
2015-07-07 20:03:00 -07:00
|
|
|
export function getMapKey<T>(value: T): T {
|
2015-04-01 10:45:56 -07:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function normalizeBlank(obj: Object): any {
|
2015-04-01 10:45:56 -07:00
|
|
|
return isBlank(obj) ? null : obj;
|
|
|
|
}
|
|
|
|
|
2015-06-12 07:50:45 -07:00
|
|
|
export function normalizeBool(obj: boolean): boolean {
|
2015-05-27 10:14:37 -07:00
|
|
|
return isBlank(obj) ? false : obj;
|
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function isJsObject(o: any): boolean {
|
2016-06-08 16:38:52 -07:00
|
|
|
return o !== null && (typeof o === 'function' || typeof o === 'object');
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-07 20:03:00 -07:00
|
|
|
export function print(obj: Error | Object) {
|
2015-09-10 15:25:36 -07:00
|
|
|
console.log(obj);
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:52:24 -07:00
|
|
|
export function warn(obj: Error | Object) {
|
|
|
|
console.warn(obj);
|
|
|
|
}
|
|
|
|
|
2015-04-01 10:45:56 -07:00
|
|
|
// Can't be all uppercase as our transpiler would think it is a special directive...
|
2015-05-18 09:24:52 -07:00
|
|
|
export class Json {
|
2015-06-26 11:10:52 -07:00
|
|
|
static parse(s: string): Object { return _global.JSON.parse(s); }
|
2015-07-07 20:03:00 -07:00
|
|
|
static stringify(data: Object): string {
|
2015-05-18 09:24:52 -07:00
|
|
|
// Dart doesn't take 3 arguments
|
|
|
|
return _global.JSON.stringify(data, null, 2);
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 10:45:56 -07:00
|
|
|
|
|
|
|
export class DateWrapper {
|
2016-06-08 16:38:52 -07:00
|
|
|
static create(
|
|
|
|
year: number, month: number = 1, day: number = 1, hour: number = 0, minutes: number = 0,
|
|
|
|
seconds: number = 0, milliseconds: number = 0): Date {
|
2015-07-04 22:34:54 +04:30
|
|
|
return new Date(year, month - 1, day, hour, minutes, seconds, milliseconds);
|
|
|
|
}
|
2015-10-13 12:04:38 -07:00
|
|
|
static fromISOString(str: string): Date { return new Date(str); }
|
2015-08-20 16:25:34 -07:00
|
|
|
static fromMillis(ms: number): Date { return new Date(ms); }
|
|
|
|
static toMillis(date: Date): number { return date.getTime(); }
|
2015-06-26 11:10:52 -07:00
|
|
|
static now(): Date { return new Date(); }
|
2015-07-04 22:34:54 +04:30
|
|
|
static toJson(date: Date): string { return date.toJSON(); }
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
2015-08-28 10:36:58 -07:00
|
|
|
|
|
|
|
export function setValueOnPath(global: any, path: string, value: any) {
|
|
|
|
var parts = path.split('.');
|
|
|
|
var obj: any = global;
|
|
|
|
while (parts.length > 1) {
|
|
|
|
var name = parts.shift();
|
2015-11-26 19:38:14 +01:00
|
|
|
if (obj.hasOwnProperty(name) && isPresent(obj[name])) {
|
2015-08-28 10:36:58 -07:00
|
|
|
obj = obj[name];
|
|
|
|
} else {
|
|
|
|
obj = obj[name] = {};
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 15:21:02 +02:00
|
|
|
if (obj === undefined || obj === null) {
|
|
|
|
obj = {};
|
|
|
|
}
|
2015-08-28 10:36:58 -07:00
|
|
|
obj[parts.shift()] = value;
|
|
|
|
}
|
2015-09-17 00:35:59 +02:00
|
|
|
|
|
|
|
// When Symbol.iterator doesn't exist, retrieves the key used in es6-shim
|
2016-06-11 21:23:37 -07:00
|
|
|
declare var Symbol: any;
|
|
|
|
var _symbolIterator: any = null;
|
2016-06-08 16:38:52 -07:00
|
|
|
export function getSymbolIterator(): string|symbol {
|
2015-09-17 00:35:59 +02:00
|
|
|
if (isBlank(_symbolIterator)) {
|
2016-04-13 11:25:45 -07:00
|
|
|
if (isPresent((<any>globalScope).Symbol) && isPresent(Symbol.iterator)) {
|
2015-09-17 00:35:59 +02:00
|
|
|
_symbolIterator = Symbol.iterator;
|
|
|
|
} else {
|
|
|
|
// es6-shim specific logic
|
|
|
|
var keys = Object.getOwnPropertyNames(Map.prototype);
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
var key = keys[i];
|
|
|
|
if (key !== 'entries' && key !== 'size' &&
|
2016-06-11 21:23:37 -07:00
|
|
|
(Map as any).prototype[key] === Map.prototype['entries']) {
|
2015-09-17 00:35:59 +02:00
|
|
|
_symbolIterator = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _symbolIterator;
|
|
|
|
}
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
export function evalExpression(
|
|
|
|
sourceUrl: string, expr: string, declarations: string, vars: {[key: string]: any}): any {
|
2015-12-02 10:35:51 -08:00
|
|
|
var fnBody = `${declarations}\nreturn ${expr}\n//# sourceURL=${sourceUrl}`;
|
2016-06-11 21:23:37 -07:00
|
|
|
var fnArgNames: string[] = [];
|
|
|
|
var fnArgValues: any[] = [];
|
2015-12-02 10:35:51 -08:00
|
|
|
for (var argName in vars) {
|
|
|
|
fnArgNames.push(argName);
|
|
|
|
fnArgValues.push(vars[argName]);
|
|
|
|
}
|
|
|
|
return new Function(...fnArgNames.concat(fnBody))(...fnArgValues);
|
|
|
|
}
|
2016-01-05 13:42:21 -08:00
|
|
|
|
|
|
|
export function isPrimitive(obj: any): boolean {
|
|
|
|
return !isJsObject(obj);
|
2016-02-01 10:29:42 -08:00
|
|
|
}
|
2016-02-09 17:46:38 -08:00
|
|
|
|
|
|
|
export function hasConstructor(value: Object, type: Type): boolean {
|
|
|
|
return value.constructor === type;
|
2016-03-08 16:38:50 -08:00
|
|
|
}
|
2016-02-02 10:37:08 +02:00
|
|
|
|
2016-03-14 11:45:14 -07:00
|
|
|
export function escape(s: string): string {
|
|
|
|
return _global.encodeURI(s);
|
2016-03-17 20:58:25 -07:00
|
|
|
}
|
2016-06-20 09:52:41 -07:00
|
|
|
|
|
|
|
export function escapeRegExp(s: string): string {
|
|
|
|
return s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
|
|
|
|
}
|