154 lines
3.7 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
import {getSymbolIterator, isJsObject, isPresent} from './lang';
/**
* Wraps Javascript Objects
*/
export class StringMapWrapper {
static merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} {
var m: {[key: string]: V} = {};
2016-07-15 16:26:19 -07:00
for (let k of Object.keys(m1)) {
m[k] = m1[k];
}
2016-07-15 16:26:19 -07:00
for (let k of Object.keys(m2)) {
m[k] = m2[k];
}
return m;
}
static equals<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): boolean {
const k1 = Object.keys(m1);
const k2 = Object.keys(m2);
if (k1.length != k2.length) {
return false;
}
for (let i = 0; i < k1.length; i++) {
const key = k1[i];
if (m1[key] !== m2[key]) {
return false;
}
}
return true;
}
}
/**
* A boolean-valued function over a value, possibly including context information
* regarding that value's position in an array.
*/
export interface Predicate<T> { (value: T, index?: number, array?: T[]): boolean; }
export class ListWrapper {
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]);
list.splice(index, 1);
}
}
2016-10-21 15:14:44 -07:00
static remove<T>(list: T[], el: T): boolean {
2016-10-21 15:14:44 -07:00
const index = list.indexOf(el);
if (index > -1) {
list.splice(index, 1);
return true;
}
return false;
}
2016-10-21 15:14:44 -07:00
static equals(a: any[], b: any[]): boolean {
if (a.length != b.length) return false;
2016-10-21 15:14:44 -07:00
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
static maximum<T>(list: T[], predicate: (t: T) => number): T {
if (list.length == 0) {
return null;
}
var solution: any /** TODO #???? */ = null;
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) {
continue;
}
var candidateValue = predicate(candidate);
if (candidateValue > maxValue) {
solution = candidate;
maxValue = candidateValue;
}
}
return solution;
}
static flatten<T>(list: Array<T|T[]>): T[] {
var target: any[] = [];
_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)) {
_flattenArray(item, target);
} else {
target.push(item);
}
}
2016-03-23 13:42:19 -07:00
}
return target;
}
export function isListLikeIterable(obj: any): boolean {
if (!isJsObject(obj)) return false;
2016-10-19 13:42:39 -07:00
return Array.isArray(obj) ||
(!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop
}
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()]();
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;
}
}
export function iterateListLike(obj: any, fn: Function) {
2016-10-19 13:42:39 -07:00
if (Array.isArray(obj)) {
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;
while (!((item = iterator.next()).done)) {
fn(item.value);
}
}
}