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

Composable functions #140

Merged
merged 280 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
280 commits
Select commit Hold shift + click to select a range
d4820e2
Adjust serialize type
gustavoguichard Apr 10, 2024
057728d
Simpler mapError
gustavoguichard Apr 10, 2024
49d717c
remove unnecessary export
diogob Apr 10, 2024
576d5e1
Remove another unnecessary export
gustavoguichard Apr 10, 2024
8112b8e
Remove undocumented safeResult
gustavoguichard Apr 10, 2024
1774cb8
Remove serialize as it can be introduced in another PR
gustavoguichard Apr 10, 2024
0d777e3
Modify one test to compensate an celebrate the end of InputErrors class
gustavoguichard Apr 10, 2024
f0ca645
Remove toError export and refactor trace to avoid using it
gustavoguichard Apr 10, 2024
e9334c4
Merge pull request #3 from seasonedcc/over-simplify-errors
gustavoguichard Apr 10, 2024
19af6c5
Added missing default generic parameters when parsers are not given. …
diogob Nov 29, 2023
c18f531
Rename custom schema
gustavoguichard Apr 10, 2024
5526a8a
Merge pull request #4 from seasonedcc/ignore-input-on-undefined-input…
gustavoguichard Apr 10, 2024
64dad0d
Create the onErrorThrow function to replace fromSuccess with an unive…
gustavoguichard Apr 10, 2024
6df264a
Implement onErrorThrow with mapError and unify mapError implementations
gustavoguichard Apr 10, 2024
278861d
Remove comment
gustavoguichard Apr 10, 2024
e4583b0
Merge pull request #5 from seasonedcc/on-error-throw
diogob Apr 10, 2024
3e145cf
Rename onErrorThrow to fromSuccess for now
gustavoguichard Apr 10, 2024
13ed120
Merge pull request #6 from seasonedcc/composable-docs
gustavoguichard Apr 10, 2024
4eaf517
Rename test file
gustavoguichard Apr 10, 2024
a6766f2
Re-export all modules according to new discussed API
gustavoguichard Apr 10, 2024
ad96562
Split combinators tests into one file per combinator to make test nav…
diogob Apr 10, 2024
9bee28c
Move DF types (except for DomainFunction) to df namespace so we avoid
diogob Apr 10, 2024
c36bb3e
Merge pull request #7 from seasonedcc/revamp-modules
diogob Apr 10, 2024
b89b127
Export mergeObjects from combinators to avoid some imports
gustavoguichard Apr 10, 2024
eb46707
Rename UnpackResult from root to UnpackData to match df.UnpackData se…
diogob Apr 10, 2024
72cd78e
update build npm script
diogob Apr 10, 2024
0f92c20
Use a brand new vite application and use a local package of composabl…
diogob Apr 10, 2024
ed899ea
Adjust some UI and remove entry files from example
gustavoguichard Apr 11, 2024
55106a0
Create serializer and compat serializer
gustavoguichard Apr 11, 2024
c188581
Adjust mutation in example
gustavoguichard Apr 11, 2024
3cf5d5f
Adjust serializer types and fix example
gustavoguichard Apr 11, 2024
a68a68e
Merge pull request #8 from seasonedcc/migrate-example
gustavoguichard Apr 11, 2024
cc240e5
Express DomainFunction type in terms of Composable type
gustavoguichard Apr 12, 2024
173d8fc
Simplify fromSuccess type with function overload
gustavoguichard Apr 12, 2024
393efc2
Make map and mapError be universal combinators
gustavoguichard Apr 12, 2024
ec09270
Generalized catchError
diogob Apr 12, 2024
f120fc8
Generalize trace
diogob Apr 12, 2024
798a8eb
Using experimental version
diogob Apr 12, 2024
759c5a4
Merge pull request #10 from seasonedcc/universal-combinators
diogob Apr 12, 2024
b1b13d5
Update example with latest changes in CF modules
diogob Apr 13, 2024
504b49a
New README
diogob Apr 13, 2024
23dbd58
WIP - investigating why 2 optional arguments are removed from parameter
diogob Apr 13, 2024
30116d4
fix test defioinition, we should not remove a parameter from the
diogob Apr 15, 2024
6f92c46
WIP - creating subtype tuple for partial tuples
diogob Apr 15, 2024
500f7c9
perhaps we should handle partial in the same recursion?
diogob Apr 17, 2024
76020a9
extract CommonSubtype and cover incompatibility between optional argu…
diogob Apr 17, 2024
0aa41a7
Handle remaining case where only common subtype is undefined
diogob Apr 17, 2024
62f5b24
this might be the end of it
diogob Apr 17, 2024
6dfb517
Remove linting errors from types test files
gustavoguichard Apr 17, 2024
325c479
Add an internal types module
gustavoguichard Apr 17, 2024
87f51ed
Fix couple type bugs
gustavoguichard Apr 17, 2024
cbcdfad
Adjust some types and add more types tests
gustavoguichard Apr 17, 2024
71f0f9c
fix some type tests
gustavoguichard Apr 17, 2024
645042c
found the infinite loop. The default definition of Composable and tho…
diogob Apr 18, 2024
fa7f6a9
Type checking on collect now generates errors only on the parameters
diogob Apr 18, 2024
60dfb29
Make all only accept array of composables
gustavoguichard Apr 18, 2024
40a5fe5
Make the CommonSubtype intersect records
gustavoguichard Apr 18, 2024
297f8ef
Make all and collect out of cfs
gustavoguichard Apr 18, 2024
5d94865
Merge pull request #11 from seasonedcc/fix-all-parameters
gustavoguichard Apr 18, 2024
a49d490
Export all and merge from CF as they are now universal
gustavoguichard Apr 18, 2024
0ad1956
Export collect from CF as they are now universal
gustavoguichard Apr 18, 2024
ac64d41
Bring first from df to cf as it is also a parallel combinator
gustavoguichard Apr 18, 2024
f2b6ece
Remove stale type utility
gustavoguichard Apr 18, 2024
8e3b21b
Merge pull request #12 from seasonedcc/all-and-merge-from-cf
gustavoguichard Apr 18, 2024
8331fd3
Remove another stale type utility
gustavoguichard Apr 18, 2024
b001081
Fix error reporting on collect type
gustavoguichard Apr 18, 2024
82684e1
Remove DF type in favor of only Composable
gustavoguichard Apr 18, 2024
8585012
Remove DF overload from fromSuccess
gustavoguichard Apr 18, 2024
422afad
Merge pull request #13 from seasonedcc/fix-collect-error-reporting
gustavoguichard Apr 18, 2024
f1647cd
Interoperability achieved - not that type safe tho
gustavoguichard Apr 18, 2024
dbd2cdf
Rename df.make -> withSchema - and - df.fromComposable -> applySchema
gustavoguichard Apr 18, 2024
4d44f82
Rename df module to environment
gustavoguichard Apr 18, 2024
e61d037
Unify UnpackData
gustavoguichard Apr 18, 2024
39bf0f0
Merge pull request #14 from seasonedcc/remove-df-type
gustavoguichard Apr 18, 2024
8a51ea3
Fix a bug with the applySchema type
gustavoguichard Apr 18, 2024
93f0b2e
Tackle some tests to eliminate repetition and have more represented i…
gustavoguichard Apr 18, 2024
cdb95cd
💅
gustavoguichard Apr 18, 2024
c1e94bf
Update example folder
gustavoguichard Apr 18, 2024
fde3ebc
Merge pull request #16 from seasonedcc/update-example
gustavoguichard Apr 18, 2024
4411732
Merge pull request #15 from seasonedcc/adjusting-tests
gustavoguichard Apr 19, 2024
59caab0
Pipe would always fail to compose more than 2 arguments
diogob Apr 19, 2024
464983d
Merge pull request #17 from seasonedcc/fix-pipe-arguments
diogob Apr 19, 2024
1f15578
Removes or hides some types we don't want to commit to publicly
gustavoguichard Apr 19, 2024
5dd2ef3
Fix the collect weird bug where it won't accept inline functions
gustavoguichard Apr 19, 2024
d862031
Normalize some types params
gustavoguichard Apr 19, 2024
af2915b
Reduce Parser types to the essential and public
gustavoguichard Apr 19, 2024
802a5e2
Fix flaky tsc by doing some non-sense change
gustavoguichard Apr 19, 2024
9104c7b
Merge pull request #18 from seasonedcc/removing-and-hiding-types
gustavoguichard Apr 19, 2024
8e38818
Bump experimental version
gustavoguichard Apr 19, 2024
d82638e
Add failing test. We should first combina all parameters, and replace…
diogob Apr 19, 2024
5322fa2
Fix all with flexible arity and collect types
gustavoguichard Apr 19, 2024
d827158
Remove log leftover
gustavoguichard Apr 19, 2024
2658ce8
Merge pull request #19 from seasonedcc/fix-all-arguments-for-any-arity
gustavoguichard Apr 19, 2024
a9ef066
Update README.md
gustavoguichard Apr 19, 2024
83b0856
Update README.md
gustavoguichard Apr 19, 2024
c4188a4
Update README.md
gustavoguichard Apr 19, 2024
b936410
Add ArkType example
gustavoguichard Apr 19, 2024
b6c9110
Merge pull request #20 from seasonedcc/example-arktype
gustavoguichard Apr 20, 2024
7b6353b
Keep easy access to tests prelude in tests
gustavoguichard Apr 22, 2024
cb7fe4d
Remove arcane syntax from combinators arguments
gustavoguichard Apr 22, 2024
c0d2138
Remove some legacy type casts
gustavoguichard Apr 22, 2024
94c6a69
Extract a lil helper
gustavoguichard Apr 22, 2024
905af43
💅
gustavoguichard Apr 22, 2024
c75b786
Remove arcane params from pipe
gustavoguichard Apr 22, 2024
638160f
Merge pull request #22 from seasonedcc/remove-arkane-params
gustavoguichard Apr 22, 2024
69d3f79
Fix case with composing a full partial with non-partial args
gustavoguichard Apr 22, 2024
6b9dbf7
Normalize helpers for composition failure and argument incompatibilit…
gustavoguichard Apr 22, 2024
6d6236d
Experimental release
gustavoguichard Apr 22, 2024
b5176e4
Environments can have arbitrary types (except in collectSequence).
diogob Apr 20, 2024
988c173
Apply some of the new patterns
gustavoguichard Apr 23, 2024
8a280e0
Now we don't need to avoid inline declarations of composables
gustavoguichard Apr 23, 2024
2742459
TODO: We found an infinite loop and almost got there
gustavoguichard Apr 23, 2024
6b6c06d
Fix sequential environment compositions
diogob Apr 24, 2024
e57c892
Write some tests and cleanup meta tests
gustavoguichard Apr 24, 2024
78ed7ba
Remove applyEnvironment from public API
gustavoguichard Apr 24, 2024
719ca21
Just create 2 additional test cases making the use of mandatory secon…
diogob Apr 24, 2024
847feae
Simplify PipeReturn centralizing all Parameters validation in PipeArg…
diogob Apr 25, 2024
ff03bba
First draft for generic branch
diogob Apr 25, 2024
3e98c78
Add some TODO comments to test things that branch didn't implement yet
gustavoguichard Apr 25, 2024
f9216cd
Merge pull request #21 from seasonedcc/composable-environments
diogob Apr 25, 2024
a262946
Publish a new experimental version
gustavoguichard Apr 26, 2024
d2a78d0
remove outdated note
diogob May 2, 2024
0da09de
This is how the code looks with deno formatter
diogob May 3, 2024
2cc5c73
Merge pull request #24 from seasonedcc/deno-fmt
diogob May 4, 2024
127f1f1
First draft of BranchReturn type. Still some basic cases failing.
diogob May 1, 2024
158d166
Cover cases where there might be a null or composable in resolver
diogob May 1, 2024
ac6e0a1
PipeReturn and PipeArguments to the rescue
diogob May 1, 2024
b39c71a
DRY the code a bit
diogob May 1, 2024
d056d3e
DRY a bit more
diogob May 1, 2024
e3202f0
We can safely ignore parameters that are not used (perhaps the compos…
diogob May 1, 2024
0f43fc2
Prettify composable union
diogob May 1, 2024
0003c0b
Fix failure to compose with prettify
diogob May 1, 2024
bdb9cff
Lil refactoring to avoid PrettifyComposable in public API and move te…
gustavoguichard May 2, 2024
59c6a69
environment.branch seems to be fine
diogob May 3, 2024
1539374
Rename types to keep right mind model and carry the context of module…
gustavoguichard May 3, 2024
d82d785
Relax the type constraints since we can receive an error type in thes…
diogob May 4, 2024
3b83fb8
Now we have a proper environment in the BranchReturn
diogob May 4, 2024
3b84d85
Merge pull request #23 from seasonedcc/branch-types
gustavoguichard May 6, 2024
046648e
Update Docs & JSDocs (#25)
diogob May 6, 2024
a7f3558
mapParameters draft - to quench your thirst for flips
diogob May 8, 2024
67c2a8a
Export mapParameters from index and add some JSDocs
gustavoguichard May 9, 2024
bd6264e
Add tests to mapParameters
gustavoguichard May 10, 2024
6fe7ddd
Merge pull request #26 from seasonedcc/map-parameters
diogob May 10, 2024
34d6ab2
Always use FailToCompose to build sequential composition failure. Cha…
diogob May 11, 2024
405ffd2
Rewrite all references to IncompatibleArguments using FailToCompose
diogob May 11, 2024
09356db
Replace all direct references to old incompatible arguments shape for…
diogob May 11, 2024
2f3428d
Rewrite FailToCompose in terms of IncompatibleArguments
diogob May 12, 2024
15e0a92
Merge pull request #27 from seasonedcc/unify-composition-type-errors
gustavoguichard May 13, 2024
b0cd6c0
First beta version
gustavoguichard May 6, 2024
7a9ee9e
Second beta version with mapParameters
gustavoguichard May 10, 2024
d59c906
Add a test for type checking the mapParameters mapper and deals with …
gustavoguichard May 10, 2024
c4dbcf9
Add task to lint docs and test code formatting in examples
diogob May 11, 2024
fb6f7b6
Remove compat module
diogob May 11, 2024
eac6c43
Add environments first draft
diogob May 12, 2024
f1b6ef0
Add branch JSDoc
diogob May 12, 2024
dd18055
Add some JSDoc to error class properties
diogob May 12, 2024
4d97e28
Use named functions as default syntax for top level functions
diogob May 12, 2024
97f81fa
Format examples in JSDoc as TS. nvim-cmp and deno docs require this t…
diogob May 12, 2024
580a891
Add top module description
diogob May 13, 2024
0071dee
Format a few more examples
diogob May 13, 2024
874e5c5
Add Docs for with-schema, update table of contents and more Docs WIP
gustavoguichard May 13, 2024
f639c4c
Merge pull request #28 from seasonedcc/beta-docs
gustavoguichard May 13, 2024
a8da90a
More Docs updates
gustavoguichard May 13, 2024
88596b5
Remove the merge combinator to avoid its dangers
gustavoguichard May 13, 2024
3fb7e13
Rename MergeObjs type utility to MergeObjects
gustavoguichard May 13, 2024
684258a
Merge pull request #30 from seasonedcc/remove-merge
diogob May 13, 2024
ea6bdaa
Remove the first combinator now that we have branch
gustavoguichard May 13, 2024
eba9eea
Merge pull request #29 from seasonedcc/remove-first
diogob May 13, 2024
7a336f0
Fix typo in file link
diogob May 13, 2024
2f0f0cd
New beta version
gustavoguichard May 14, 2024
66ae4a3
Add first draft of migration docs
gustavoguichard May 16, 2024
7da5c47
fix README note on composition error
diogob May 16, 2024
57ae538
Add notes to the new Result type in the migration docs
gustavoguichard May 16, 2024
b2345e9
Add an updated table of contents
gustavoguichard May 16, 2024
1c18e96
Add environment to the migration docs
gustavoguichard May 16, 2024
72d4696
Merge pull request #31 from seasonedcc/migration-docs
gustavoguichard May 16, 2024
2525f36
Update README.md
gustavoguichard May 16, 2024
b1a9fe1
Update CF logo
gustavoguichard May 16, 2024
db1abdb
Fixes and polishes the Docs, removes every instance of DF
gustavoguichard May 17, 2024
c9e88c1
Add JSDocs to applySchema
gustavoguichard May 17, 2024
8e12d38
Update Table of Contents in the README
gustavoguichard May 17, 2024
2017a0a
Adds API Reference Doc
gustavoguichard May 17, 2024
86b5d93
Merge pull request #32 from seasonedcc/api-docs
gustavoguichard May 17, 2024
b8a05d3
Add a link to examples folder
gustavoguichard May 17, 2024
d8c7ca2
Fixing typos in the docs
gustavoguichard May 17, 2024
8ae459a
Update README.md
gustavoguichard May 17, 2024
56a50b1
Update README.md
gustavoguichard May 17, 2024
c669a7c
Adds a list of benefits to the README
gustavoguichard May 17, 2024
0cdbabe
Update the package description
gustavoguichard May 17, 2024
93612c6
Update example
gustavoguichard May 17, 2024
d3b72bb
Rename mapError to mapErrors
gustavoguichard May 17, 2024
55a75b0
Rename toErrorPayload to serializeError
gustavoguichard May 17, 2024
ee3f8a5
Merge pull request #33 from seasonedcc/rename-maperror-to-maperrors
diogob May 17, 2024
7601e6d
Add tests to serializers
gustavoguichard May 17, 2024
72ebfd5
Merge pull request #34 from seasonedcc/serialize-test
gustavoguichard May 17, 2024
e73c54b
Rename catchError to catchFailure
gustavoguichard May 17, 2024
0c70001
Update README.md
gustavoguichard May 20, 2024
c876b74
Input and Environment default to unknown to be aligned with types exp…
gustavoguichard May 20, 2024
2136dc5
Merge pull request #35 from seasonedcc/catch-failure
diogob May 20, 2024
14ff7ad
Merge pull request #36 from seasonedcc/default-schema-is-unknown
diogob May 20, 2024
5ee9818
Make parsers synchronous again
gustavoguichard May 20, 2024
7605c6c
Merge pull request #37 from seasonedcc/parser
diogob May 20, 2024
9b6a32c
tweak fromSuccess for clarity for within composables use case
diogob May 20, 2024
9f14e4d
Adds list of recipes to Table of Contents
gustavoguichard May 20, 2024
71869d6
Lil adjustment in README
gustavoguichard May 20, 2024
1f25246
WIP
gustavoguichard May 17, 2024
8b9e127
Update Docs with deserializers and error checkers
gustavoguichard May 21, 2024
66119e7
Update exports
gustavoguichard May 21, 2024
143d931
Add tests to deserializers and error checkers
gustavoguichard May 21, 2024
f347253
Remove deserialize since it's much harder than we anticipated.
diogob May 21, 2024
af5fe23
Rename for the sake of consistency
diogob May 21, 2024
88973f5
Merge pull request #38 from seasonedcc/deserializers
diogob May 21, 2024
e573fa1
Add GPT guides to environemnts.md
gustavoguichard May 21, 2024
c8658ed
Add GPT guides to with-schema.md
gustavoguichard May 21, 2024
0c8a500
Edit the GPT version of with-schema
gustavoguichard May 21, 2024
f08238c
Edit example to use more composables then withSchema
gustavoguichard May 21, 2024
eb0a108
Call the section 'Guides' instead of 'Recipes'
gustavoguichard May 21, 2024
acf2ce2
Add a comment about the `applySchema` usage
gustavoguichard May 21, 2024
96d405a
Merge pull request #40 from seasonedcc/example
gustavoguichard May 21, 2024
d87f20f
Edit the GPT version of environments
gustavoguichard May 21, 2024
719db35
Merge pull request #39 from seasonedcc/guides
gustavoguichard May 21, 2024
cecbc7b
Add a list of benefits on migrating from DF to CF
gustavoguichard May 21, 2024
9cae2c3
Add emojis... can't help
gustavoguichard May 21, 2024
44ddeec
Having these types makes it easier for a library user to build custom…
diogob May 22, 2024
a5ef9cd
Merge pull request #41 from seasonedcc/export-additional-types-for-ex…
diogob May 22, 2024
a312ad6
Updates some JSDocs
gustavoguichard May 21, 2024
e475d15
Release a new beta version
gustavoguichard May 21, 2024
c57f709
Reference [email protected] from migration doc
gustavoguichard May 22, 2024
3812b2e
Reference isInputError and isEnvironmentError in migration docs
gustavoguichard May 22, 2024
4c39520
Forward arguments on map function
gustavoguichard May 22, 2024
360636b
Implement pipe with map
gustavoguichard May 22, 2024
330f230
Document new map capabilities
gustavoguichard May 22, 2024
9f8a879
Publish a new version
gustavoguichard May 22, 2024
63346d9
Add links to fns in migration benefits docs
gustavoguichard May 22, 2024
eae12ff
Merge pull request #43 from seasonedcc/forward-args-on-map
gustavoguichard May 22, 2024
0097af2
mapper typing was causing subtle bugs when interacting with DFs
diogob May 23, 2024
1bb5fa0
bump beta
diogob May 23, 2024
6c72004
Adjust branch resolver type and add a type test to ensure it in the f…
gustavoguichard May 23, 2024
682afa8
Bump beta version
gustavoguichard May 23, 2024
607300a
Merge pull request #44 from seasonedcc/adjust-branch-resolver-type
gustavoguichard May 23, 2024
cb151f1
Add migration note on polymorphic functions
gustavoguichard May 23, 2024
b91b6ef
Make applySchema API similar to withSchema
gustavoguichard May 23, 2024
460985a
Merge pull request #46 from seasonedcc/apply-schema-order
diogob May 23, 2024
a7c8afb
Bump beta version and update lock
diogob May 23, 2024
d50fbdb
Add missing throwing section and tweak catch example
diogob May 23, 2024
4b53d7f
Merge pull request #47 from seasonedcc/tweak-error-handling-doc
diogob May 23, 2024
cac614a
Copy all documentation to npm folder after building
gustavoguichard May 23, 2024
b6f3d79
Update arktype and remix examples and add docs and an adapter to arktype
gustavoguichard May 23, 2024
6343eb2
Merge pull request #48 from seasonedcc/update-examples
gustavoguichard May 23, 2024
5054a67
Revamp list of benefits in README
gustavoguichard May 23, 2024
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
878 changes: 878 additions & 0 deletions API.md

Large diffs are not rendered by default.

986 changes: 162 additions & 824 deletions README.md

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"version": "2.6.0",
"version": "1.0.0-beta-20240523-3",
"tasks": {
"test": "deno test --allow-env --allow-net src",
"publish": "deno task build-npm && cd npm/ && npm publish",
"build-npm": "deno run -A scripts/build-npm.ts",
"docs": "deno doc --html --name='domain-functions' ./mod.ts"
"docs": "deno doc --html --name='composable-functions' ./mod.ts",
"docs-lint": "deno doc --lint ./mod.ts"
},
"lint": {
"include": [
Expand All @@ -16,5 +17,17 @@
"ban-types"
]
}
},
"compilerOptions": {
"types": ["./src/test.d.ts"]
},
"fmt": {
"useTabs": false,
"lineWidth": 80,
"indentWidth": 2,
"semiColons": false,
"singleQuote": true,
"proseWrap": "preserve",
"include": ["src/"]
}
}
87 changes: 37 additions & 50 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions environments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
## Environments

Sometimes you want to ensure the safety of certain values that are constant accross sequential compositions.
This parameter is called an environment. And to simplify the composition of this kind of Composable
we always define it with a single input. Therefore we have the type

```ts
Composable<(input: I, environment: E) => O>
```

A common use case would be a sequence of functions that depend on an authorization system.
The currently authenticated user would have to be propagated every time there is a sequential composition.
To avoid such awkwardness we use environments:

```tsx
import { environment } from 'composable-functions'
const dangerousFunction = composable(async (input: string, { user } : { user: { name: string, admin: boolean } }) => {
// do something that only the admin can do
})

const carryUser = environment.pipe(gatherInput, dangerousFunction)
```

## Composing with environments

These combinators are useful for composing functions with environment. Note that the standard parallel compositions will work just fine with the concept of environments.

### `pipe`

The environment.pipe function allows you to compose multiple functions in a sequence, forwarding the environment to each function in the chain.

```ts
import { environment } from 'composable-functions'

const a = composable((str: string, env: { user: User }) => str === '1')
const b = composable((bool: boolean, env: { user: User }) => bool && env.user.admin)

const pipeline = environment.pipe(a, b)

const result = await pipeline('1', { user: { admin: true } })
/*
result = {
success: true,
data: true,
errors: []
}
*/
```

### `sequence`
The environment.sequence function works similarly to pipe, but it returns a tuple containing the result of each function in the sequence.

```ts
import { environment } from 'composable-functions'

const a = composable((str: string, env: { user: User }) => str === '1')
const b = composable((bool: boolean, env: { user: User }) => bool && env.user.admin)

const sequence = environment.sequence(a, b)

const result = await sequence('1', { user: { admin: true } })
/*
result = {
success: true,
data: [true, true],
errors: []
}
*/
```

### `branch`

The environment.branch function adds conditional logic to your compositions, forwarding the environment to each branch as needed.

```ts
import { composable, environment } from 'composable-functions'

const adminIncrement = composable((a: number, { user }: { user: { admin: boolean } }) =>
user.admin ? a + 1 : a
)
const adminMakeItEven = (sum: number) => sum % 2 != 0 ? adminIncrement : null
const incrementUntilEven = environment.branch(adminIncrement, adminMakeItEven)

const result = await incrementUntilEven(1, { user: { admin: true } })
/*
result = {
success: true,
data: 2,
errors: []
}
*/
```
23 changes: 23 additions & 0 deletions examples/arktype/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
9 changes: 9 additions & 0 deletions examples/arktype/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Use composable-functions with a custom parser

This simple example can be a reference to adapt composable-functions to any other parser library.

There are two approaches to use composable-functions with a custom parser:
- Create an adapter function that will receive a schema and return a schema in the shape of a `ParserSchena`. Example: [the `adapt` function](./src/adapters.ts).
- Create your custom `withSchema` and `applySchema` that will validate your input and environment and return a `Result`. Example: [the `withArkSchema` and `applyArkSchema` functions](./src/adapters.ts).

Check out the [`./src`](./src/) directory to understand how we implemented both approaches with [`arktype`](https://github.com/arktypeio/arktype).
18 changes: 18 additions & 0 deletions examples/arktype/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "composable-functions-arktype-example",
"private": true,
"version": "0.0.0",
"type": "module",
"main": "src/usage.ts",
"scripts": {
"dev": "tsx src/usage.ts"
},
"devDependencies": {
"tsx": "^4.7.2"
},
"dependencies": {
"arktype": "2.0.0-dev.7",
"composable-functions": "file:../../npm",
"typescript": "^5.4.5"
}
}
Loading
Loading