-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequenceEqual.ts
30 lines (30 loc) · 932 Bytes
/
sequenceEqual.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
import { deferP0 } from "ts-functional-pipe";
export function _sequenceEqual<T>(
src: Iterable<T>,
seq: Iterable<T>,
equalityComparer?: (a: T | undefined, b: T | undefined) => boolean
): boolean {
const eq = equalityComparer
? (a: T | undefined, b: T | undefined) =>
typeof a !== "undefined" &&
typeof b !== "undefined" &&
equalityComparer(a, b)
: (a: T | undefined, b: T | undefined) =>
typeof a !== "undefined" && typeof b !== "undefined" && a === b;
const it1 = src[Symbol.iterator]();
const it2 = seq[Symbol.iterator]();
for (;;) {
const it1Result = it1.next();
const it2Result = it2.next();
if (it1Result.done && it2Result.done) {
return true;
}
if (it1Result.done || it2Result.done) {
return false;
}
if (!eq(it1Result.value, it2Result.value)) {
return false;
}
}
}
export const sequenceEqual = deferP0(_sequenceEqual);