kind-adt 0.1.0 → 0.1.1
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 +24 -24
- package/package.json +1 -1
- package/utils.d.ts +2 -0
- package/utils.js +8 -12
package/README.md
CHANGED
|
@@ -27,10 +27,10 @@ import { type Data, make } from "kind-adt";
|
|
|
27
27
|
import type { Arg0, HKT } from "hkt-core";
|
|
28
28
|
|
|
29
29
|
// Define an ADT
|
|
30
|
-
export type Option<T> = Data<
|
|
31
|
-
Some: [value: T]
|
|
32
|
-
None: []
|
|
33
|
-
>;
|
|
30
|
+
export type Option<T> = Data<{
|
|
31
|
+
Some: [value: T];
|
|
32
|
+
None: [];
|
|
33
|
+
}>;
|
|
34
34
|
|
|
35
35
|
// Generate constructors and match functions for the ADT
|
|
36
36
|
export const { Some, None, match } = make<OptionHKT>();
|
|
@@ -75,10 +75,10 @@ npm install kind-adt
|
|
|
75
75
|
```typescript
|
|
76
76
|
import type { Data } from "kind-adt";
|
|
77
77
|
|
|
78
|
-
export type Option<T> = Data<
|
|
79
|
-
Some: [T]
|
|
80
|
-
None: []
|
|
81
|
-
>;
|
|
78
|
+
export type Option<T> = Data<{
|
|
79
|
+
Some: [value: T];
|
|
80
|
+
None: [];
|
|
81
|
+
}>;
|
|
82
82
|
// Expands to:
|
|
83
83
|
// export type Option<T> =
|
|
84
84
|
// | { readonly _tag: "Some"; readonly _0: T }
|
|
@@ -232,10 +232,10 @@ You can extract the type of each variant of an ADT using the `Tagged` utility ty
|
|
|
232
232
|
```typescript
|
|
233
233
|
import type { Data, Tagged } from "kind-adt";
|
|
234
234
|
|
|
235
|
-
type Option<T> = Data<
|
|
236
|
-
Some: [T]
|
|
237
|
-
None: []
|
|
238
|
-
>;
|
|
235
|
+
type Option<T> = Data<{
|
|
236
|
+
Some: [value: T];
|
|
237
|
+
None: [];
|
|
238
|
+
}>;
|
|
239
239
|
|
|
240
240
|
type Some<T> = Extract<Option<T>, Tagged<"Some">>;
|
|
241
241
|
// Expands to:
|
|
@@ -255,10 +255,10 @@ type None = Extract<Option<unknown>, Tagged<"None">>;
|
|
|
255
255
|
Let’s revisit the `Option<T>` example in the quickstart section.
|
|
256
256
|
|
|
257
257
|
```typescript
|
|
258
|
-
type Option<T> = Data<
|
|
259
|
-
Some: [T]
|
|
260
|
-
None: []
|
|
261
|
-
>;
|
|
258
|
+
type Option<T> = Data<{
|
|
259
|
+
Some: [value: T];
|
|
260
|
+
None: [];
|
|
261
|
+
}>;
|
|
262
262
|
|
|
263
263
|
const Option = make<OptionHKT>();
|
|
264
264
|
interface OptionHKT extends HKT {
|
|
@@ -294,15 +294,15 @@ function Option.match<T, R>(adt: Option<T>, cases: {
|
|
|
294
294
|
This also applies to other ADTs, such as `Result`, `Either`, etc.
|
|
295
295
|
|
|
296
296
|
```typescript
|
|
297
|
-
type Result<T, E> = Data<
|
|
298
|
-
Ok: [value: T]
|
|
299
|
-
Err: [error: E]
|
|
300
|
-
>;
|
|
297
|
+
type Result<T, E> = Data<{
|
|
298
|
+
Ok: [value: T];
|
|
299
|
+
Err: [error: E];
|
|
300
|
+
}>;
|
|
301
301
|
|
|
302
|
-
type Either<A, B> = Data<
|
|
303
|
-
Left: [value: A]
|
|
304
|
-
Right: [value: B]
|
|
305
|
-
>;
|
|
302
|
+
type Either<A, B> = Data<{
|
|
303
|
+
Left: [value: A];
|
|
304
|
+
Right: [value: B];
|
|
305
|
+
}>;
|
|
306
306
|
```
|
|
307
307
|
|
|
308
308
|
### Syntax sugar for ADTs with only one object field
|
package/package.json
CHANGED
package/utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ShowOptions } from "showify";
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Print arguments including (possibly) ADTs to `stdout` with newline.
|
|
4
5
|
* @param args The arguments to print.
|
|
@@ -24,6 +25,7 @@ import type { ShowOptions } from "showify";
|
|
|
24
25
|
* @see {@linkcode show} if you want more control over the output.
|
|
25
26
|
*/
|
|
26
27
|
export declare function println(...args: unknown[]): void;
|
|
28
|
+
|
|
27
29
|
/**
|
|
28
30
|
* Stringify a (possibly) ADT to human-readable format.
|
|
29
31
|
* @param value The value to stringify.
|
package/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Node as SerializerNode, serializer, show as stringify } from "showify";
|
|
2
2
|
|
|
3
|
-
import { unwrap } from ".";
|
|
3
|
+
import { unwrap } from "./index.js";
|
|
4
4
|
|
|
5
5
|
const { between, pair, sequence, text, variant } = SerializerNode;
|
|
6
6
|
|
|
@@ -30,9 +30,7 @@ const { between, pair, sequence, text, variant } = SerializerNode;
|
|
|
30
30
|
*/
|
|
31
31
|
export function println(...args) {
|
|
32
32
|
getConsole().log(
|
|
33
|
-
...args.map((arg) =>
|
|
34
|
-
typeof arg === "string" ? arg : show(arg, { colors: true, indent: 2 }),
|
|
35
|
-
),
|
|
33
|
+
...args.map((arg) => (typeof arg === "string" ? arg : show(arg, { colors: true, indent: 2 }))),
|
|
36
34
|
);
|
|
37
35
|
}
|
|
38
36
|
|
|
@@ -103,31 +101,29 @@ export function show(value, options = {}) {
|
|
|
103
101
|
fields.length ?
|
|
104
102
|
variant(
|
|
105
103
|
sequence([
|
|
106
|
-
text(c.
|
|
104
|
+
text(c.cyan(val._tag) + "("),
|
|
107
105
|
...flatMap(fields, (field, i, arr) =>
|
|
108
|
-
i === arr.length - 1 ?
|
|
109
|
-
expand(field)
|
|
110
|
-
: [expand(field), text(", ")],
|
|
106
|
+
i === arr.length - 1 ? expand(field) : [expand(field), text(", ")],
|
|
111
107
|
),
|
|
112
108
|
...(body.type === "text" ? [text(")")] : [text(") "), body]),
|
|
113
109
|
]),
|
|
114
110
|
body.type === "text" ?
|
|
115
111
|
between(
|
|
116
112
|
fields.map((field) => pair(expand(field), text(","))),
|
|
117
|
-
text(c.
|
|
113
|
+
text(c.cyan(val._tag) + "("),
|
|
118
114
|
text(")"),
|
|
119
115
|
)
|
|
120
116
|
: pair(
|
|
121
117
|
between(
|
|
122
118
|
fields.map((field) => pair(expand(field), text(","))),
|
|
123
|
-
text(c.
|
|
119
|
+
text(c.cyan(val._tag) + "("),
|
|
124
120
|
text(") "),
|
|
125
121
|
),
|
|
126
122
|
body,
|
|
127
123
|
),
|
|
128
124
|
)
|
|
129
|
-
: body.type === "text" ? text(c.
|
|
130
|
-
: pair(text(c.
|
|
125
|
+
: body.type === "text" ? text(c.cyan(val._tag))
|
|
126
|
+
: pair(text(c.cyan(val._tag) + " "), body)
|
|
131
127
|
);
|
|
132
128
|
},
|
|
133
129
|
}),
|