-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roy Razon
committed
Jan 14, 2024
1 parent
8fed874
commit 5220276
Showing
35 changed files
with
934 additions
and
745 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type AsyncObjectIterator<TObject, TResult> = ( | ||
value: TObject[keyof TObject], | ||
key: string, | ||
collection: TObject, | ||
) => Promise<TResult> | ||
|
||
export const asyncMapValues = async <T extends object, TResult>( | ||
obj: T, | ||
callback: AsyncObjectIterator<T, TResult>, | ||
): Promise<{ [P in keyof T]: TResult }> => Object.fromEntries( | ||
await Promise.all( | ||
Object.entries(obj).map(async ([key, value]) => [key, await callback(value, key, obj)]) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, test, expect } from '@jest/globals' | ||
import { aggregator } from './aggregator.js' | ||
|
||
describe('aggregator', () => { | ||
type Obj = { key: string; value: number } | ||
|
||
test('distinct objects', () => { | ||
const agg = aggregator<Obj>(o => o.key) | ||
const o1 = { key: 'a', value: 1 } | ||
const o2 = { key: 'b', value: 2 } | ||
expect(agg('s1', [o1])).toEqual([o1]) | ||
expect(agg('s2', [o2])).toEqual([o1, o2]) | ||
}) | ||
|
||
test('duplicate objects', () => { | ||
const agg = aggregator<Obj>(o => o.key) | ||
const o1 = { key: 'a', value: 1 } | ||
const o2 = { key: 'a', value: 2 } | ||
expect(agg('s1', [o1])).toEqual([o1]) | ||
expect(agg('s2', [o2])).toEqual([o2]) | ||
expect(agg('s2', [])).toEqual([o1]) | ||
}) | ||
}) |
Oops, something went wrong.