-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uniq_test.ts
33 lines (31 loc) · 1.03 KB
/
uniq_test.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
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { uniq } from "./uniq.ts";
await test("uniq default", () => {
const result = uniq([1, 2, 2, 3, 3, 3]);
const expected = [1, 2, 3];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
await test("uniq with identify", () => {
const values: number[] = [];
const indices: number[] = [];
const identities: number[] = [];
const result = uniq(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
(v, index) => {
values.push(v);
indices.push(index);
const id = v % 4;
identities.push(id);
return id;
},
);
const expected = [1, 2, 3, 4];
assertEquals(Array.from(result), expected);
assertEquals(values, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
assertEquals(indices, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
assertEquals(identities, [1, 2, 3, 0, 1, 2, 3, 0, 1]);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});