@nlozgachev/pipelined 0.6.4
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 +85 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/src/Composition/compose.js +3 -0
- package/esm/src/Composition/converge.js +3 -0
- package/esm/src/Composition/curry.js +42 -0
- package/esm/src/Composition/flip.js +20 -0
- package/esm/src/Composition/flow.js +8 -0
- package/esm/src/Composition/fn.js +85 -0
- package/esm/src/Composition/index.js +13 -0
- package/esm/src/Composition/juxt.js +3 -0
- package/esm/src/Composition/memoize.js +66 -0
- package/esm/src/Composition/not.js +25 -0
- package/esm/src/Composition/on.js +12 -0
- package/esm/src/Composition/pipe.js +3 -0
- package/esm/src/Composition/tap.js +33 -0
- package/esm/src/Composition/uncurry.js +32 -0
- package/esm/src/Core/Arr.js +463 -0
- package/esm/src/Core/Deferred.js +26 -0
- package/esm/src/Core/InternalTypes.js +1 -0
- package/esm/src/Core/Lens.js +98 -0
- package/esm/src/Core/Option.js +186 -0
- package/esm/src/Core/Optional.js +160 -0
- package/esm/src/Core/Reader.js +134 -0
- package/esm/src/Core/Rec.js +167 -0
- package/esm/src/Core/RemoteData.js +206 -0
- package/esm/src/Core/Result.js +164 -0
- package/esm/src/Core/Task.js +187 -0
- package/esm/src/Core/TaskOption.js +105 -0
- package/esm/src/Core/TaskResult.js +125 -0
- package/esm/src/Core/TaskValidation.js +101 -0
- package/esm/src/Core/These.js +241 -0
- package/esm/src/Core/Validation.js +214 -0
- package/esm/src/Core/index.js +15 -0
- package/esm/src/Types/Brand.js +28 -0
- package/esm/src/Types/NonEmptyList.js +14 -0
- package/esm/src/Types/index.js +2 -0
- package/package.json +61 -0
- package/script/mod.js +19 -0
- package/script/package.json +3 -0
- package/script/src/Composition/compose.js +6 -0
- package/script/src/Composition/converge.js +6 -0
- package/script/src/Composition/curry.js +48 -0
- package/script/src/Composition/flip.js +24 -0
- package/script/src/Composition/flow.js +11 -0
- package/script/src/Composition/fn.js +98 -0
- package/script/src/Composition/index.js +29 -0
- package/script/src/Composition/juxt.js +6 -0
- package/script/src/Composition/memoize.js +71 -0
- package/script/src/Composition/not.js +29 -0
- package/script/src/Composition/on.js +16 -0
- package/script/src/Composition/pipe.js +6 -0
- package/script/src/Composition/tap.js +37 -0
- package/script/src/Composition/uncurry.js +38 -0
- package/script/src/Core/Arr.js +466 -0
- package/script/src/Core/Deferred.js +29 -0
- package/script/src/Core/InternalTypes.js +2 -0
- package/script/src/Core/Lens.js +101 -0
- package/script/src/Core/Option.js +189 -0
- package/script/src/Core/Optional.js +163 -0
- package/script/src/Core/Reader.js +137 -0
- package/script/src/Core/Rec.js +170 -0
- package/script/src/Core/RemoteData.js +209 -0
- package/script/src/Core/Result.js +167 -0
- package/script/src/Core/Task.js +190 -0
- package/script/src/Core/TaskOption.js +108 -0
- package/script/src/Core/TaskResult.js +128 -0
- package/script/src/Core/TaskValidation.js +104 -0
- package/script/src/Core/These.js +244 -0
- package/script/src/Core/Validation.js +217 -0
- package/script/src/Core/index.js +31 -0
- package/script/src/Types/Brand.js +31 -0
- package/script/src/Types/NonEmptyList.js +18 -0
- package/script/src/Types/index.js +18 -0
- package/types/mod.d.ts +4 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/Composition/compose.d.ts +33 -0
- package/types/src/Composition/compose.d.ts.map +1 -0
- package/types/src/Composition/converge.d.ts +21 -0
- package/types/src/Composition/converge.d.ts.map +1 -0
- package/types/src/Composition/curry.d.ts +43 -0
- package/types/src/Composition/curry.d.ts.map +1 -0
- package/types/src/Composition/flip.d.ts +21 -0
- package/types/src/Composition/flip.d.ts.map +1 -0
- package/types/src/Composition/flow.d.ts +56 -0
- package/types/src/Composition/flow.d.ts.map +1 -0
- package/types/src/Composition/fn.d.ts +76 -0
- package/types/src/Composition/fn.d.ts.map +1 -0
- package/types/src/Composition/index.d.ts +14 -0
- package/types/src/Composition/index.d.ts.map +1 -0
- package/types/src/Composition/juxt.d.ts +18 -0
- package/types/src/Composition/juxt.d.ts.map +1 -0
- package/types/src/Composition/memoize.d.ts +46 -0
- package/types/src/Composition/memoize.d.ts.map +1 -0
- package/types/src/Composition/not.d.ts +26 -0
- package/types/src/Composition/not.d.ts.map +1 -0
- package/types/src/Composition/on.d.ts +13 -0
- package/types/src/Composition/on.d.ts.map +1 -0
- package/types/src/Composition/pipe.d.ts +56 -0
- package/types/src/Composition/pipe.d.ts.map +1 -0
- package/types/src/Composition/tap.d.ts +31 -0
- package/types/src/Composition/tap.d.ts.map +1 -0
- package/types/src/Composition/uncurry.d.ts +54 -0
- package/types/src/Composition/uncurry.d.ts.map +1 -0
- package/types/src/Core/Arr.d.ts +355 -0
- package/types/src/Core/Arr.d.ts.map +1 -0
- package/types/src/Core/Deferred.d.ts +49 -0
- package/types/src/Core/Deferred.d.ts.map +1 -0
- package/types/src/Core/InternalTypes.d.ts +20 -0
- package/types/src/Core/InternalTypes.d.ts.map +1 -0
- package/types/src/Core/Lens.d.ts +118 -0
- package/types/src/Core/Lens.d.ts.map +1 -0
- package/types/src/Core/Option.d.ts +205 -0
- package/types/src/Core/Option.d.ts.map +1 -0
- package/types/src/Core/Optional.d.ts +158 -0
- package/types/src/Core/Optional.d.ts.map +1 -0
- package/types/src/Core/Reader.d.ts +156 -0
- package/types/src/Core/Reader.d.ts.map +1 -0
- package/types/src/Core/Rec.d.ts +121 -0
- package/types/src/Core/Rec.d.ts.map +1 -0
- package/types/src/Core/RemoteData.d.ts +192 -0
- package/types/src/Core/RemoteData.d.ts.map +1 -0
- package/types/src/Core/Result.d.ts +176 -0
- package/types/src/Core/Result.d.ts.map +1 -0
- package/types/src/Core/Task.d.ts +189 -0
- package/types/src/Core/Task.d.ts.map +1 -0
- package/types/src/Core/TaskOption.d.ts +120 -0
- package/types/src/Core/TaskOption.d.ts.map +1 -0
- package/types/src/Core/TaskResult.d.ts +117 -0
- package/types/src/Core/TaskResult.d.ts.map +1 -0
- package/types/src/Core/TaskValidation.d.ts +119 -0
- package/types/src/Core/TaskValidation.d.ts.map +1 -0
- package/types/src/Core/These.d.ts +221 -0
- package/types/src/Core/These.d.ts.map +1 -0
- package/types/src/Core/Validation.d.ts +213 -0
- package/types/src/Core/Validation.d.ts.map +1 -0
- package/types/src/Core/index.d.ts +16 -0
- package/types/src/Core/index.d.ts.map +1 -0
- package/types/src/Types/Brand.d.ts +52 -0
- package/types/src/Types/Brand.d.ts.map +1 -0
- package/types/src/Types/NonEmptyList.d.ts +29 -0
- package/types/src/Types/NonEmptyList.d.ts.map +1 -0
- package/types/src/Types/index.d.ts +3 -0
- package/types/src/Types/index.d.ts.map +1 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Optional } from "./Optional.js";
|
|
2
|
+
/**
|
|
3
|
+
* Lens<S, A> focuses on a single value A inside a structure S, providing
|
|
4
|
+
* a composable way to read and immutably update nested data.
|
|
5
|
+
*
|
|
6
|
+
* A Lens always succeeds: the focused value is guaranteed to exist.
|
|
7
|
+
* For optional or indexed focuses, use Optional<S, A>.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* type Address = { city: string; zip: string };
|
|
12
|
+
* type User = { name: string; address: Address };
|
|
13
|
+
*
|
|
14
|
+
* const addressLens = Lens.prop<User>()("address");
|
|
15
|
+
* const cityLens = Lens.prop<Address>()("city");
|
|
16
|
+
* const userCityLens = pipe(addressLens, Lens.andThen(cityLens));
|
|
17
|
+
*
|
|
18
|
+
* pipe(user, Lens.get(userCityLens)); // "Berlin"
|
|
19
|
+
* pipe(user, Lens.set(userCityLens)("Hamburg")); // new User with city updated
|
|
20
|
+
* pipe(user, Lens.modify(userCityLens)(c => c.toUpperCase())); // "BERLIN"
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export type Lens<S, A> = {
|
|
24
|
+
readonly get: (s: S) => A;
|
|
25
|
+
readonly set: (a: A) => (s: S) => S;
|
|
26
|
+
};
|
|
27
|
+
export declare namespace Lens {
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a Lens from a getter and a setter.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const nameLens = Lens.make(
|
|
34
|
+
* (user: User) => user.name,
|
|
35
|
+
* (name) => (user) => ({ ...user, name }),
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const make: <S, A>(get: (s: S) => A, set: (a: A) => (s: S) => S) => Lens<S, A>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a Lens that focuses on a property of an object.
|
|
42
|
+
* Call with the structure type first, then the key.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const nameLens = Lens.prop<User>()("name");
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
const prop: <S>() => <K extends keyof S>(key: K) => Lens<S, S[K]>;
|
|
50
|
+
/**
|
|
51
|
+
* Reads the focused value from a structure.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* pipe(user, Lens.get(nameLens)); // "Alice"
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
const get: <S, A>(lens: Lens<S, A>) => (s: S) => A;
|
|
59
|
+
/**
|
|
60
|
+
* Replaces the focused value within a structure, returning a new structure.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* pipe(user, Lens.set(nameLens)("Bob")); // new User with name "Bob"
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
const set: <S, A>(lens: Lens<S, A>) => (a: A) => (s: S) => S;
|
|
68
|
+
/**
|
|
69
|
+
* Applies a function to the focused value, returning a new structure.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* pipe(user, Lens.modify(nameLens)(n => n.toUpperCase())); // "ALICE"
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
const modify: <S, A>(lens: Lens<S, A>) => (f: (a: A) => A) => (s: S) => S;
|
|
77
|
+
/**
|
|
78
|
+
* Composes two Lenses: focuses through the outer, then through the inner.
|
|
79
|
+
* Use in a pipe chain to build up a deep focus step by step.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const userCityLens = pipe(
|
|
84
|
+
* Lens.prop<User>()("address"),
|
|
85
|
+
* Lens.andThen(Lens.prop<Address>()("city")),
|
|
86
|
+
* );
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
const andThen: <A, B>(inner: Lens<A, B>) => <S>(outer: Lens<S, A>) => Lens<S, B>;
|
|
90
|
+
/**
|
|
91
|
+
* Composes a Lens with an Optional, producing an Optional.
|
|
92
|
+
* Use when the next step in the focus is optional (may be absent).
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const userBioOpt = pipe(
|
|
97
|
+
* Lens.prop<User>()("profile"),
|
|
98
|
+
* Lens.andThenOptional(Optional.prop<Profile>()("bio")),
|
|
99
|
+
* );
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
const andThenOptional: <A, B>(inner: Optional<A, B>) => <S>(outer: Lens<S, A>) => Optional<S, B>;
|
|
103
|
+
/**
|
|
104
|
+
* Converts a Lens to an Optional. Every Lens is a valid Optional
|
|
105
|
+
* whose get always returns Some.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* pipe(
|
|
110
|
+
* Lens.prop<User>()("address"),
|
|
111
|
+
* Lens.toOptional,
|
|
112
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
113
|
+
* );
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
const toOptional: <S, A>(lens: Lens<S, A>) => Optional<S, A>;
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=Lens.d.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AI,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,KAAG,CACjD,CAAC;IAE9B;;;;;;;;;;;OAWG;IACI,MAAM,OAAO,GAAI,CAAC,EAAE,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAIlF,CAAC;IAEJ;;;;;;;;;;;OAWG;IACI,MAAM,eAAe,GACzB,CAAC,EAAE,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAGpE,CAAC;IAEL;;;;;;;;;;;;OAYG;IACI,MAAM,UAAU,GAAI,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAG/D,CAAC;CACJ"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { WithKind, WithValue } from "./InternalTypes.js";
|
|
2
|
+
import { Result } from "./Result.js";
|
|
3
|
+
/**
|
|
4
|
+
* Option represents an optional value: every Option is either Some (contains a value) or None (empty).
|
|
5
|
+
* Use Option instead of null/undefined to make optionality explicit and composable.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const findUser = (id: string): Option<User> =>
|
|
10
|
+
* users.has(id) ? Option.some(users.get(id)!) : Option.none();
|
|
11
|
+
*
|
|
12
|
+
* pipe(
|
|
13
|
+
* findUser("123"),
|
|
14
|
+
* Option.map(user => user.name),
|
|
15
|
+
* Option.getOrElse("Unknown")
|
|
16
|
+
* );
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type Option<T> = Some<T> | None;
|
|
20
|
+
export type Some<A> = WithKind<"Some"> & WithValue<A>;
|
|
21
|
+
export type None = WithKind<"None">;
|
|
22
|
+
export declare namespace Option {
|
|
23
|
+
/**
|
|
24
|
+
* Creates a Some containing the given value.
|
|
25
|
+
*/
|
|
26
|
+
const some: <A>(value: A) => Some<A>;
|
|
27
|
+
/**
|
|
28
|
+
* Type guard that checks if a Option is Some.
|
|
29
|
+
*/
|
|
30
|
+
const isSome: <A>(data: Option<A>) => data is Some<A>;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a None (empty Option).
|
|
33
|
+
*/
|
|
34
|
+
const none: () => None;
|
|
35
|
+
/**
|
|
36
|
+
* Type guard that checks if a Option is None.
|
|
37
|
+
*/
|
|
38
|
+
const isNone: <A>(data: Option<A>) => data is None;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a Option from a nullable value.
|
|
41
|
+
* Returns None if the value is null or undefined, Some otherwise.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* Option.fromNullable(null); // None
|
|
46
|
+
* Option.fromNullable(42); // Some(42)
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
const fromNullable: <A>(value: A | null | undefined) => Option<A>;
|
|
50
|
+
/**
|
|
51
|
+
* Extracts the value from a Option, returning null if None.
|
|
52
|
+
*/
|
|
53
|
+
const toNullable: <A>(data: Option<A>) => A | null;
|
|
54
|
+
/**
|
|
55
|
+
* Extracts the value from a Option, returning undefined if None.
|
|
56
|
+
*/
|
|
57
|
+
const toUndefined: <A>(data: Option<A>) => A | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a Option from a possibly undefined value.
|
|
60
|
+
* Returns None if undefined, Some otherwise.
|
|
61
|
+
*/
|
|
62
|
+
const fromUndefined: <A>(value: A | undefined) => Option<A>;
|
|
63
|
+
/**
|
|
64
|
+
* Converts an Option to a Result.
|
|
65
|
+
* Some becomes Ok, None becomes Err with the provided error.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* pipe(
|
|
70
|
+
* Option.some(42),
|
|
71
|
+
* Option.toResult(() => "Value was missing")
|
|
72
|
+
* ); // Ok(42)
|
|
73
|
+
*
|
|
74
|
+
* pipe(
|
|
75
|
+
* Option.none(),
|
|
76
|
+
* Option.toResult(() => "Value was missing")
|
|
77
|
+
* ); // Err("Value was missing")
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
const toResult: <E>(onNone: () => E) => <A>(data: Option<A>) => Result<E, A>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates an Option from a Result.
|
|
83
|
+
* Ok becomes Some, Err becomes None (the error is discarded).
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* Option.fromResult(Result.ok(42)); // Some(42)
|
|
88
|
+
* Option.fromResult(Result.err("oops")); // None
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
const fromResult: <E, A>(data: Result<E, A>) => Option<A>;
|
|
92
|
+
/**
|
|
93
|
+
* Transforms the value inside a Option if it exists.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* pipe(Option.some(5), Option.map(n => n * 2)); // Some(10)
|
|
98
|
+
* pipe(Option.none(), Option.map(n => n * 2)); // None
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
const map: <A, B>(f: (a: A) => B) => (data: Option<A>) => Option<B>;
|
|
102
|
+
/**
|
|
103
|
+
* Chains Option computations. If the first is Some, passes the value to f.
|
|
104
|
+
* If the first is None, propagates None.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const parseNumber = (s: string): Option<number> => {
|
|
109
|
+
* const n = parseInt(s, 10);
|
|
110
|
+
* return isNaN(n) ? Option.none() : Option.some(n);
|
|
111
|
+
* };
|
|
112
|
+
*
|
|
113
|
+
* pipe(Option.some("42"), Option.chain(parseNumber)); // Some(42)
|
|
114
|
+
* pipe(Option.some("abc"), Option.chain(parseNumber)); // None
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
const chain: <A, B>(f: (a: A) => Option<B>) => (data: Option<A>) => Option<B>;
|
|
118
|
+
/**
|
|
119
|
+
* Extracts the value from a Option by providing handlers for both cases.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* pipe(
|
|
124
|
+
* Option.some(5),
|
|
125
|
+
* Option.fold(
|
|
126
|
+
* () => "No value",
|
|
127
|
+
* n => `Value: ${n}`
|
|
128
|
+
* )
|
|
129
|
+
* ); // "Value: 5"
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
const fold: <A, B>(onNone: () => B, onSome: (a: A) => B) => (data: Option<A>) => B;
|
|
133
|
+
/**
|
|
134
|
+
* Pattern matches on a Option, returning the result of the matching case.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* pipe(
|
|
139
|
+
* optionUser,
|
|
140
|
+
* Option.match({
|
|
141
|
+
* some: user => `Hello, ${user.name}`,
|
|
142
|
+
* none: () => "Hello, stranger"
|
|
143
|
+
* })
|
|
144
|
+
* );
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
const match: <A, B>(cases: {
|
|
148
|
+
none: () => B;
|
|
149
|
+
some: (a: A) => B;
|
|
150
|
+
}) => (data: Option<A>) => B;
|
|
151
|
+
/**
|
|
152
|
+
* Returns the value inside a Option, or a default value if None.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* pipe(Option.some(5), Option.getOrElse(0)); // 5
|
|
157
|
+
* pipe(Option.none(), Option.getOrElse(0)); // 0
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
const getOrElse: <A>(defaultValue: A) => (data: Option<A>) => A;
|
|
161
|
+
/**
|
|
162
|
+
* Executes a side effect on the value without changing the Option.
|
|
163
|
+
* Useful for logging or debugging.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* pipe(
|
|
168
|
+
* Option.some(5),
|
|
169
|
+
* Option.tap(n => console.log("Value:", n)),
|
|
170
|
+
* Option.map(n => n * 2)
|
|
171
|
+
* );
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
const tap: <A>(f: (a: A) => void) => (data: Option<A>) => Option<A>;
|
|
175
|
+
/**
|
|
176
|
+
* Filters a Option based on a predicate.
|
|
177
|
+
* Returns None if the predicate returns false or if the Option is already None.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* pipe(Option.some(5), Option.filter(n => n > 3)); // Some(5)
|
|
182
|
+
* pipe(Option.some(2), Option.filter(n => n > 3)); // None
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
const filter: <A>(predicate: (a: A) => boolean) => (data: Option<A>) => Option<A>;
|
|
186
|
+
/**
|
|
187
|
+
* Recovers from a None by providing a fallback Option.
|
|
188
|
+
*/
|
|
189
|
+
const recover: <A>(fallback: () => Option<A>) => (data: Option<A>) => Option<A>;
|
|
190
|
+
/**
|
|
191
|
+
* Applies a function wrapped in a Option to a value wrapped in a Option.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
196
|
+
* pipe(
|
|
197
|
+
* Option.some(add),
|
|
198
|
+
* Option.ap(Option.some(5)),
|
|
199
|
+
* Option.ap(Option.some(3))
|
|
200
|
+
* ); // Some(8)
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
const ap: <A>(arg: Option<A>) => <B>(data: Option<(a: A) => B>) => Option<B>;
|
|
204
|
+
}
|
|
205
|
+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,CAAC,KAAK,CAAC,CAAA;KAAE,MAAM,MAAM,MAAM,CAAC,CAAC,CAAC,KAAG,CACpB,CAAC;IAEzD;;;;;;;;OAQG;IACI,MAAM,SAAS,GAAI,CAAC,EAAE,cAAc,CAAC,MAAM,MAAM,MAAM,CAAC,CAAC,CAAC,KAAG,CAC1B,CAAC;IAE3C;;;;;;;;;;;;OAYG;IACI,MAAM,GAAG,GAAI,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,MAAM,MAAM,MAAM,CAAC,CAAC,CAAC,KAAG,MAAM,CAAC,CAAC,CAGvE,CAAC;IAEF;;;;;;;;;OASG;IACI,MAAM,MAAM,GAAI,CAAC,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,MAAM,MAAM,MAAM,CAAC,CAAC,CAAC,KAAG,MAAM,CAAC,CAAC,CAC/B,CAAC;IAExD;;OAEG;IACI,MAAM,OAAO,GAAI,CAAC,EAAE,UAAU,MAAM,MAAM,CAAC,CAAC,CAAC,MAAM,MAAM,MAAM,CAAC,CAAC,CAAC,KAAG,MAAM,CAAC,CAAC,CAClD,CAAC;IAEnC;;;;;;;;;;;;OAYG;IACI,MAAM,EAAE,GAAI,CAAC,EAAE,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAG,MAAM,CAAC,CAAC,CACb,CAAC;CACtE"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { Option } from "./Option.js";
|
|
2
|
+
import type { Lens } from "./Lens.js";
|
|
3
|
+
/** Keys of T for which undefined is assignable (i.e. optional fields). */
|
|
4
|
+
type OptionalKeys<T> = {
|
|
5
|
+
[K in keyof T]-?: undefined extends T[K] ? K : never;
|
|
6
|
+
}[keyof T];
|
|
7
|
+
/**
|
|
8
|
+
* Optional<S, A> focuses on a value A inside a structure S that may or may
|
|
9
|
+
* not be present. Like a Lens, but get returns Option<A>.
|
|
10
|
+
*
|
|
11
|
+
* Compose with other Optionals via `andThen`, or with a Lens via `andThenLens`.
|
|
12
|
+
* Convert a Lens to an Optional with `Lens.toOptional`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* type Profile = { username: string; bio?: string };
|
|
17
|
+
*
|
|
18
|
+
* const bioOpt = Optional.prop<Profile>()("bio");
|
|
19
|
+
*
|
|
20
|
+
* pipe(profile, Optional.get(bioOpt)); // Some("hello") or None
|
|
21
|
+
* pipe(profile, Optional.set(bioOpt)("hello")); // new Profile with bio set
|
|
22
|
+
* pipe(profile, Optional.modify(bioOpt)(s => s + "!")); // appends if present
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type Optional<S, A> = {
|
|
26
|
+
readonly get: (s: S) => Option<A>;
|
|
27
|
+
readonly set: (a: A) => (s: S) => S;
|
|
28
|
+
};
|
|
29
|
+
export declare namespace Optional {
|
|
30
|
+
/**
|
|
31
|
+
* Constructs an Optional from a getter (returning Option<A>) and a setter.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const firstChar = Optional.make(
|
|
36
|
+
* (s: string) => s.length > 0 ? Option.some(s[0]) : Option.none(),
|
|
37
|
+
* (c) => (s) => s.length > 0 ? c + s.slice(1) : s,
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
const make: <S, A>(get: (s: S) => Option<A>, set: (a: A) => (s: S) => S) => Optional<S, A>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates an Optional that focuses on an optional property of an object.
|
|
44
|
+
* Only keys whose type includes undefined (i.e. `field?: T`) are accepted.
|
|
45
|
+
* Call with the structure type first, then the key.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* type Profile = { username: string; bio?: string };
|
|
50
|
+
* const bioOpt = Optional.prop<Profile>()("bio");
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
const prop: <S>() => <K extends OptionalKeys<S>>(key: K) => Optional<S, NonNullable<S[K]>>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates an Optional that focuses on an element at a given index in an array.
|
|
56
|
+
* Returns None when the index is out of bounds; set is a no-op when out of bounds.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const firstItem = Optional.index<string>(0);
|
|
61
|
+
*
|
|
62
|
+
* pipe(["a", "b"], Optional.get(firstItem)); // Some("a")
|
|
63
|
+
* pipe([], Optional.get(firstItem)); // None
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
const index: <A>(i: number) => Optional<A[], A>;
|
|
67
|
+
/**
|
|
68
|
+
* Reads the focused value from a structure, returning Option<A>.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* pipe(profile, Optional.get(bioOpt)); // Some("...") or None
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
const get: <S, A>(opt: Optional<S, A>) => (s: S) => Option<A>;
|
|
76
|
+
/**
|
|
77
|
+
* Replaces the focused value within a structure.
|
|
78
|
+
* For indexed focuses, this is a no-op when the index is out of bounds.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* pipe(profile, Optional.set(bioOpt)("hello"));
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
const set: <S, A>(opt: Optional<S, A>) => (a: A) => (s: S) => S;
|
|
86
|
+
/**
|
|
87
|
+
* Applies a function to the focused value if it is present; returns the
|
|
88
|
+
* structure unchanged if the focus is absent.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* pipe(profile, Optional.modify(bioOpt)(s => s.toUpperCase()));
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
const modify: <S, A>(opt: Optional<S, A>) => (f: (a: A) => A) => (s: S) => S;
|
|
96
|
+
/**
|
|
97
|
+
* Returns the focused value or a default when the focus is absent.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* pipe(profile, Optional.getOrElse(bioOpt)("no bio"));
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
const getOrElse: <S, A>(opt: Optional<S, A>) => (defaultValue: A) => (s: S) => A;
|
|
105
|
+
/**
|
|
106
|
+
* Extracts a value from an Optional focus using handlers for the present
|
|
107
|
+
* and absent cases.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* pipe(profile, Optional.fold(bioOpt)(() => "no bio", (bio) => bio.toUpperCase()));
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
const fold: <S, A>(opt: Optional<S, A>) => <B>(onNone: () => B, onSome: (a: A) => B) => (s: S) => B;
|
|
115
|
+
/**
|
|
116
|
+
* Pattern matches on an Optional focus using a named-case object.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* pipe(
|
|
121
|
+
* profile,
|
|
122
|
+
* Optional.match(bioOpt)({ none: () => "no bio", some: (bio) => bio }),
|
|
123
|
+
* );
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
const match: <S, A>(opt: Optional<S, A>) => <B>(cases: {
|
|
127
|
+
none: () => B;
|
|
128
|
+
some: (a: A) => B;
|
|
129
|
+
}) => (s: S) => B;
|
|
130
|
+
/**
|
|
131
|
+
* Composes two Optionals: focuses through the outer, then through the inner.
|
|
132
|
+
* Returns None if either focus is absent.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const deepOpt = pipe(
|
|
137
|
+
* Optional.prop<User>()("address"),
|
|
138
|
+
* Optional.andThen(Optional.prop<Address>()("landmark")),
|
|
139
|
+
* );
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
const andThen: <A, B>(inner: Optional<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
|
|
143
|
+
/**
|
|
144
|
+
* Composes an Optional with a Lens, producing an Optional.
|
|
145
|
+
* The Lens focuses within the value found by the Optional.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* const cityOpt = pipe(
|
|
150
|
+
* Optional.prop<User>()("address"),
|
|
151
|
+
* Optional.andThenLens(Lens.prop<Address>()("city")),
|
|
152
|
+
* );
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
const andThenLens: <A, B>(inner: Lens<A, B>) => <S>(outer: Optional<S, A>) => Optional<S, B>;
|
|
156
|
+
}
|
|
157
|
+
export {};
|
|
158
|
+
//# sourceMappingURL=Optional.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Optional.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Optional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,0EAA0E;AAC1E,KAAK,YAAY,CAAC,CAAC,IAAI;KACpB,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CACrD,CAAC,MAAM,CAAC,CAAC,CAAC;AAEX;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI;IAC3B,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC;IAClC,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;CACrC,CAAC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,EAAE,CAAC,CAAC,MAAM,cAAc,CAAC,MAAM,GAAG,CAAC,KAAG,CAGpF,CAAC;IAEF;;;;;;;;OAQG;IACI,MAAM,IAAI,GACd,CAAC,EAAE,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,KAAG,CAGnF,CAAC;IAEJ;;;;;;;;;;OAUG;IACI,MAAM,KAAK,GACf,CAAC,EAAE,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MACzB,CAAC,EAAE,OAAO;QAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;KAAE,MAE7C,GAAG,CAAC,KACH,CAGF,CAAC;IAEJ;;;;;;;;;;;OAWG;IACI,MAAM,OAAO,GACjB,CAAC,EAAE,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAUvE,CAAC;IAEN;;;;;;;;;;;OAWG;IACI,MAAM,WAAW,GACrB,CAAC,EAAE,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAUnE,CAAC;CACP"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A computation that reads from a shared environment `R` and produces a value `A`.
|
|
3
|
+
* Use Reader to thread a dependency (config, logger, DB pool) through a pipeline
|
|
4
|
+
* without passing it explicitly to every function.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type Config = { baseUrl: string; apiKey: string };
|
|
9
|
+
*
|
|
10
|
+
* const buildUrl = (path: string): Reader<Config, string> =>
|
|
11
|
+
* (config) => `${config.baseUrl}${path}`;
|
|
12
|
+
*
|
|
13
|
+
* const withAuth = (url: string): Reader<Config, string> =>
|
|
14
|
+
* (config) => `${url}?key=${config.apiKey}`;
|
|
15
|
+
*
|
|
16
|
+
* const fetchEndpoint = (path: string): Reader<Config, string> =>
|
|
17
|
+
* pipe(
|
|
18
|
+
* buildUrl(path),
|
|
19
|
+
* Reader.chain(withAuth)
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* // Inject the config once at the edge
|
|
23
|
+
* fetchEndpoint("/users")(appConfig); // "https://api.example.com/users?key=secret"
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export type Reader<R, A> = (env: R) => A;
|
|
27
|
+
export declare namespace Reader {
|
|
28
|
+
/**
|
|
29
|
+
* Lifts a pure value into a Reader. The environment is ignored.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const always42: Reader<Config, number> = Reader.resolve(42);
|
|
34
|
+
* always42(anyConfig); // 42
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
const resolve: <R, A>(value: A) => Reader<R, A>;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the full environment as the result.
|
|
40
|
+
* The fundamental way to access the environment in a pipeline.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* pipe(
|
|
45
|
+
* Reader.ask<Config>(),
|
|
46
|
+
* Reader.map(config => config.baseUrl)
|
|
47
|
+
* )(appConfig); // "https://api.example.com"
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
const ask: <R>() => Reader<R, R>;
|
|
51
|
+
/**
|
|
52
|
+
* Projects a value from the environment using a selector function.
|
|
53
|
+
* Equivalent to `pipe(Reader.ask(), Reader.map(f))` but more direct.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const getBaseUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
|
|
58
|
+
* getBaseUrl(appConfig); // "https://api.example.com"
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
const asks: <R, A>(f: (env: R) => A) => Reader<R, A>;
|
|
62
|
+
/**
|
|
63
|
+
* Transforms the value produced by a Reader.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* pipe(
|
|
68
|
+
* Reader.asks((c: Config) => c.baseUrl),
|
|
69
|
+
* Reader.map(url => url.toUpperCase())
|
|
70
|
+
* )(appConfig); // "HTTPS://API.EXAMPLE.COM"
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
const map: <R, A, B>(f: (a: A) => B) => (data: Reader<R, A>) => Reader<R, B>;
|
|
74
|
+
/**
|
|
75
|
+
* Sequences two Readers. Both see the same environment.
|
|
76
|
+
* The output of the first is passed to `f`, which returns the next Reader.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const buildUrl = (path: string): Reader<Config, string> =>
|
|
81
|
+
* Reader.asks(c => `${c.baseUrl}${path}`);
|
|
82
|
+
*
|
|
83
|
+
* const addAuth = (url: string): Reader<Config, string> =>
|
|
84
|
+
* Reader.asks(c => `${url}?key=${c.apiKey}`);
|
|
85
|
+
*
|
|
86
|
+
* pipe(
|
|
87
|
+
* buildUrl("/items"),
|
|
88
|
+
* Reader.chain(addAuth)
|
|
89
|
+
* )(appConfig); // "https://api.example.com/items?key=secret"
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
const chain: <R, A, B>(f: (a: A) => Reader<R, B>) => (data: Reader<R, A>) => Reader<R, B>;
|
|
93
|
+
/**
|
|
94
|
+
* Applies a function wrapped in a Reader to a value wrapped in a Reader.
|
|
95
|
+
* Both Readers see the same environment.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* const add = (a: number) => (b: number) => a + b;
|
|
100
|
+
* pipe(
|
|
101
|
+
* Reader.resolve<Config, typeof add>(add),
|
|
102
|
+
* Reader.ap(Reader.asks(c => c.timeout)),
|
|
103
|
+
* Reader.ap(Reader.resolve(5))
|
|
104
|
+
* )(appConfig);
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
const ap: <R, A>(arg: Reader<R, A>) => <B>(data: Reader<R, (a: A) => B>) => Reader<R, B>;
|
|
108
|
+
/**
|
|
109
|
+
* Executes a side effect on the produced value without changing the Reader.
|
|
110
|
+
* Useful for logging or debugging inside a pipeline.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* pipe(
|
|
115
|
+
* buildUrl("/users"),
|
|
116
|
+
* Reader.tap(url => console.log("Requesting:", url)),
|
|
117
|
+
* Reader.chain(addAuth)
|
|
118
|
+
* )(appConfig);
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
const tap: <R, A>(f: (a: A) => void) => (data: Reader<R, A>) => Reader<R, A>;
|
|
122
|
+
/**
|
|
123
|
+
* Adapts a Reader to work with a different (typically wider) environment
|
|
124
|
+
* by transforming the environment before passing it to the Reader.
|
|
125
|
+
* This lets you compose Readers that expect different environments.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* type AppEnv = { db: DbPool; config: Config; logger: Logger };
|
|
130
|
+
*
|
|
131
|
+
* // buildUrl only needs Config
|
|
132
|
+
* const buildUrl: Reader<Config, string> = Reader.asks(c => c.baseUrl);
|
|
133
|
+
*
|
|
134
|
+
* // Zoom in from AppEnv to Config
|
|
135
|
+
* const buildUrlFromApp: Reader<AppEnv, string> =
|
|
136
|
+
* pipe(buildUrl, Reader.local((env: AppEnv) => env.config));
|
|
137
|
+
*
|
|
138
|
+
* buildUrlFromApp(appEnv); // works with the full AppEnv
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
const local: <R2, R>(f: (env: R2) => R) => <A>(data: Reader<R, A>) => Reader<R2, A>;
|
|
142
|
+
/**
|
|
143
|
+
* Runs a Reader by supplying the environment. Use this at the edge of your
|
|
144
|
+
* program where the environment is available.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* pipe(
|
|
149
|
+
* buildEndpoint("/users"),
|
|
150
|
+
* Reader.run(appConfig)
|
|
151
|
+
* ); // "https://api.example.com/users?key=secret"
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
const run: <R>(env: R) => <A>(data: Reader<R, A>) => A;
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=Reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Reader.d.ts","sourceRoot":"","sources":["../../../src/src/Core/Reader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;AAEzC,yBAAiB,MAAM,CAAC;IACtB;;;;;;;;OAQG;IACI,MAAM,OAAO,GAAI,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAoB,CAAC;IAEzE;;;;;;;;;;;OAWG;IACI,MAAM,GAAG,GAAI,CAAC,OAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAiB,CAAC;IAEvD;;;;;;;;;OASG;IACI,MAAM,IAAI,GAAI,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,KAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAoB,CAAC;IAE9E;;;;;;;;;;OAUG;IACI,MAAM,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