| 
									
										
										
										
											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-06-08 16:38:52 -07:00
										 |  |  | import {getSymbolIterator, global, isArray, isBlank, isJsObject, isPresent} from './lang'; | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export var Map = global.Map; | 
					
						
							|  |  |  | export var Set = global.Set; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-01 16:15:48 -07:00
										 |  |  | // Safari and Internet Explorer do not support the iterable parameter to the
 | 
					
						
							|  |  |  | // Map constructor.  We work around that by manually adding the items.
 | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  | var createMapFromPairs: {(pairs: any[]): Map<any, any>} = (function() { | 
					
						
							| 
									
										
										
										
											2015-05-01 16:15:48 -07:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |     if (new Map(<any>[[1, 2]]).size === 1) { | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |       return function createMapFromPairs(pairs: any[]): Map<any, any> { return new Map(pairs); }; | 
					
						
							| 
									
										
										
										
											2015-05-01 16:15:48 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   return function createMapAndPopulateFromPairs(pairs: any[]): Map<any, any> { | 
					
						
							| 
									
										
										
										
											2015-05-01 16:15:48 -07:00
										 |  |  |     var map = new Map(); | 
					
						
							|  |  |  |     for (var i = 0; i < pairs.length; i++) { | 
					
						
							|  |  |  |       var pair = pairs[i]; | 
					
						
							|  |  |  |       map.set(pair[0], pair[1]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return map; | 
					
						
							| 
									
										
										
										
											2015-07-15 12:12:23 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2015-05-01 16:15:48 -07:00
										 |  |  | })(); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:42:24 +02:00
										 |  |  | var createMapFromMap: {(m: Map<any, any>): Map<any, any>} = (function() { | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |     if (new Map(<any>new Map())) { | 
					
						
							|  |  |  |       return function createMapFromMap(m: Map<any, any>): Map<any, any> { return new Map(<any>m); }; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:42:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return function createMapAndPopulateFromMap(m: Map<any, any>): Map<any, any> { | 
					
						
							|  |  |  |     var map = new Map(); | 
					
						
							|  |  |  |     m.forEach((v, k) => { map.set(k, v); }); | 
					
						
							|  |  |  |     return map; | 
					
						
							| 
									
										
										
										
											2015-07-15 12:12:23 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:42:24 +02:00
										 |  |  | })(); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  | var _clearValues: {(m: Map<any, any>): void} = (function() { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:43:48 +02:00
										 |  |  |   if ((<any>(new Map()).keys()).next) { | 
					
						
							|  |  |  |     return function _clearValues(m: Map<any, any>) { | 
					
						
							|  |  |  |       var keyIterator = m.keys(); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |       var k: any /** TODO #???? */; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:43:48 +02:00
										 |  |  |       while (!((k = (<any>keyIterator).next()).done)) { | 
					
						
							|  |  |  |         m.set(k.value, null); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return function _clearValuesWithForeEach(m: Map<any, any>) { | 
					
						
							|  |  |  |       m.forEach((v, k) => { m.set(k, null); }); | 
					
						
							| 
									
										
										
										
											2015-07-15 12:12:23 -07:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-05-22 12:43:48 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | })(); | 
					
						
							| 
									
										
										
										
											2015-07-30 15:20:08 +02:00
										 |  |  | // Safari doesn't implement MapIterator.next(), which is used is Traceur's polyfill of Array.from
 | 
					
						
							|  |  |  | // TODO(mlaval): remove the work around once we have a working polyfill of Array.from
 | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  | var _arrayFromMap: {(m: Map<any, any>, getValues: boolean): any[]} = (function() { | 
					
						
							| 
									
										
										
										
											2015-07-30 15:20:08 +02:00
										 |  |  |   try { | 
					
						
							|  |  |  |     if ((<any>(new Map()).values()).next) { | 
					
						
							| 
									
										
										
										
											2015-10-28 08:59:19 +01:00
										 |  |  |       return function createArrayFromMap(m: Map<any, any>, getValues: boolean): any[] { | 
					
						
							|  |  |  |         return getValues ? (<any>Array).from(m.values()) : (<any>Array).from(m.keys()); | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2015-07-30 15:20:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   return function createArrayFromMapWithForeach(m: Map<any, any>, getValues: boolean): any[] { | 
					
						
							| 
									
										
										
										
											2015-07-30 15:20:08 +02:00
										 |  |  |     var res = ListWrapper.createFixedSize(m.size), i = 0; | 
					
						
							|  |  |  |     m.forEach((v, k) => { | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |       res[i] = getValues ? v : k; | 
					
						
							| 
									
										
										
										
											2015-07-30 15:20:08 +02:00
										 |  |  |       i++; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | })(); | 
					
						
							| 
									
										
										
										
											2015-05-01 16:15:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | export class MapWrapper { | 
					
						
							| 
									
										
										
										
											2015-05-22 12:42:24 +02:00
										 |  |  |   static clone<K, V>(m: Map<K, V>): Map<K, V> { return createMapFromMap(m); } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static createFromStringMap<T>(stringMap: {[key: string]: T}): Map<string, T> { | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |     var result = new Map<string, T>(); | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     for (var prop in stringMap) { | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |       result.set(prop, stringMap[prop]); | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static toStringMap<T>(m: Map<string, T>): {[key: string]: T} { | 
					
						
							| 
									
										
										
										
											2015-10-02 17:33:21 -07:00
										 |  |  |     var r: {[key: string]: T} = {}; | 
					
						
							| 
									
										
										
										
											2015-07-23 18:01:34 -07:00
										 |  |  |     m.forEach((v, k) => r[k] = v); | 
					
						
							|  |  |  |     return r; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static createFromPairs(pairs: any[]): Map<any, any> { return createMapFromPairs(pairs); } | 
					
						
							| 
									
										
										
										
											2015-05-22 12:43:48 +02:00
										 |  |  |   static clearValues(m: Map<any, any>) { _clearValues(m); } | 
					
						
							| 
									
										
										
										
											2015-06-26 11:10:52 -07:00
										 |  |  |   static iterable<T>(m: T): T { return m; } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static keys<K>(m: Map<K, any>): K[] { return _arrayFromMap(m, false); } | 
					
						
							|  |  |  |   static values<V>(m: Map<any, V>): V[] { return _arrayFromMap(m, true); } | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Wraps Javascript Objects | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class StringMapWrapper { | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static create(): {[k: /*any*/ string]: any} { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     // Note: We are not using Object.create(null) here due to
 | 
					
						
							|  |  |  |     // performance!
 | 
					
						
							|  |  |  |     // http://jsperf.com/ng2-object-create-null
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static contains(map: {[key: string]: any}, key: string): boolean { | 
					
						
							| 
									
										
										
										
											2015-06-26 11:10:52 -07:00
										 |  |  |     return map.hasOwnProperty(key); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static get<V>(map: {[key: string]: V}, key: string): V { | 
					
						
							| 
									
										
										
										
											2015-05-20 09:48:15 -07:00
										 |  |  |     return map.hasOwnProperty(key) ? map[key] : undefined; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static set<V>(map: {[key: string]: V}, key: string, value: V) { map[key] = value; } | 
					
						
							| 
									
										
										
										
											2016-07-15 16:26:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static keys(map: {[key: string]: any}): string[] { return Object.keys(map); } | 
					
						
							| 
									
										
										
										
											2016-03-06 20:20:55 -08:00
										 |  |  |   static values<T>(map: {[key: string]: T}): T[] { | 
					
						
							| 
									
										
										
										
											2016-07-15 16:26:19 -07:00
										 |  |  |     return Object.keys(map).map((k: string): T => map[k]); | 
					
						
							| 
									
										
										
										
											2016-03-06 20:20:55 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static isEmpty(map: {[key: string]: any}): boolean { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     for (var prop in map) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static delete (map: {[key: string]: any}, key: string) { delete map[key]; } | 
					
						
							| 
									
										
										
										
											2016-07-15 16:26:19 -07:00
										 |  |  |   static forEach<K, V>(map: {[key: string]: V}, callback: (v: V, K: string) => void) { | 
					
						
							|  |  |  |     for (let k of Object.keys(map)) { | 
					
						
							|  |  |  |       callback(map[k], k); | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} { | 
					
						
							| 
									
										
										
										
											2015-10-02 17:33:21 -07:00
										 |  |  |     var m: {[key: string]: V} = {}; | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 16:26:19 -07:00
										 |  |  |     for (let k of Object.keys(m1)) { | 
					
						
							|  |  |  |       m[k] = m1[k]; | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 16:26:19 -07:00
										 |  |  |     for (let k of Object.keys(m2)) { | 
					
						
							|  |  |  |       m[k] = m2[k]; | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-05-11 09:08:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-02 16:47:54 -07:00
										 |  |  |   static equals<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): boolean { | 
					
						
							| 
									
										
										
										
											2015-05-11 09:08:37 -07:00
										 |  |  |     var k1 = Object.keys(m1); | 
					
						
							|  |  |  |     var k2 = Object.keys(m2); | 
					
						
							|  |  |  |     if (k1.length != k2.length) { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |     var key: any /** TODO #???? */; | 
					
						
							| 
									
										
										
										
											2015-05-11 09:08:37 -07:00
										 |  |  |     for (var i = 0; i < k1.length; i++) { | 
					
						
							|  |  |  |       key = k1[i]; | 
					
						
							|  |  |  |       if (m1[key] !== m2[key]) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A boolean-valued function over a value, possibly including context information | 
					
						
							|  |  |  |  * regarding that value's position in an array. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-06-26 11:10:52 -07:00
										 |  |  | export interface Predicate<T> { (value: T, index?: number, array?: T[]): boolean; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | export class ListWrapper { | 
					
						
							| 
									
										
										
										
											2015-09-15 09:38:20 -07:00
										 |  |  |   // JS has no way to express a statically fixed size list, but dart does so we
 | 
					
						
							| 
									
										
										
										
											2015-06-22 17:52:34 -07:00
										 |  |  |   // keep both methods.
 | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static createFixedSize(size: number): any[] { return new Array(size); } | 
					
						
							|  |  |  |   static createGrowableSize(size: number): any[] { return new Array(size); } | 
					
						
							|  |  |  |   static clone<T>(array: T[]): T[] { return array.slice(0); } | 
					
						
							| 
									
										
										
										
											2015-10-08 21:38:44 -07:00
										 |  |  |   static forEachWithIndex<T>(array: T[], fn: (t: T, n: number) => void) { | 
					
						
							| 
									
										
										
										
											2015-08-19 11:26:45 -07:00
										 |  |  |     for (var i = 0; i < array.length; i++) { | 
					
						
							|  |  |  |       fn(array[i], i); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static first<T>(array: T[]): T { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     if (!array) return null; | 
					
						
							|  |  |  |     return array[0]; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static last<T>(array: T[]): T { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     if (!array || array.length == 0) return null; | 
					
						
							|  |  |  |     return array[array.length - 1]; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static indexOf<T>(array: T[], value: T, startIndex: number = 0): number { | 
					
						
							| 
									
										
										
										
											2015-04-18 11:18:03 -07:00
										 |  |  |     return array.indexOf(value, startIndex); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static contains<T>(list: T[], el: T): boolean { return list.indexOf(el) !== -1; } | 
					
						
							|  |  |  |   static reversed<T>(array: T[]): T[] { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     var a = ListWrapper.clone(array); | 
					
						
							|  |  |  |     return a.reverse(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static concat(a: any[], b: any[]): any[] { return a.concat(b); } | 
					
						
							|  |  |  |   static insert<T>(list: T[], index: number, value: T) { list.splice(index, 0, value); } | 
					
						
							|  |  |  |   static removeAt<T>(list: T[], index: number): T { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     var res = list[index]; | 
					
						
							|  |  |  |     list.splice(index, 1); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static removeAll<T>(list: T[], items: T[]) { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     for (var i = 0; i < items.length; ++i) { | 
					
						
							|  |  |  |       var index = list.indexOf(items[i]); | 
					
						
							|  |  |  |       list.splice(index, 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static remove<T>(list: T[], el: T): boolean { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     var index = list.indexOf(el); | 
					
						
							|  |  |  |     if (index > -1) { | 
					
						
							|  |  |  |       list.splice(index, 1); | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-01 20:14:54 -07:00
										 |  |  |   static clear(list: any[]) { list.length = 0; } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static isEmpty(list: any[]): boolean { return list.length == 0; } | 
					
						
							|  |  |  |   static fill(list: any[], value: any, start: number = 0, end: number = null) { | 
					
						
							| 
									
										
										
										
											2015-08-16 01:30:18 +02:00
										 |  |  |     list.fill(value, start, end === null ? list.length : end); | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static equals(a: any[], b: any[]): boolean { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     if (a.length != b.length) return false; | 
					
						
							|  |  |  |     for (var i = 0; i < a.length; ++i) { | 
					
						
							|  |  |  |       if (a[i] !== b[i]) return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static slice<T>(l: T[], from: number = 0, to: number = null): T[] { | 
					
						
							| 
									
										
										
										
											2015-04-17 10:42:16 +02:00
										 |  |  |     return l.slice(from, to === null ? undefined : to); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static splice<T>(l: T[], from: number, length: number): T[] { return l.splice(from, length); } | 
					
						
							|  |  |  |   static sort<T>(l: T[], compareFn?: (a: T, b: T) => number) { | 
					
						
							| 
									
										
										
										
											2015-05-22 17:47:13 +02:00
										 |  |  |     if (isPresent(compareFn)) { | 
					
						
							|  |  |  |       l.sort(compareFn); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       l.sort(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static toString<T>(l: T[]): string { return l.toString(); } | 
					
						
							|  |  |  |   static toJSON<T>(l: T[]): string { return JSON.stringify(l); } | 
					
						
							| 
									
										
										
										
											2015-08-13 11:19:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 19:49:45 -07:00
										 |  |  |   static maximum<T>(list: T[], predicate: (t: T) => number): T { | 
					
						
							| 
									
										
										
										
											2015-08-13 11:19:37 -07:00
										 |  |  |     if (list.length == 0) { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |     var solution: any /** TODO #???? */ = null; | 
					
						
							| 
									
										
										
										
											2015-08-13 11:19:37 -07:00
										 |  |  |     var maxValue = -Infinity; | 
					
						
							|  |  |  |     for (var index = 0; index < list.length; index++) { | 
					
						
							|  |  |  |       var candidate = list[index]; | 
					
						
							|  |  |  |       if (isBlank(candidate)) { | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       var candidateValue = predicate(candidate); | 
					
						
							|  |  |  |       if (candidateValue > maxValue) { | 
					
						
							|  |  |  |         solution = candidate; | 
					
						
							|  |  |  |         maxValue = candidateValue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return solution; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-02-17 09:17:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   static flatten<T>(list: Array<T|T[]>): T[] { | 
					
						
							| 
									
										
										
										
											2016-06-22 13:13:31 -07:00
										 |  |  |     var target: any[] = []; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     _flattenArray(list, target); | 
					
						
							|  |  |  |     return target; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static addAll<T>(list: Array<T>, source: Array<T>): void { | 
					
						
							|  |  |  |     for (var i = 0; i < source.length; i++) { | 
					
						
							|  |  |  |       list.push(source[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-23 13:42:19 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   return target; | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  | export function isListLikeIterable(obj: any): boolean { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |   if (!isJsObject(obj)) return false; | 
					
						
							| 
									
										
										
										
											2015-06-11 19:32:55 +02:00
										 |  |  |   return isArray(obj) || | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       (!(obj instanceof Map) &&      // JS Map are iterables but return entries as [k, v]
 | 
					
						
							|  |  |  |        getSymbolIterator() in obj);  // JS Iterable have a Symbol.iterator prop
 | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-05 13:42:21 -08:00
										 |  |  | export function areIterablesEqual(a: any, b: any, comparator: Function): boolean { | 
					
						
							|  |  |  |   var iterator1 = a[getSymbolIterator()](); | 
					
						
							|  |  |  |   var iterator2 = b[getSymbolIterator()](); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (true) { | 
					
						
							|  |  |  |     let item1 = iterator1.next(); | 
					
						
							|  |  |  |     let item2 = iterator2.next(); | 
					
						
							|  |  |  |     if (item1.done && item2.done) return true; | 
					
						
							|  |  |  |     if (item1.done || item2.done) return false; | 
					
						
							|  |  |  |     if (!comparator(item1.value, item2.value)) return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  | export function iterateListLike(obj: any, fn: Function) { | 
					
						
							| 
									
										
										
										
											2015-06-11 19:32:55 +02:00
										 |  |  |   if (isArray(obj)) { | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     for (var i = 0; i < obj.length; i++) { | 
					
						
							|  |  |  |       fn(obj[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2015-09-17 00:35:59 +02:00
										 |  |  |     var iterator = obj[getSymbolIterator()](); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |     var item: any /** TODO #???? */; | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |     while (!((item = iterator.next()).done)) { | 
					
						
							|  |  |  |       fn(item.value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-21 18:18:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Safari and Internet Explorer do not support the iterable parameter to the
 | 
					
						
							|  |  |  | // Set constructor.  We work around that by manually adding the items.
 | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  | var createSetFromList: {(lst: any[]): Set<any>} = (function() { | 
					
						
							| 
									
										
										
										
											2015-05-21 18:18:19 +02:00
										 |  |  |   var test = new Set([1, 2, 3]); | 
					
						
							|  |  |  |   if (test.size === 3) { | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |     return function createSetFromList(lst: any[]): Set<any> { return new Set(lst); }; | 
					
						
							| 
									
										
										
										
											2015-05-21 18:18:19 +02:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |     return function createSetAndPopulateFromList(lst: any[]): Set<any> { | 
					
						
							| 
									
										
										
										
											2015-05-21 18:18:19 +02:00
										 |  |  |       var res = new Set(lst); | 
					
						
							|  |  |  |       if (res.size !== lst.length) { | 
					
						
							|  |  |  |         for (var i = 0; i < lst.length; i++) { | 
					
						
							|  |  |  |           res.add(lst[i]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return res; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | })(); | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | export class SetWrapper { | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   static createFromList<T>(lst: T[]): Set<T> { return createSetFromList(lst); } | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  |   static has<T>(s: Set<T>, key: T): boolean { return s.has(key); } | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |   static delete<K>(m: Set<K>, k: K) { m.delete(k); } | 
					
						
							| 
									
										
										
										
											2015-04-01 10:45:56 -07:00
										 |  |  | } |