collection.js
4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @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 } from './lang';
/**
* Wraps Javascript Objects
*/
export var StringMapWrapper = (function () {
function StringMapWrapper() {
}
/**
* @param {?} m1
* @param {?} m2
* @return {?}
*/
StringMapWrapper.merge = function (m1, m2) {
var /** @type {?} */ m = {};
for (var _i = 0, _a = Object.keys(m1); _i < _a.length; _i++) {
var k = _a[_i];
m[k] = m1[k];
}
for (var _b = 0, _c = Object.keys(m2); _b < _c.length; _b++) {
var k = _c[_b];
m[k] = m2[k];
}
return m;
};
/**
* @param {?} m1
* @param {?} m2
* @return {?}
*/
StringMapWrapper.equals = function (m1, m2) {
var /** @type {?} */ k1 = Object.keys(m1);
var /** @type {?} */ k2 = Object.keys(m2);
if (k1.length != k2.length) {
return false;
}
for (var /** @type {?} */ i = 0; i < k1.length; i++) {
var /** @type {?} */ key = k1[i];
if (m1[key] !== m2[key]) {
return false;
}
}
return true;
};
return StringMapWrapper;
}());
export var ListWrapper = (function () {
function ListWrapper() {
}
/**
* @param {?} arr
* @param {?} condition
* @return {?}
*/
ListWrapper.findLast = function (arr, condition) {
for (var /** @type {?} */ i = arr.length - 1; i >= 0; i--) {
if (condition(arr[i])) {
return arr[i];
}
}
return null;
};
/**
* @param {?} list
* @param {?} items
* @return {?}
*/
ListWrapper.removeAll = function (list, items) {
for (var /** @type {?} */ i = 0; i < items.length; ++i) {
var /** @type {?} */ index = list.indexOf(items[i]);
if (index > -1) {
list.splice(index, 1);
}
}
};
/**
* @param {?} list
* @param {?} el
* @return {?}
*/
ListWrapper.remove = function (list, el) {
var /** @type {?} */ index = list.indexOf(el);
if (index > -1) {
list.splice(index, 1);
return true;
}
return false;
};
/**
* @param {?} a
* @param {?} b
* @return {?}
*/
ListWrapper.equals = function (a, b) {
if (a.length != b.length)
return false;
for (var /** @type {?} */ i = 0; i < a.length; ++i) {
if (a[i] !== b[i])
return false;
}
return true;
};
/**
* @param {?} list
* @return {?}
*/
ListWrapper.flatten = function (list) {
return list.reduce(function (flat, item) {
var /** @type {?} */ flatItem = Array.isArray(item) ? ListWrapper.flatten(item) : item;
return ((flat)).concat(flatItem);
}, []);
};
return ListWrapper;
}());
/**
* @param {?} obj
* @return {?}
*/
export function isListLikeIterable(obj) {
if (!isJsObject(obj))
return false;
return Array.isArray(obj) ||
(!(obj instanceof Map) &&
getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop
}
/**
* @param {?} a
* @param {?} b
* @param {?} comparator
* @return {?}
*/
export function areIterablesEqual(a, b, comparator) {
var /** @type {?} */ iterator1 = a[getSymbolIterator()]();
var /** @type {?} */ iterator2 = b[getSymbolIterator()]();
while (true) {
var /** @type {?} */ item1 = iterator1.next();
var /** @type {?} */ 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;
}
}
/**
* @param {?} obj
* @param {?} fn
* @return {?}
*/
export function iterateListLike(obj, fn) {
if (Array.isArray(obj)) {
for (var /** @type {?} */ i = 0; i < obj.length; i++) {
fn(obj[i]);
}
}
else {
var /** @type {?} */ iterator = obj[getSymbolIterator()]();
var /** @type {?} */ item = void 0;
while (!((item = iterator.next()).done)) {
fn(item.value);
}
}
}
//# sourceMappingURL=collection.js.map