Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New sequenceAll combinator #127

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/composable/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ function collect<T extends Record<string, Composable>>(
* @example
* import { cf as C } from 'domain-functions'
*
* const a = C.compose((aNumber: number) => String(aNumber))
* const b = C.compose((aString: string) => aString === '1')
* const a = C.composable((aNumber: number) => String(aNumber))
* const b = C.composable((aString: string) => aString === '1')
* const cf = C.sequence(a, b)
* // ^? Composable<(aNumber: number) => [string, boolean]>
*/
Expand All @@ -166,6 +166,40 @@ function sequence<T extends [Composable, ...Composable[]]>(
>
}

/**
* Sequences a tuple of functions and collect the output of every function like `sequence` feeding the same input to all of them.
* Will abort the sequence on the first error.
* @example
* import { composable as C } from 'domain-functions'
*
* const a = C.composable((something: unknown) => Number(something))
* const b = C.composable((aString: string) => aString === '1')
* const cf = C.sequenceAll(a, b)
* // ^? Composable<(aString: string) => [string, boolean]>
*/
function sequenceAll<T extends [Composable, ...Composable[]]>(
...fns: T & AllArguments<T>
) {
return (async (...args) => {
const [head, ...tail] = fns as T

const res = await head(...args)
if (!res.success) return error(res.errors)

const result = [res.data]
for await (const fn of tail) {
const res = await fn(...args)
if (!res.success) return error(res.errors)
result.push(res.data)
}
return success(result)
}) as Composable<
(...args: Parameters<AllArguments<T>[0]>) => {
[key in keyof T]: UnpackResult<ReturnType<Extract<T[key], Composable>>>
}
>
}

/**
* It takes a Composable and a predicate to apply a transformation over the resulting `data`. It only runs if the function was successfull. When the given function fails, its error is returned wihout changes.
* @example
Expand Down Expand Up @@ -224,6 +258,7 @@ export {
mergeObjects,
pipe,
sequence,
sequenceAll,
success,
}

32 changes: 31 additions & 1 deletion src/composable/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assertEquals, describe, it } from '../test-prelude.ts'
import { map, mapError, pipe, sequence } from './index.ts'
import type { Composable, ErrorWithMessage, Result } from './index.ts'
import { Equal, Expect } from './types.test.ts'
import { all, collect, composable } from './composable.ts'
import { all, collect, composable, sequenceAll } from './composable.ts'

const voidFn = composable(() => {})
const toString = composable((a: unknown) => `${a}`)
Expand Down Expand Up @@ -189,6 +189,36 @@ describe('sequence', () => {
})
})

describe('sequenceAll', () => {
it('executes all functions in sequence using the same input returning a tuple with every result when all are successful', async () => {
const fn = sequenceAll(add, toString, voidFn)

const res = await fn(1, 2)

assertEquals(res, { success: true, data: [3, '1', undefined], errors: [] })
})

it('expects argument types just like all', () => {
const isA = composable((aString: string) => aString === 'a')

const fn = sequenceAll(toString, isA)
type _FN = Expect<
Equal<typeof fn, Composable<(a: string) => [string, boolean]>>
>
})

it('fails when any function fails', async () => {
const fn = sequenceAll(add, faultyAdd)

const res = await fn(1, 2)

assertEquals(res, {
success: false,
errors: [{ exception: new Error('a is 1'), message: 'a is 1' }],
})
})
})

describe('all', () => {
it('executes all functions using the same input returning a tuple with every result when all are successful', async () => {
const fn = all(add, toString, voidFn)
Expand Down
Loading