@typed/guard 0.6.0 → 0.7.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/.nvmrc +1 -0
- package/biome.json +36 -0
- package/dist/ExtensibleFunction.d.ts +1 -0
- package/dist/ExtensibleFunction.js +7 -0
- package/dist/ExtensibleFunction.js.map +1 -0
- package/dist/Guard.d.ts +143 -0
- package/dist/{esm/index.js → Guard.js} +32 -107
- package/dist/Guard.js.map +1 -0
- package/dist/Guardable.d.ts +10 -0
- package/dist/Guardable.js +13 -0
- package/dist/Guardable.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -21
- package/readme.md +197 -0
- package/src/ExtensibleFunction.test.ts +104 -0
- package/src/ExtensibleFunction.ts +8 -0
- package/src/Guard.test.ts +189 -0
- package/src/Guard.ts +536 -0
- package/src/Guardable.test.ts +18 -0
- package/src/Guardable.ts +21 -0
- package/src/index.ts +3 -494
- package/tsconfig.json +27 -0
- package/LICENSE +0 -21
- package/README.md +0 -5
- package/dist/cjs/index.js +0 -199
- package/dist/cjs/index.js.map +0 -1
- package/dist/dts/index.d.ts +0 -254
- package/dist/dts/index.d.ts.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/package.json +0 -4
package/src/index.ts
CHANGED
|
@@ -1,494 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import type { ParseOptions } from "@effect/schema/AST"
|
|
6
|
-
import type * as ParseResult from "@effect/schema/ParseResult"
|
|
7
|
-
import * as Schema from "@effect/schema/Schema"
|
|
8
|
-
import type * as Cause from "effect/Cause"
|
|
9
|
-
import type * as Context from "effect/Context"
|
|
10
|
-
import * as Effect from "effect/Effect"
|
|
11
|
-
import { dual } from "effect/Function"
|
|
12
|
-
import type * as Layer from "effect/Layer"
|
|
13
|
-
import * as Option from "effect/Option"
|
|
14
|
-
import type * as Predicate from "effect/Predicate"
|
|
15
|
-
import type * as Runtime from "effect/Runtime"
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @since 1.0.0
|
|
19
|
-
*/
|
|
20
|
-
export type Guard<in I, out O, out E = never, out R = never> = (input: I) => Effect.Effect<Option.Option<O>, E, R>
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @since 1.0.0
|
|
24
|
-
*/
|
|
25
|
-
export namespace Guard {
|
|
26
|
-
/**
|
|
27
|
-
* @since 1.0.0
|
|
28
|
-
*/
|
|
29
|
-
export type Input<T> = [T] extends [Guard<infer I, infer _R, infer _E, infer _O>] ? I :
|
|
30
|
-
[T] extends [AsGuard<infer I, infer _R, infer _E, infer _O>] ? I
|
|
31
|
-
: never
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @since 1.0.0
|
|
35
|
-
*/
|
|
36
|
-
export type Context<T> = [T] extends [Guard<infer _I, infer _O, infer _E, infer R>] ? R :
|
|
37
|
-
[T] extends [AsGuard<infer _I, infer _O, infer _E, infer R>] ? R
|
|
38
|
-
: never
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @since 1.0.0
|
|
42
|
-
*/
|
|
43
|
-
export type Error<T> = [T] extends [Guard<infer _I, infer _O, infer E, infer _R>] ? E
|
|
44
|
-
: [T] extends [AsGuard<infer _I, infer _O, infer E, infer _R>] ? E
|
|
45
|
-
: never
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @since 1.0.0
|
|
49
|
-
*/
|
|
50
|
-
export type Output<T> = [T] extends [Guard<infer _I, infer O, infer _E, infer _R>] ? O
|
|
51
|
-
: [T] extends [AsGuard<infer _I, infer O, infer _E, infer _R>] ? O
|
|
52
|
-
: never
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @since 1.0.0
|
|
57
|
-
*/
|
|
58
|
-
export interface AsGuard<in I, out O, out E = never, out R = never> {
|
|
59
|
-
readonly asGuard: () => Guard<I, O, E, R>
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @since 1.0.0
|
|
64
|
-
*/
|
|
65
|
-
export type GuardInput<I, O, E = never, R = never> = Guard<I, O, E, R> | AsGuard<I, O, E, R>
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @since 1.0.0
|
|
69
|
-
*/
|
|
70
|
-
export const getGuard = <I, O, E = never, R = never>(guard: GuardInput<I, O, E, R>): Guard<I, O, E, R> =>
|
|
71
|
-
"asGuard" in guard ? guard.asGuard() : guard
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @since 1.0.0
|
|
75
|
-
*/
|
|
76
|
-
export const compose: {
|
|
77
|
-
<O, B, E2, R2>(
|
|
78
|
-
output: GuardInput<O, B, E2, R2>
|
|
79
|
-
): <I, R, E>(input: GuardInput<I, O, E, R>) => Guard<I, B, E | E2, R | R2>
|
|
80
|
-
<I, O, E, R, B, E2, R2>(input: GuardInput<I, O, E, R>, output: GuardInput<O, B, E2, R2>): Guard<I, B, E | E2, R | R2>
|
|
81
|
-
} = dual(2, function flatMap<I, O, E, R, B, E2, R2>(
|
|
82
|
-
input: GuardInput<I, O, E, R>,
|
|
83
|
-
output: GuardInput<O, B, E2, R2>
|
|
84
|
-
): Guard<I, B, E | E2, R | R2> {
|
|
85
|
-
const g1 = getGuard(input)
|
|
86
|
-
const g2 = getGuard(output)
|
|
87
|
-
return (i) =>
|
|
88
|
-
Effect.flatMap(
|
|
89
|
-
g1(i),
|
|
90
|
-
Option.match({
|
|
91
|
-
onNone: () => Effect.succeedNone,
|
|
92
|
-
onSome: g2
|
|
93
|
-
})
|
|
94
|
-
)
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @since 1.0.0
|
|
99
|
-
*/
|
|
100
|
-
export const mapEffect: {
|
|
101
|
-
<O, B, E2, R2>(
|
|
102
|
-
f: (o: O) => Effect.Effect<B, E2, R2>
|
|
103
|
-
): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, B, E | E2, R | R2>
|
|
104
|
-
<I, O, E, R, B, E2, R2>(
|
|
105
|
-
guard: GuardInput<I, O, E, R>,
|
|
106
|
-
f: (o: O) => Effect.Effect<B, E2, R2>
|
|
107
|
-
): Guard<I, B, E | E2, R | R2>
|
|
108
|
-
} = dual(2, function mapEffect<I, O, E, R, B, E2, R2>(
|
|
109
|
-
guard: GuardInput<I, O, E, R>,
|
|
110
|
-
f: (o: O) => Effect.Effect<B, E2, R2>
|
|
111
|
-
): Guard<I, B, E | E2, R | R2> {
|
|
112
|
-
return compose(guard, (o) => Effect.asSome(f(o)))
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* @since 1.0.0
|
|
117
|
-
*/
|
|
118
|
-
export const map: {
|
|
119
|
-
<O, B>(f: (o: O) => B): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, B, E, R>
|
|
120
|
-
<I, O, E, R, B>(guard: GuardInput<I, O, E, R>, f: (o: O) => B): Guard<I, B, E, R>
|
|
121
|
-
} = dual(2, function map<I, O, E, R, B>(
|
|
122
|
-
guard: GuardInput<I, O, E, R>,
|
|
123
|
-
f: (o: O) => B
|
|
124
|
-
): Guard<I, B, E, R> {
|
|
125
|
-
return mapEffect(guard, (o) => Effect.sync(() => f(o)))
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @since 1.0.0
|
|
130
|
-
*/
|
|
131
|
-
export const tap: {
|
|
132
|
-
<O, B, E2, R2>(
|
|
133
|
-
f: (o: O) => Effect.Effect<B, E2, R2>
|
|
134
|
-
): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E | E2, R | R2>
|
|
135
|
-
<I, O, E, R, B, E2, R2>(
|
|
136
|
-
guard: GuardInput<I, O, E, R>,
|
|
137
|
-
f: (o: O) => Effect.Effect<B, E2, R2>
|
|
138
|
-
): Guard<I, O, E | E2, R | R2>
|
|
139
|
-
} = dual(2, function tap<I, O, E, R, B, E2, R2>(
|
|
140
|
-
guard: GuardInput<I, O, E, R>,
|
|
141
|
-
f: (o: O) => Effect.Effect<B, E2, R2>
|
|
142
|
-
): Guard<I, O, E | E2, R | R2> {
|
|
143
|
-
return compose(guard, (o) => Effect.as(f(o), Option.some(o)))
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* @since 1.0.0
|
|
148
|
-
*/
|
|
149
|
-
export const filterMap: {
|
|
150
|
-
<O, B>(f: (o: O) => Option.Option<B>): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, B, E, R>
|
|
151
|
-
<I, O, E, R, B>(guard: GuardInput<I, O, E, R>, f: (o: O) => Option.Option<B>): Guard<I, B, E, R>
|
|
152
|
-
} = dual(
|
|
153
|
-
2,
|
|
154
|
-
<I, O, E, R, B>(guard: GuardInput<I, O, E, R>, f: (o: O) => Option.Option<B>): Guard<I, B, E, R> => {
|
|
155
|
-
const g = getGuard(guard)
|
|
156
|
-
return (i) => Effect.map(g(i), Option.filterMap(f))
|
|
157
|
-
}
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* @since 1.0.0
|
|
162
|
-
*/
|
|
163
|
-
export const filter: {
|
|
164
|
-
<O, O2 extends O>(predicate: (o: O) => o is O2): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, R>
|
|
165
|
-
<O>(predicate: (o: O) => boolean): <I, R, E>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, R>
|
|
166
|
-
<I, O, E, R, O2 extends O>(guard: GuardInput<I, O, E, R>, predicate: (o: O) => o is O2): Guard<I, O, E, R>
|
|
167
|
-
<I, O, E, R>(guard: GuardInput<I, O, E, R>, predicate: (o: O) => boolean): Guard<I, O, E, R>
|
|
168
|
-
} = dual(
|
|
169
|
-
2,
|
|
170
|
-
<I, O, E, R>(guard: GuardInput<I, O, E, R>, predicate: (o: O) => boolean): Guard<I, O, E, R> => {
|
|
171
|
-
const g = getGuard(guard)
|
|
172
|
-
return (i) => Effect.map(g(i), Option.filter(predicate))
|
|
173
|
-
}
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* @since 1.0.0
|
|
178
|
-
*/
|
|
179
|
-
export function any<const GS extends Readonly<Record<string, GuardInput<any, any, any, any>>>>(
|
|
180
|
-
guards: GS
|
|
181
|
-
): Guard<AnyInput<GS>, AnyOutput<GS>, Guard.Error<GS[keyof GS]>, Guard.Context<GS[keyof GS]>> {
|
|
182
|
-
const entries = Object.entries(guards).map(([k, v]) => [k, getGuard(v)] as const)
|
|
183
|
-
return (i: AnyInput<GS>) =>
|
|
184
|
-
Effect.gen(function*() {
|
|
185
|
-
for (const [_tag, guard] of entries) {
|
|
186
|
-
const match = yield* guard(i)
|
|
187
|
-
if (Option.isSome(match)) {
|
|
188
|
-
return Option.some({ _tag, value: match.value } as AnyOutput<GS>)
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return Option.none()
|
|
192
|
-
})
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* @since 1.0.0
|
|
197
|
-
*/
|
|
198
|
-
export type AnyInput<GS extends Readonly<Record<string, GuardInput<any, any, any, any>>>> = UnionToIntersection<
|
|
199
|
-
Guard.Input<GS[keyof GS]>
|
|
200
|
-
>
|
|
201
|
-
|
|
202
|
-
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* @since 1.0.0
|
|
206
|
-
*/
|
|
207
|
-
export type AnyOutput<GS extends Readonly<Record<string, GuardInput<any, any, any, any>>>> = [
|
|
208
|
-
{
|
|
209
|
-
[K in keyof GS]: { readonly _tag: K; readonly value: Guard.Output<GS[K]> }
|
|
210
|
-
}[keyof GS]
|
|
211
|
-
] extends [infer R] ? R : never
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* @since 1.0.0
|
|
215
|
-
*/
|
|
216
|
-
export function liftPredicate<A, B extends A>(predicate: Predicate.Refinement<A, B>): Guard<A, B>
|
|
217
|
-
export function liftPredicate<A>(predicate: Predicate.Predicate<A>): Guard<A, A>
|
|
218
|
-
export function liftPredicate<A>(predicate: Predicate.Predicate<A>): Guard<A, A> {
|
|
219
|
-
return (a) => Effect.sync(() => (predicate(a) ? Option.some(a) : Option.none()))
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* @since 1.0.0
|
|
224
|
-
*/
|
|
225
|
-
export const catchAllCause: {
|
|
226
|
-
<E, O2, E2, R2>(
|
|
227
|
-
f: (e: Cause.Cause<E>) => Effect.Effect<O2, E2, R2>
|
|
228
|
-
): <I, O, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O | O2, E2, R | R2>
|
|
229
|
-
<I, O, E, R, O2, E2, R2>(
|
|
230
|
-
guard: GuardInput<I, O, E, R>,
|
|
231
|
-
f: (e: Cause.Cause<E>) => Effect.Effect<O2, E2, R2>
|
|
232
|
-
): Guard<I, O | O2, E2, R | R2>
|
|
233
|
-
} = dual(2, function catchAllCause<I, O, E, R, O2, E2, R2>(
|
|
234
|
-
guard: GuardInput<I, O, E, R>,
|
|
235
|
-
f: (e: Cause.Cause<E>) => Effect.Effect<O2, E2, R2>
|
|
236
|
-
): Guard<I, O | O2, E2, R | R2> {
|
|
237
|
-
const g = getGuard(guard)
|
|
238
|
-
return (i) => Effect.catchAllCause(g(i), (a) => Effect.asSome(f(a)))
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* @since 1.0.0
|
|
243
|
-
*/
|
|
244
|
-
export const catchAll: {
|
|
245
|
-
<E, O2, E2, R2>(
|
|
246
|
-
f: (e: E) => Effect.Effect<O2, E2, R2>
|
|
247
|
-
): <I, O, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O | O2, E2, R | R2>
|
|
248
|
-
<I, O, E, R, O2, E2, R2>(
|
|
249
|
-
guard: GuardInput<I, O, E, R>,
|
|
250
|
-
f: (e: E) => Effect.Effect<O2, E2, R2>
|
|
251
|
-
): Guard<I, O | O2, E2, R | R2>
|
|
252
|
-
} = dual(2, function catchAll<I, O, E, R, O2, E2, R2>(
|
|
253
|
-
guard: GuardInput<I, O, E, R>,
|
|
254
|
-
f: (e: E) => Effect.Effect<O2, E2, R2>
|
|
255
|
-
): Guard<I, O | O2, E2, R | R2> {
|
|
256
|
-
const g = getGuard(guard)
|
|
257
|
-
return (i) => Effect.catchAll(g(i), (a) => Effect.asSome(f(a)))
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @since 1.0.0
|
|
262
|
-
*/
|
|
263
|
-
export const catchTag: {
|
|
264
|
-
<E, K extends E extends { _tag: string } ? E["_tag"] : never, O2, E2, R2>(
|
|
265
|
-
tag: K,
|
|
266
|
-
f: (e: Extract<E, { _tag: K }>) => Effect.Effect<O2, E2, R2>
|
|
267
|
-
): <I, O, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O | O2, E2 | Exclude<E, { _tag: K }>, R | R2>
|
|
268
|
-
|
|
269
|
-
<I, O, E, R, K extends E extends { _tag: string } ? E["_tag"] : never, O2, E2, R2>(
|
|
270
|
-
guard: GuardInput<I, O, E, R>,
|
|
271
|
-
tag: K,
|
|
272
|
-
f: (e: Extract<E, { _tag: K }>) => Effect.Effect<O2, E2, R2>
|
|
273
|
-
): Guard<I, O | O2, E2 | Exclude<E, { _tag: K }>, R | R2>
|
|
274
|
-
} = dual(
|
|
275
|
-
3,
|
|
276
|
-
function catchTag<I, O, E, R, K extends E extends { _tag: string } ? E["_tag"] : never, O2, E2, R2>(
|
|
277
|
-
guard: GuardInput<I, O, E, R>,
|
|
278
|
-
tag: K,
|
|
279
|
-
f: (e: Extract<E, { _tag: K }>) => Effect.Effect<O2, E2, R2>
|
|
280
|
-
): Guard<I, O | O2, Exclude<E, { _tag: K }> | E2, R | R2> {
|
|
281
|
-
const g = getGuard(guard)
|
|
282
|
-
return (i): Effect.Effect<Option.Option<O | O2>, E2 | Exclude<E, { _tag: K }>, R | R2> =>
|
|
283
|
-
Effect.catchTag(g(i), tag, (e) => Effect.asSome(f(e)))
|
|
284
|
-
}
|
|
285
|
-
)
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* @since 1.0.0
|
|
289
|
-
*/
|
|
290
|
-
export const provide: {
|
|
291
|
-
<R2>(
|
|
292
|
-
provided: Context.Context<R2>
|
|
293
|
-
): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, Exclude<R, R2>>
|
|
294
|
-
<R2>(provided: Runtime.Runtime<R2>): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, Exclude<R, R2>>
|
|
295
|
-
<R2, E2, R3>(
|
|
296
|
-
provided: Layer.Layer<R2, E2, R3>
|
|
297
|
-
): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E | E2, Exclude<R, R2> | R3>
|
|
298
|
-
|
|
299
|
-
<I, O, E, R, R2>(guard: GuardInput<I, O, E, R>, provided: Context.Context<R2>): Guard<I, O, E, Exclude<R, R2>>
|
|
300
|
-
<I, O, E, R, R2>(guard: GuardInput<I, O, E, R>, provided: Runtime.Runtime<R2>): Guard<I, O, E, Exclude<R, R2>>
|
|
301
|
-
<I, O, E, R, R2, E2, R3>(
|
|
302
|
-
guard: GuardInput<I, O, E, R>,
|
|
303
|
-
provided: Layer.Layer<R2, E2, R3>
|
|
304
|
-
): Guard<I, O, E | E2, Exclude<R, R2> | R3>
|
|
305
|
-
} = dual(2, function provide<I, O, E, R, R2>(
|
|
306
|
-
guard: GuardInput<I, O, E, R>,
|
|
307
|
-
provided: Context.Context<R2>
|
|
308
|
-
): Guard<I, O, E, Exclude<R, R2>> {
|
|
309
|
-
const g = getGuard(guard)
|
|
310
|
-
return (i) => Effect.provide(g(i), provided)
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* @since 1.0.0
|
|
315
|
-
*/
|
|
316
|
-
export const provideService: {
|
|
317
|
-
<Id, S>(
|
|
318
|
-
tag: Context.Tag<Id, S>,
|
|
319
|
-
service: S
|
|
320
|
-
): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E, Exclude<R, Id>>
|
|
321
|
-
<I, O, E, R, Id, S>(
|
|
322
|
-
guard: GuardInput<I, O, E, R>,
|
|
323
|
-
tag: Context.Tag<Id, S>,
|
|
324
|
-
service: S
|
|
325
|
-
): Guard<I, O, E, Exclude<R, Id>>
|
|
326
|
-
} = dual(3, function provideService<I, O, E, R, Id, S>(
|
|
327
|
-
guard: GuardInput<I, O, E, R>,
|
|
328
|
-
tag: Context.Tag<Id, S>,
|
|
329
|
-
service: S
|
|
330
|
-
): Guard<I, O, E, Exclude<R, Id>> {
|
|
331
|
-
const g = getGuard(guard)
|
|
332
|
-
return (i) => Effect.provideService(g(i), tag, service)
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* @since 1.0.0
|
|
337
|
-
*/
|
|
338
|
-
export const provideServiceEffect: {
|
|
339
|
-
<Id, S, E2, R2>(
|
|
340
|
-
tag: Context.Tag<Id, S>,
|
|
341
|
-
service: Effect.Effect<S, E2, R2>
|
|
342
|
-
): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O, E | E2, Exclude<R, Id> | R2>
|
|
343
|
-
<I, O, E, R, Id, S, E2, R2>(
|
|
344
|
-
guard: GuardInput<I, O, E, R>,
|
|
345
|
-
tag: Context.Tag<Id, S>,
|
|
346
|
-
service: Effect.Effect<S, E2, R2>
|
|
347
|
-
): Guard<I, O, E | E2, Exclude<R, Id> | R2>
|
|
348
|
-
} = dual(3, function provideServiceEffect<I, O, E, R, Id, S, E2, R2>(
|
|
349
|
-
guard: GuardInput<I, O, E, R>,
|
|
350
|
-
tag: Context.Tag<Id, S>,
|
|
351
|
-
service: Effect.Effect<S, E2, R2>
|
|
352
|
-
): Guard<I, O, E | E2, Exclude<R, Id> | R2> {
|
|
353
|
-
const g = getGuard(guard)
|
|
354
|
-
return (i) => Effect.provideServiceEffect(g(i), tag, service)
|
|
355
|
-
})
|
|
356
|
-
|
|
357
|
-
const parseOptions: ParseOptions = { errors: "all", onExcessProperty: "ignore" }
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* @since 1.0.0
|
|
361
|
-
*/
|
|
362
|
-
export function fromSchemaDecode<A, I, R>(schema: Schema.Schema<A, I, R>): Guard<I, A, ParseResult.ParseError, R> {
|
|
363
|
-
const decode_ = Schema.decode(schema)
|
|
364
|
-
return (i: I) => Effect.asSome(decode_(i, parseOptions))
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* @since 1.0.0
|
|
369
|
-
*/
|
|
370
|
-
export function fromSchemaEncode<A, I, R>(schema: Schema.Schema<A, I, R>): Guard<A, I, ParseResult.ParseError, R> {
|
|
371
|
-
const encode_ = Schema.encode(schema)
|
|
372
|
-
return (a: A) => Effect.asSome(encode_(a, parseOptions))
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @since 1.0.0
|
|
377
|
-
*/
|
|
378
|
-
export const decode: {
|
|
379
|
-
<A, O, R2>(
|
|
380
|
-
schema: Schema.Schema<A, O, R2>
|
|
381
|
-
): <I, E = never, R = never>(guard: GuardInput<I, O, E, R>) => Guard<I, A, ParseResult.ParseError | E, R | R2>
|
|
382
|
-
|
|
383
|
-
<I, O, E, R, A, R2>(
|
|
384
|
-
guard: GuardInput<I, O, E, R>,
|
|
385
|
-
schema: Schema.Schema<A, O, R2>
|
|
386
|
-
): Guard<I, A, ParseResult.ParseError | E, R | R2>
|
|
387
|
-
} = dual(2, function decode<I, O, E, R, A, R2>(
|
|
388
|
-
guard: GuardInput<I, O, E, R>,
|
|
389
|
-
schema: Schema.Schema<A, O, R2>
|
|
390
|
-
): Guard<I, A, E | ParseResult.ParseError, R | R2> {
|
|
391
|
-
return compose(guard, fromSchemaDecode(schema))
|
|
392
|
-
})
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* @since 1.0.0
|
|
396
|
-
*/
|
|
397
|
-
export const encode: {
|
|
398
|
-
<O, A, R2>(
|
|
399
|
-
schema: Schema.Schema<O, A, R2>
|
|
400
|
-
): <I, E = never, R = never>(guard: GuardInput<I, O, E, R>) => Guard<I, A, ParseResult.ParseError | E, R | R2>
|
|
401
|
-
|
|
402
|
-
<I, O, E, R, A, R2>(
|
|
403
|
-
guard: GuardInput<I, O, E, R>,
|
|
404
|
-
schema: Schema.Schema<O, A, R2>
|
|
405
|
-
): Guard<I, A, ParseResult.ParseError | E, R | R2>
|
|
406
|
-
} = dual(2, function encode<I, O, E, R, A, R2>(
|
|
407
|
-
guard: GuardInput<I, O, E, R>,
|
|
408
|
-
schema: Schema.Schema<O, A, R2>
|
|
409
|
-
): Guard<I, A, E | ParseResult.ParseError, R | R2> {
|
|
410
|
-
return compose(guard, fromSchemaEncode(schema))
|
|
411
|
-
})
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* @since 1.0.0
|
|
415
|
-
*/
|
|
416
|
-
const let_: {
|
|
417
|
-
<K extends PropertyKey, B>(
|
|
418
|
-
key: K,
|
|
419
|
-
value: B
|
|
420
|
-
): <I, O, E = never, R = never>(guard: Guard<I, O, E, R>) => Guard<I, O & { [k in K]: B }, E, R>
|
|
421
|
-
|
|
422
|
-
<I, O, E, R, K extends PropertyKey, B>(
|
|
423
|
-
guard: Guard<I, O, E, R>,
|
|
424
|
-
key: K,
|
|
425
|
-
value: B
|
|
426
|
-
): Guard<I, O & { [k in K]: B }, E, R>
|
|
427
|
-
} = dual(3, function attachProperty<I, O, E, R, K extends PropertyKey, B>(
|
|
428
|
-
guard: Guard<I, O, E, R>,
|
|
429
|
-
key: K,
|
|
430
|
-
value: B
|
|
431
|
-
): Guard<I, O & { [k in K]: B }, E, R> {
|
|
432
|
-
return map(guard, (a) => ({ ...a, [key]: value } as O & { [k in K]: B }))
|
|
433
|
-
})
|
|
434
|
-
|
|
435
|
-
export {
|
|
436
|
-
/**
|
|
437
|
-
* @since 1.0.0
|
|
438
|
-
*/
|
|
439
|
-
let_ as let
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* @since 1.0.0
|
|
444
|
-
*/
|
|
445
|
-
export const addTag: {
|
|
446
|
-
<B>(
|
|
447
|
-
value: B
|
|
448
|
-
): <I, O, E = never, R = never>(guard: GuardInput<I, O, E, R>) => Guard<I, O & { readonly _tag: B }, E, R>
|
|
449
|
-
|
|
450
|
-
<I, O, E, R, B>(
|
|
451
|
-
guard: GuardInput<I, O, E, R>,
|
|
452
|
-
value: B
|
|
453
|
-
): Guard<I, O & { readonly _tag: B }, E, R>
|
|
454
|
-
} = dual(2, function attachProperty<I, O, E, R, B>(
|
|
455
|
-
guard: GuardInput<I, O, E, R>,
|
|
456
|
-
value: B
|
|
457
|
-
): Guard<I, O & { readonly _tag: B }, E, R> {
|
|
458
|
-
return map(guard, (a) => ({ ...a, _tag: value } as O & { readonly _tag: B }))
|
|
459
|
-
})
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* @since 1.0.0
|
|
463
|
-
*/
|
|
464
|
-
export const bindTo: {
|
|
465
|
-
<K extends PropertyKey>(key: K): <I, O, E, R>(guard: GuardInput<I, O, E, R>) => Guard<I, { [k in K]: O }, E, R>
|
|
466
|
-
<I, O, E, R, K extends PropertyKey>(guard: GuardInput<I, O, E, R>, key: K): Guard<I, { [k in K]: O }, E, R>
|
|
467
|
-
} = dual(2, <I, O, E, R, K extends PropertyKey>(
|
|
468
|
-
guard: GuardInput<I, O, E, R>,
|
|
469
|
-
key: K
|
|
470
|
-
): Guard<I, { [k in K]: O }, E, R> => map(guard, (a) => ({ [key]: a } as { [k in K]: O })))
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* @since 1.0.0
|
|
474
|
-
*/
|
|
475
|
-
export const bind: {
|
|
476
|
-
<I, O, E, R, K extends PropertyKey, B, E2, R2>(
|
|
477
|
-
key: K,
|
|
478
|
-
f: GuardInput<O, B, E2, R2>
|
|
479
|
-
): (guard: GuardInput<I, O, E, R>) => Guard<I, O & { [k in K]: B }, E | E2, R | R2>
|
|
480
|
-
|
|
481
|
-
<I, O, E, R, K extends PropertyKey, B, E2, R2>(
|
|
482
|
-
guard: GuardInput<I, O, E, R>,
|
|
483
|
-
key: K,
|
|
484
|
-
f: GuardInput<O, B, E2, R2>
|
|
485
|
-
): Guard<I, O & { [k in K]: B }, E | E2, R | R2>
|
|
486
|
-
} = dual(3, function bind<I, O, E, R, K extends PropertyKey, B, E2, R2>(
|
|
487
|
-
guard: GuardInput<I, O, E, R>,
|
|
488
|
-
key: K,
|
|
489
|
-
f: GuardInput<O, B, E2, R2>
|
|
490
|
-
): Guard<I, O & { [k in K]: B }, E | E2, R | R2> {
|
|
491
|
-
const f_ = bindTo(f, key)
|
|
492
|
-
|
|
493
|
-
return compose(guard, (o) => Effect.map(f_(o), Option.map((b) => ({ ...o, ...b }))))
|
|
494
|
-
})
|
|
1
|
+
export * from './ExtensibleFunction.js'
|
|
2
|
+
export * from './Guard.js'
|
|
3
|
+
export * from './Guardable.js'
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": [
|
|
7
|
+
"ES2022",
|
|
8
|
+
"DOM"
|
|
9
|
+
],
|
|
10
|
+
"strict": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"declaration": true,
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"outDir": "dist",
|
|
17
|
+
"rootDir": "src"
|
|
18
|
+
},
|
|
19
|
+
"include": [
|
|
20
|
+
"src/**/*"
|
|
21
|
+
],
|
|
22
|
+
"exclude": [
|
|
23
|
+
"node_modules",
|
|
24
|
+
"dist",
|
|
25
|
+
"**/*.test.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023-present The Contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|