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-11-03 16:58:27 -07:00
|
|
|
import {getSymbolIterator, isJsObject, isPresent} from './lang';
|
2015-05-01 16:15:48 -07:00
|
|
|
|
2015-04-01 10:45:56 -07:00
|
|
|
/**
|
|
|
|
* Wraps Javascript Objects
|
|
|
|
*/
|
|
|
|
export class StringMapWrapper {
|
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 {
|
2016-09-09 12:03:51 -07:00
|
|
|
const k1 = Object.keys(m1);
|
|
|
|
const k2 = Object.keys(m2);
|
|
|
|
|
2015-05-11 09:08:37 -07:00
|
|
|
if (k1.length != k2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-09 12:03:51 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < k1.length; i++) {
|
|
|
|
const key = k1[i];
|
2015-05-11 09:08:37 -07:00
|
|
|
if (m1[key] !== m2[key]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 12:03:51 -07:00
|
|
|
|
2015-05-11 09:08:37 -07:00
|
|
|
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-08-28 11:29:19 -07:00
|
|
|
static removeAll<T>(list: T[], items: T[]) {
|
2016-10-21 15:14:44 -07:00
|
|
|
for (let i = 0; i < items.length; ++i) {
|
|
|
|
const index = list.indexOf(items[i]);
|
2015-04-01 10:45:56 -07:00
|
|
|
list.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 15:14:44 -07:00
|
|
|
|
2015-08-28 11:29:19 -07:00
|
|
|
static remove<T>(list: T[], el: T): boolean {
|
2016-10-21 15:14:44 -07:00
|
|
|
const index = list.indexOf(el);
|
2015-04-01 10:45:56 -07:00
|
|
|
if (index > -1) {
|
|
|
|
list.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-21 15:14:44 -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;
|
2016-10-21 15:14:44 -07:00
|
|
|
for (let i = 0; i < a.length; ++i) {
|
2015-04-01 10:45:56 -07:00
|
|
|
if (a[i] !== b[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
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];
|
2016-10-21 15:14:44 -07:00
|
|
|
if (candidate == null) {
|
2015-08-13 11:19:37 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _flattenArray(source: any[], target: any[]): any[] {
|
|
|
|
if (isPresent(source)) {
|
2016-10-19 13:42:39 -07:00
|
|
|
for (let i = 0; i < source.length; i++) {
|
|
|
|
const item = source[i];
|
|
|
|
if (Array.isArray(item)) {
|
2016-01-06 14:13:44 -08:00
|
|
|
_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;
|
2016-10-19 13:42:39 -07:00
|
|
|
return Array.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 {
|
2016-10-19 13:42:39 -07:00
|
|
|
const iterator1 = a[getSymbolIterator()]();
|
|
|
|
const iterator2 = b[getSymbolIterator()]();
|
2016-01-05 13:42:21 -08:00
|
|
|
|
|
|
|
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) {
|
2016-10-19 13:42:39 -07:00
|
|
|
if (Array.isArray(obj)) {
|
2015-04-01 10:45:56 -07:00
|
|
|
for (var i = 0; i < obj.length; i++) {
|
|
|
|
fn(obj[i]);
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-19 13:42:39 -07:00
|
|
|
const iterator = obj[getSymbolIterator()]();
|
|
|
|
let item: any;
|
2015-04-01 10:45:56 -07:00
|
|
|
while (!((item = iterator.next()).done)) {
|
|
|
|
fn(item.value);
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 16:46:05 -07:00
|
|
|
}
|