Skip to content

Commit

Permalink
namedSeriesAll (#24)
Browse files Browse the repository at this point in the history
* fix seriesAll test type error

* update docs command pnpm bump -> pnpm release

* namedSeriesAll
  • Loading branch information
betafcc authored Oct 22, 2024
1 parent ed568e7 commit b7a1fe8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# deverything

## 1.9.0

### Minor Changes

- namedSeriesAll
- update docs command pnpm bump -> pnpm release
- fix seriesAll test type error

## 1.8.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ These functions are optimized for low entropy random data generation useful for
After changes, run

```
pnpm bump
pnpm release
```

To bump the version. CI will take care of publishing the package when merged.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "1.8.1",
"version": "1.9.0",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
52 changes: 52 additions & 0 deletions src/helpers/namedSeriesAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, test } from '@jest/globals'
import { namedSeriesAll } from './namedSeriesAll'
import { sleep } from './sleep'

describe('namedSeriesAll', () => {
test('simple', async () => {
expect(
await namedSeriesAll({
a: Promise.resolve(true),
b: sleep(1).then(() => 42),
c: () => Promise.resolve('foo'),
d: async () => [69],
e: async () => {
await sleep(1)
return null
},
f: async () => {
return sleep(1).then(() => {})
},
}),
).toStrictEqual({
a: true,
b: 42,
c: 'foo',
d: [69],
e: null,
f: undefined,
})
})

test('throw new Error', () => {
expect(
namedSeriesAll({
first: () => {
throw new Error('1')
},
second: () => {
throw new Error('2')
},
}),
).rejects.toThrowError('1')
})

test('Promise.reject', () => {
expect(
namedSeriesAll({
first: Promise.reject('3'),
second: () => Promise.reject('4'),
}),
).rejects.toEqual('3')
})
})
32 changes: 32 additions & 0 deletions src/helpers/namedSeriesAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isFunction, isPromise } from '../validators'

type NamedSeriesResult<T extends Record<string, unknown>> = {
[K in keyof T]: T[K] extends Promise<infer U>
? U
: T[K] extends () => Promise<infer U>
? U
: never
} & {}

/**
* @description Run a series of (async) functions in order and return the results as an object with the same keys.
* @example
* const { foo, bar } = await namedSeriesAll({
* foo: Promise.resolve(1),
* bar: async () => 2,
* }); // => { foo: 1, bar: 2 }
*/
export const namedSeriesAll = async <T extends Record<string, unknown>>(
series: T,
): Promise<NamedSeriesResult<T>> => {
const results = {} as NamedSeriesResult<T>

for (const key of Object.keys(series) as (keyof T)[]) {
const fn = series[key]
if (isPromise(fn))
results[key] = (await fn) as NamedSeriesResult<T>[typeof key]
else if (isFunction(fn)) results[key] = await fn()
else throw new Error('namedSeriesAll: invalid type')
}
return results
}
2 changes: 1 addition & 1 deletion src/helpers/seriesAll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sleep } from "./sleep";
describe("seriesAll", () => {
test("simple", async () => {
expect(
await seriesAll<number>([
await seriesAll([
Promise.resolve(1),
sleep(1).then(() => 2),
() => Promise.resolve(3),
Expand Down

0 comments on commit b7a1fe8

Please sign in to comment.