deno.land / std@0.224.0 / collections / distinct_by_test.ts

distinct_by_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { distinctBy } from "./distinct_by.ts";
function distinctByTest<I>( array: Array<I>, selector: (element: I) => unknown, expected: Array<I>, message?: string,) { const actual = distinctBy(array, selector); assertEquals(actual, expected, message);}
Deno.test({ name: "distinctBy() handles identities on empty array", fn() { distinctByTest( [], () => {}, [], ); },});
Deno.test({ name: "distinctBy() gets head on noop selector", fn() { distinctByTest( [25, "asdf", true], () => {}, [25], ); },});
Deno.test({ name: "distinctBy() removes duplicates and preserves order on identity", fn() { distinctByTest( [true, "asdf", 4, "asdf", true], (it) => it, [true, "asdf", 4], ); distinctByTest( [null, undefined, null, "foo", undefined], (it) => it, [null, undefined, "foo"], ); },});
Deno.test({ name: "distinctBy() does not check for deep equality on identity", fn() { const objects = [{ foo: "bar" }, { foo: "bar" }]; distinctByTest( objects, (it) => it, objects, );
const arrays = [[], []]; distinctByTest( arrays, (it) => it, arrays, );
const nans = [NaN, NaN]; distinctByTest( nans, (it) => it, [nans[0]], );
const noops = [() => {}, () => {}]; distinctByTest( noops, (it) => it, noops, );
const sets = [new Set(), new Set()]; distinctByTest( sets, (it) => it, sets, ); },});
Deno.test({ name: "distinctBy() handles distincts by selected value and preserves order", fn() { const kim = { name: "Kim", age: 22 }; const arthur = { name: "Arthur", age: 22 }; const anna = { name: "Anna", age: 48 }; const karl = { name: "Karl", age: 48 }; const people = [kim, arthur, anna, karl];
distinctByTest( people, (it) => it.name.charAt(0), [kim, arthur], ); distinctByTest( people, (it) => it.age, [kim, anna], ); distinctByTest( people, (it) => it.name.length, [kim, arthur, anna], ); },});
std

Version Info

Tagged at
6 months ago