@dxos/util 0.5.3-main.bc67fdb → 0.5.3-main.c8ad1bb

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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Joins two untyped tables on the given columns.
3
+ * @param leftColumn The column to join on in the first table.
4
+ * @param rightColumn The column to join on in the second table.
5
+ */
6
+ export declare const joinTables: (leftColumn: string, rightColumn: string, left: Record<string, any>[], right: Record<string, any>[]) => any[];
7
+ //# sourceMappingURL=join-tables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"join-tables.d.ts","sourceRoot":"","sources":["../../../src/join-tables.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,eAAO,MAAM,UAAU,eACT,MAAM,eACL,MAAM,QACb,OAAO,MAAM,EAAE,GAAG,CAAC,EAAE,SACpB,OAAO,MAAM,EAAE,GAAG,CAAC,EAAE,UAwB7B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=join-tables.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"join-tables.test.d.ts","sourceRoot":"","sources":["../../../src/join-tables.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/util",
3
- "version": "0.5.3-main.bc67fdb",
3
+ "version": "0.5.3-main.c8ad1bb",
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.bc67fdb",
20
- "@dxos/invariant": "0.5.3-main.bc67fdb",
21
- "@dxos/node-std": "0.5.3-main.bc67fdb",
22
- "@dxos/keys": "0.5.3-main.bc67fdb"
19
+ "@dxos/invariant": "0.5.3-main.c8ad1bb",
20
+ "@dxos/node-std": "0.5.3-main.c8ad1bb",
21
+ "@dxos/debug": "0.5.3-main.c8ad1bb",
22
+ "@dxos/keys": "0.5.3-main.c8ad1bb"
23
23
  },
24
24
  "devDependencies": {
25
- "@dxos/crypto": "0.5.3-main.bc67fdb"
25
+ "@dxos/crypto": "0.5.3-main.c8ad1bb"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"
package/src/index.ts CHANGED
@@ -32,3 +32,4 @@ export * from './weak';
32
32
  export * from './map-values';
33
33
  export * from './defer';
34
34
  export * from './circular-buffer';
35
+ export * from './join-tables';
@@ -0,0 +1,30 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+ import { describe, test } from 'vitest';
7
+
8
+ import { joinTables } from './join-tables';
9
+
10
+ describe('joinTables', () => {
11
+ test('smoke', () => {
12
+ const left = [
13
+ { key: 1, name: 'A' },
14
+ { key: 2, name: 'B' },
15
+ ];
16
+
17
+ const right = [
18
+ { foreignKey: 1, value: 'X' },
19
+ { foreignKey: 3, value: 'Y' },
20
+ ];
21
+
22
+ const result = joinTables('key', 'foreignKey', left, right);
23
+
24
+ expect(result).to.deep.eq([
25
+ { foreignKey: 1, value: 'X', key: 1, name: 'A' },
26
+ { key: 2, name: 'B' },
27
+ { foreignKey: 3, value: 'Y' },
28
+ ]);
29
+ });
30
+ });
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Joins two untyped tables on the given columns.
3
+ * @param leftColumn The column to join on in the first table.
4
+ * @param rightColumn The column to join on in the second table.
5
+ */
6
+ //
7
+ // Copyright 2024 DXOS.org
8
+ //
9
+
10
+ export const joinTables = (
11
+ leftColumn: string,
12
+ rightColumn: string,
13
+ left: Record<string, any>[],
14
+ right: Record<string, any>[],
15
+ ) => {
16
+ const map = new Map();
17
+ const used = new Set();
18
+ for (const row of right) {
19
+ map.set(row[rightColumn], row);
20
+ }
21
+
22
+ const result = [];
23
+ for (const row of left) {
24
+ const right = map.get(row[leftColumn]);
25
+ used.add(right);
26
+
27
+ result.push(Object.assign(right ?? {}, row));
28
+ }
29
+
30
+ // Add unmatched rows from the right table.
31
+ for (const row of right) {
32
+ if (!used.has(row)) {
33
+ result.push(row);
34
+ }
35
+ }
36
+
37
+ return result;
38
+ };