@fulcro/functions 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/dist/switchFor/index.d.ts +33 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ const label = switchFor(status, {
|
|
|
33
33
|
|
|
34
34
|
Leave a member out and it does not compile:
|
|
35
35
|
|
|
36
|
-
```
|
|
36
|
+
```text
|
|
37
37
|
error TS2345: Property '[Status.Archived]' is missing in type
|
|
38
38
|
'{ 0: () => string; 1: () => string; }' but required in type
|
|
39
39
|
'ExhaustiveCases<Status, string>'.
|
|
@@ -163,3 +163,8 @@ came would make the failure indistinguishable from a success.
|
|
|
163
163
|
|
|
164
164
|
`tryCatch` always returns a promise, including for a fully synchronous
|
|
165
165
|
operation.
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
**Full guide:** [docs/functions.md](../../docs/functions.md) — scenarios, worked
|
|
170
|
+
examples and the failure modes worth knowing before you meet them.
|
|
@@ -123,6 +123,38 @@ export type ExhaustiveCases<T extends PropertyKey, R> = {
|
|
|
123
123
|
* @returns The result of the branch that handled the value, the result of
|
|
124
124
|
* `otherwise`, or `undefined` when nothing matched and no fallback was given.
|
|
125
125
|
*/
|
|
126
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Chooses between branches by condition, falling back when none matches.
|
|
128
|
+
*
|
|
129
|
+
* @template T Type of the evaluated value.
|
|
130
|
+
* @template R Type produced by every branch.
|
|
131
|
+
* @param value Value being evaluated.
|
|
132
|
+
* @param cases Branches, tested in order; the first match wins.
|
|
133
|
+
* @param otherwise Produces the result when no branch matches.
|
|
134
|
+
* @returns The result of the matching branch, or of `otherwise`.
|
|
135
|
+
*/
|
|
127
136
|
export declare function switchFor<T, R>(value: T, cases: readonly SwitchCase<T, R>[], otherwise: (value: T) => R): R;
|
|
137
|
+
/**
|
|
138
|
+
* Chooses between branches by condition, with nothing to fall back on.
|
|
139
|
+
*
|
|
140
|
+
* @template T Type of the evaluated value.
|
|
141
|
+
* @template R Type produced by every branch.
|
|
142
|
+
* @param value Value being evaluated.
|
|
143
|
+
* @param cases Branches, tested in order; the first match wins.
|
|
144
|
+
* @returns The result of the matching branch, or `undefined` when none matched.
|
|
145
|
+
*/
|
|
128
146
|
export declare function switchFor<T, R>(value: T, cases: readonly SwitchCase<T, R>[]): R | undefined;
|
|
147
|
+
/**
|
|
148
|
+
* Chooses between one branch per member of a closed set, checked exhaustively.
|
|
149
|
+
*
|
|
150
|
+
* Leaving a member out does not compile, and neither does adding a branch for
|
|
151
|
+
* something that is not a member. No fallback is accepted, deliberately: a
|
|
152
|
+
* fallback is what would absorb a newly added member in silence.
|
|
153
|
+
*
|
|
154
|
+
* @template T Union being matched, typically an enum.
|
|
155
|
+
* @template R Type produced by every branch.
|
|
156
|
+
* @param value Value being evaluated.
|
|
157
|
+
* @param cases One branch per member of `T`.
|
|
158
|
+
* @returns The result of the branch handling the value.
|
|
159
|
+
*/
|
|
160
|
+
export declare function switchFor<T extends PropertyKey, R>(value: T, cases: ExhaustiveCases<T, R>): R;
|