-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathiterable.ts
55 lines (49 loc) · 1.54 KB
/
iterable.ts
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
/**
* @license
* Copyright Google LLC 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.dev/license
*/
export function isIterable(obj: any): obj is Iterable<any> {
return obj !== null && typeof obj === 'object' && obj[Symbol.iterator] !== undefined;
}
export function isListLikeIterable(obj: any): boolean {
if (!isJsObject(obj)) return false;
return (
Array.isArray(obj) ||
(!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
Symbol.iterator in obj)
); // JS Iterable have a Symbol.iterator prop
}
export function areIterablesEqual<T>(
a: Iterable<T>,
b: Iterable<T>,
comparator: (a: T, b: T) => boolean,
): boolean {
const iterator1 = a[Symbol.iterator]();
const iterator2 = b[Symbol.iterator]();
while (true) {
const item1 = iterator1.next();
const 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<T>(obj: Iterable<T>, fn: (p: T) => void) {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
fn(obj[i]);
}
} else {
const iterator = obj[Symbol.iterator]();
let item: IteratorResult<T, any>;
while (!(item = iterator.next()).done) {
fn(item.value);
}
}
}
export function isJsObject(o: any): boolean {
return o !== null && (typeof o === 'function' || typeof o === 'object');
}