From b7a1fe82a653c6e6765c5ad402d236f112f50fb0 Mon Sep 17 00:00:00 2001 From: Brendon Date: Tue, 22 Oct 2024 18:09:22 +0200 Subject: [PATCH] namedSeriesAll (#24) * fix seriesAll test type error * update docs command pnpm bump -> pnpm release * namedSeriesAll --- CHANGELOG.md | 8 +++++ README.md | 2 +- package.json | 2 +- src/helpers/namedSeriesAll.test.ts | 52 ++++++++++++++++++++++++++++++ src/helpers/namedSeriesAll.ts | 32 ++++++++++++++++++ src/helpers/seriesAll.test.ts | 2 +- 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/helpers/namedSeriesAll.test.ts create mode 100644 src/helpers/namedSeriesAll.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 704b111..d356b3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index c8e79e1..54ecc48 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package.json b/package.json index 2b8a77a..8196b2d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/helpers/namedSeriesAll.test.ts b/src/helpers/namedSeriesAll.test.ts new file mode 100644 index 0000000..3caf6ff --- /dev/null +++ b/src/helpers/namedSeriesAll.test.ts @@ -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') + }) +}) diff --git a/src/helpers/namedSeriesAll.ts b/src/helpers/namedSeriesAll.ts new file mode 100644 index 0000000..9dfa97f --- /dev/null +++ b/src/helpers/namedSeriesAll.ts @@ -0,0 +1,32 @@ +import { isFunction, isPromise } from '../validators' + +type NamedSeriesResult> = { + [K in keyof T]: T[K] extends Promise + ? U + : T[K] extends () => Promise + ? 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 >( + series: T, +): Promise> => { + const results = {} as NamedSeriesResult + + for (const key of Object.keys(series) as (keyof T)[]) { + const fn = series[key] + if (isPromise(fn)) + results[key] = (await fn) as NamedSeriesResult[typeof key] + else if (isFunction(fn)) results[key] = await fn() + else throw new Error('namedSeriesAll: invalid type') + } + return results +} diff --git a/src/helpers/seriesAll.test.ts b/src/helpers/seriesAll.test.ts index 616ffea..337c641 100644 --- a/src/helpers/seriesAll.test.ts +++ b/src/helpers/seriesAll.test.ts @@ -5,7 +5,7 @@ import { sleep } from "./sleep"; describe("seriesAll", () => { test("simple", async () => { expect( - await seriesAll([ + await seriesAll([ Promise.resolve(1), sleep(1).then(() => 2), () => Promise.resolve(3),