@dxos/util 0.5.3-main.d28cf09 → 0.5.3-main.d2f7366
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/src/array.test.ts +58 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/util",
|
|
3
|
-
"version": "0.5.3-main.
|
|
3
|
+
"version": "0.5.3-main.d2f7366",
|
|
4
4
|
"description": "Temporary bucket for misc functions, which should graduate into separate packages.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
"src"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@dxos/debug": "0.5.3-main.
|
|
20
|
-
"@dxos/invariant": "0.5.3-main.
|
|
21
|
-
"@dxos/
|
|
22
|
-
"@dxos/
|
|
19
|
+
"@dxos/debug": "0.5.3-main.d2f7366",
|
|
20
|
+
"@dxos/invariant": "0.5.3-main.d2f7366",
|
|
21
|
+
"@dxos/keys": "0.5.3-main.d2f7366",
|
|
22
|
+
"@dxos/node-std": "0.5.3-main.d2f7366"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@dxos/crypto": "0.5.3-main.
|
|
25
|
+
"@dxos/crypto": "0.5.3-main.d2f7366"
|
|
26
26
|
},
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
package/src/array.test.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { expect } from 'chai';
|
|
6
6
|
import { describe, test } from 'vitest';
|
|
7
7
|
|
|
8
|
-
import { diff, intersection } from './array';
|
|
8
|
+
import { diff, intersection, distinctBy } from './array';
|
|
9
9
|
|
|
10
10
|
describe('diff', () => {
|
|
11
11
|
test('returns the difference between two sets', () => {
|
|
@@ -40,3 +40,60 @@ describe('diff', () => {
|
|
|
40
40
|
).to.deep.eq([{ x: 2 }, { x: 3 }]);
|
|
41
41
|
});
|
|
42
42
|
});
|
|
43
|
+
|
|
44
|
+
describe('distinctBy', () => {
|
|
45
|
+
test('filters distinct elements by a given key', () => {
|
|
46
|
+
{
|
|
47
|
+
const array = [1, 2, 2, 3, 4, 4, 5];
|
|
48
|
+
const distinct = distinctBy(array, (a) => a);
|
|
49
|
+
expect(distinct).to.deep.eq([1, 2, 3, 4, 5]);
|
|
50
|
+
}
|
|
51
|
+
{
|
|
52
|
+
const array = [{ x: 1 }, { x: 2 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 4 }, { x: 5 }];
|
|
53
|
+
const distinct = distinctBy(array, (a) => a.x);
|
|
54
|
+
expect(distinct).to.deep.eq([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }]);
|
|
55
|
+
}
|
|
56
|
+
{
|
|
57
|
+
const array = ['apple', 'banana', 'apple', 'orange', 'banana'];
|
|
58
|
+
const distinct = distinctBy(array, (a) => a);
|
|
59
|
+
expect(distinct).to.deep.eq(['apple', 'banana', 'orange']);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('handles empty arrays', () => {
|
|
64
|
+
{
|
|
65
|
+
const array: number[] = [];
|
|
66
|
+
const distinct = distinctBy(array, (a) => a);
|
|
67
|
+
expect(distinct).to.deep.eq([]);
|
|
68
|
+
}
|
|
69
|
+
{
|
|
70
|
+
const array: { x: number }[] = [];
|
|
71
|
+
const distinct = distinctBy(array, (a) => a.x);
|
|
72
|
+
expect(distinct).to.deep.eq([]);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('works with complex objects', () => {
|
|
77
|
+
{
|
|
78
|
+
const array = [{ x: { y: 1 } }, { x: { y: 2 } }, { x: { y: 1 } }];
|
|
79
|
+
const distinct = distinctBy(array, (a) => a.x.y);
|
|
80
|
+
expect(distinct).to.deep.eq([{ x: { y: 1 } }, { x: { y: 2 } }]);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('case sensitivity', () => {
|
|
85
|
+
{
|
|
86
|
+
const array = ['apple', 'Apple', 'APPLE'];
|
|
87
|
+
const distinct = distinctBy(array, (a) => a.toLowerCase());
|
|
88
|
+
expect(distinct).to.deep.eq(['apple']);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('null and undefined values', () => {
|
|
93
|
+
{
|
|
94
|
+
const array = [1, 2, null, 2, null, undefined, 3];
|
|
95
|
+
const distinct = distinctBy(array, (a) => a);
|
|
96
|
+
expect(distinct).to.deep.eq([1, 2, null, undefined, 3]);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|