@@ -12,9 +12,11 @@ A small but powerful functional programming utility for type-safe pipe-like oper
1212---
1313
1414Assemble composes arrays of functions that pick from and assign to a given type (assembling it).
15+
1516The composed "assembly" is like a pipe that steps through each function, merging input with any
16- returned props and passing it to the next, returning the assembled result. Common use cases would be
17- applying a sequence of functions to app state, DB or API results.
17+ returned props and passing it to the next, returning the assembled result.
18+
19+ Common use cases would be applying a sequence of functions to app state, DB or API results.
1820
1921The focus of Assemble is to encourage an approach to function composition that is versatile yet
2022simple to reason about and is type-safe with minimal definition overhead.
@@ -27,9 +29,9 @@ This sample shows two "assemblers" operating on a props type. These functions be
2729hinting for the props they can access and if defined, the props they need to return.
2830
2931``` ts
30- import { assemble , Assembler , VoidAssembler } from ' @os-gurus/assemble'
32+ import { assemble , Assembler , NonAssembler } from ' @os-gurus/assemble'
3133
32- interface Props {
34+ type Props = {
3335 name: string
3436 message? : string
3537}
@@ -39,7 +41,7 @@ const prepareMessage: Assembler<Props, 'message'> = ({ name }) => {
3941}
4042// ☝️ Must return { message }
4143
42- const logMessage: VoidAssembler <Props > = ({ message }) => {
44+ const logMessage: NonAssembler <Props > = ({ message }) => {
4345 console .log (message )
4446}
4547// ☝️ Must return void
@@ -54,11 +56,11 @@ sayHello({ name: 'World' })
5456
5557### Assembler Functions
5658
57- The ` Assembler ` , ` PartialAssembler ` and ` VoidAssembler ` type utilities define functions that can
59+ The ` Assembler ` , ` PartialAssembler ` and ` NonAssembler ` type utilities define functions that can
5860be given to ` assemble ` and declare the props type they will operate on.
5961- ` Assembler ` functions return a subset of props, as defined by keys given to the utility.
6062- ` PartialAssembler ` functions optionally return a subset of props as defined.
61- - ` VoidAssembler ` functions return void, but can use the props, e.g. for logging or sending.
63+ - ` NonAssembler ` functions return undefined but can use the props, e.g. for logging or sending.
6264
6365Note, you don't have to use these utilities. Any function signature can be given to ` assemble ` as
6466long as it accepts a single props object argument and returns either a subset of props or nothing.
@@ -77,7 +79,7 @@ Assembler<Props, 'name' | 'message'>
7779PartialAssembler < Props , ' message' >
7880// ➥ (props: Props) => { message?: string } | undefined
7981
80- VoidAssembler < Props >
82+ NonAssembler < Props >
8183// ➥ (props: Props) => void
8284```
8385
@@ -86,15 +88,15 @@ VoidAssembler<Props>
8688### Async Assemblers
8789
8890` AsyncAssembler ` works exactly as ` Assembler ` for asynchronous functions and ` assemble ` can compose
89- a mixture of async and sync assemblers. As do ` AsyncPartialAssembler ` and ` AsyncVoidAssembler ` .
91+ a mixture of async and sync assemblers. As do ` AsyncPartialAssembler ` and ` AsyncNonAssembler ` .
9092
9193` assembleSync ` can be used to enforce synchronous functions and a non-promise return.
9294
9395``` ts
9496import fetch from ' node-fetch'
95- import { assemble , Assembler , AsyncAssembler , VoidAssembler } from ' @os-gurus/assemble'
97+ import { assemble , Assembler , AsyncAssembler , NonAssembler } from ' @os-gurus/assemble'
9698
97- interface Props {
99+ type Props = {
98100 name? : string
99101 message? : string
100102}
@@ -109,8 +111,9 @@ const prepareMessage: Assembler<Props, 'message'> = ({ name }) => {
109111 return { message: ` Hello ${name } ` }
110112}
111113
112- const logMessage: VoidAssembler <Props > = ({ message }) => {
114+ const logMessage: NonAssembler <Props > = ({ message }) => {
113115 console .log (message )
116+ return undefined // see known issues below
114117}
115118
116119const sayHello = assemble (fetchName , prepareMessage , logMessage )
@@ -158,6 +161,16 @@ mixedAssembly({ b: true })
158161
159162### Known Issues
160163
164+ ** Partial Assembler has to return undefined**
165+
166+ I tried to make void and partial assemblers that didn't need to return explicit undefined. This
167+ might have been nicer to use, but for now if they don't return a prop they must ` return undefined `
168+ (or ` {} ` for partial assemblers only).
169+
170+ Unfortunately any async function type will not raise a type error when applied to a ` () => void `
171+ type restriction. This meant by including void returning assemblers the ` SyncAssemblers ` type would
172+ allow async assemblers.
173+
161174** Merged type constraints override**
162175
163176Using ` MergeUnion ` on function's input and prop types to cast the return type of ` assemble ` creates
0 commit comments