@dxos/util 0.5.3-main.bbd33a9 → 0.5.3-main.bfb5bca
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/dist/lib/browser/index.mjs +12 -0
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +13 -0
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/array.d.ts +10 -0
- package/dist/types/src/array.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/array.test.ts +58 -1
- package/src/array.ts +23 -0
|
@@ -12,4 +12,14 @@ export type DiffResult<A, B = A> = {
|
|
|
12
12
|
*/
|
|
13
13
|
export declare const diff: <A, B = A>(previous: readonly A[], next: readonly B[], comparator: Comparator<A, B>) => DiffResult<A, B>;
|
|
14
14
|
export declare const intersection: <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>) => A[];
|
|
15
|
+
/**
|
|
16
|
+
* Returns a new array with only the first instance of each unique item
|
|
17
|
+
* based on a specified property.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T - The type of items in the input array.
|
|
20
|
+
* @param array - The array to filter for distinct items.
|
|
21
|
+
* @param key - The property key to determine uniqueness for each item.
|
|
22
|
+
* @returns A new array with only distinct items based on the specified property.
|
|
23
|
+
*/
|
|
24
|
+
export declare const distinctBy: <T, K>(array: T[], selector: (item: T) => K) => T[];
|
|
15
25
|
//# sourceMappingURL=array.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../../src/array.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAE3D,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG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E,CAAC"}
|
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.bfb5bca",
|
|
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/
|
|
21
|
-
"@dxos/
|
|
22
|
-
"@dxos/
|
|
19
|
+
"@dxos/debug": "0.5.3-main.bfb5bca",
|
|
20
|
+
"@dxos/keys": "0.5.3-main.bfb5bca",
|
|
21
|
+
"@dxos/invariant": "0.5.3-main.bfb5bca",
|
|
22
|
+
"@dxos/node-std": "0.5.3-main.bfb5bca"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@dxos/crypto": "0.5.3-main.
|
|
25
|
+
"@dxos/crypto": "0.5.3-main.bfb5bca"
|
|
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
|
+
});
|
package/src/array.ts
CHANGED
|
@@ -46,3 +46,26 @@ export const diff = <A, B = A>(
|
|
|
46
46
|
// TODO(burdon): Factor out.
|
|
47
47
|
export const intersection = <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>): A[] =>
|
|
48
48
|
a.filter((a) => b.find((b) => comparator(a, b)) !== undefined);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns a new array with only the first instance of each unique item
|
|
52
|
+
* based on a specified property.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam T - The type of items in the input array.
|
|
55
|
+
* @param array - The array to filter for distinct items.
|
|
56
|
+
* @param key - The property key to determine uniqueness for each item.
|
|
57
|
+
* @returns A new array with only distinct items based on the specified property.
|
|
58
|
+
*/
|
|
59
|
+
export const distinctBy = <T, K>(array: T[], selector: (item: T) => K): T[] => {
|
|
60
|
+
const seenKeys = new Set<K>();
|
|
61
|
+
return array.filter((item) => {
|
|
62
|
+
const key = selector(item);
|
|
63
|
+
|
|
64
|
+
if (seenKeys.has(key)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
seenKeys.add(key);
|
|
69
|
+
return true;
|
|
70
|
+
});
|
|
71
|
+
};
|