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-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};
|
|
|
|
|
|
2016-06-22 14:06:23 -07:00
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
export function getTypeNameForDebugging(type: any): string {
|
2016-09-18 15:39:26 -07:00
|
|
|
return type['name'] || typeof type;
|
2015-07-13 14:22:43 -07:00
|
|
|
}
|
|
|
|
|
|
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-08-30 18:07:40 -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 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
|
|
|
|
2016-09-18 15:39:26 -07:00
|
|
|
const res = token.toString();
|
|
|
|
|
const newLineIndex = res.indexOf('\n');
|
|
|
|
|
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
2016-03-08 16:38:50 -08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-08-25 00:50:16 -07:00
|
|
|
throw new Error('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-08-25 00:50:16 -07:00
|
|
|
throw new Error('Invalid integer literal when parsing ' + text + ' in base ' + radix);
|
2015-04-01 10:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 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
|
|
|
|
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
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
export function hasConstructor(value: Object, type: any): boolean {
|
2016-02-09 17:46:38 -08:00
|
|
|
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');
|
|
|
|
|
}
|