slang-ts 0.2.1 → 0.2.2
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/CHANGELOG.md +2 -3
- package/README.md +2 -2
- package/package.json +1 -1
- package/skill/SKILL.md +50 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
Everything notable in `slang-ts`, grouped by area. No release diary
|
|
3
|
+
Everything notable in `slang-ts`, grouped by area. No release diary: just what's here.
|
|
4
4
|
|
|
5
5
|
## Results and flow
|
|
6
6
|
|
|
7
|
-
- `Ok(value, message?)` keeps an optional success message on the result
|
|
8
|
-
the user. Results without one have no `message` property.
|
|
7
|
+
- `Ok(value, message?)` keeps an optional success message on the result. Results without one have no `message` property.
|
|
9
8
|
- `SafeTry` unwraps Option and Atom values into one result. A Result never nests inside another
|
|
10
9
|
Result, and plain object properties and array elements stay unchanged.
|
|
11
10
|
|
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ if (result.isOk) {
|
|
|
39
39
|
}
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
`println` is optional
|
|
42
|
+
`println` is optional: it's a small development logger.
|
|
43
43
|
|
|
44
44
|
## Imports
|
|
45
45
|
|
|
@@ -168,7 +168,7 @@ Full detail: [SafeTry][safe-try] and [Result][result].
|
|
|
168
168
|
### Call an API with a timeout and retries: SuperPromise
|
|
169
169
|
|
|
170
170
|
Your function gets an `AbortSignal`. `timeout` aborts an attempt, `retry` runs it again after an
|
|
171
|
-
ordinary failure, and you get back `Ok` or `Err
|
|
171
|
+
ordinary failure, and you get back `Ok` or `Err`: so no `try`/`catch` around the await.
|
|
172
172
|
|
|
173
173
|
```ts
|
|
174
174
|
import { superPromise, println } from "slang-ts";
|
package/package.json
CHANGED
package/skill/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# slang-ts agent skill
|
|
2
2
|
|
|
3
|
-
This skill tells AI agents how to work with slang-ts
|
|
3
|
+
This skill tells AI agents how to work with slang-ts: its APIs, patterns, and conventions so agents can write correct TypeScript against it.
|
|
4
4
|
|
|
5
5
|
## What slang-ts is
|
|
6
6
|
|
|
@@ -16,7 +16,9 @@ import { option, Ok, Err, match, superPromise, list } from "slang-ts";
|
|
|
16
16
|
|
|
17
17
|
### Option
|
|
18
18
|
|
|
19
|
-
`option(value)
|
|
19
|
+
`option<T>(value): Option<T>` — wraps a value that may or may not be there.
|
|
20
|
+
|
|
21
|
+
`0` and `false` count as present; `null`, `undefined`, `""`, `NaN`, and `Infinity` are `None`.
|
|
20
22
|
|
|
21
23
|
```ts
|
|
22
24
|
const port = option(process.env.PORT).unwrap().else(3000);
|
|
@@ -26,7 +28,16 @@ Never read `.value` on `None`. Check `.isSome` / `.isNone` first, or use `.unwra
|
|
|
26
28
|
|
|
27
29
|
### Result
|
|
28
30
|
|
|
29
|
-
`
|
|
31
|
+
`Result<T, E>` is `Ok<T>` or `Err<E>`.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
Ok<T>(value, message?); // success, optional message
|
|
35
|
+
Err<E>(error, raw?); // failure, optional diagnostic cause
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`Err(error, raw?)` keeps the original cause on the non-enumerable `.raw` for logs; `.error` stays human-readable.
|
|
39
|
+
|
|
40
|
+
Branch with `if (result.isOk) { ... } else { ... }`. Finish chains with `.unwrap().else(fallback)` or `.unwrap().panic(message?)`.
|
|
30
41
|
|
|
31
42
|
```ts
|
|
32
43
|
function divide(a: number, b: number): Result<number, string> {
|
|
@@ -34,15 +45,17 @@ function divide(a: number, b: number): Result<number, string> {
|
|
|
34
45
|
}
|
|
35
46
|
```
|
|
36
47
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
`Err(error, raw?)` keeps the original cause on the non-enumerable `.raw` for logs; `.error` stays human-readable.
|
|
48
|
+
`Result` never nests inside another `Result` — `Ok(Ok(x))` and `Ok(Err(x))` won't compile.
|
|
40
49
|
|
|
41
50
|
### match / matchAll
|
|
42
51
|
|
|
43
|
-
`match(
|
|
52
|
+
`match(value, patterns)` — exhaustive pattern matching for `Option` and `Result`. TypeScript requires both cases.
|
|
44
53
|
|
|
45
|
-
|
|
54
|
+
```ts
|
|
55
|
+
match(result, { Ok: (v) => ..., Err: (e) => ... });
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`matchAll(value, patterns)` — matching for strings, numbers, booleans, symbols, and `Atom`s. Always needs a `_` fallback.
|
|
46
59
|
|
|
47
60
|
```ts
|
|
48
61
|
const message = matchAll(failed.category, {
|
|
@@ -65,31 +78,42 @@ if (failed.category === "OUT_OF_BOUNDS") {
|
|
|
65
78
|
```
|
|
66
79
|
|
|
67
80
|
- `get` already takes a fallback: `get(key, fallback)`. Do not pair `get(x, fallback)` with `.else(fallback)` — that's redundant.
|
|
68
|
-
- `.panic()` throws; once you panic, no custom
|
|
81
|
+
- `.panic()` throws; once you panic, no custom message is read afterward.
|
|
69
82
|
|
|
70
83
|
### list
|
|
71
84
|
|
|
72
|
-
|
|
85
|
+
`list<T>(input): ListChain<T>` — strict indexed arrays with explicit bounds. Out-of-range fails instead of clamping.
|
|
73
86
|
|
|
74
87
|
```ts
|
|
75
88
|
const page = list(users).range(start, end).else([]);
|
|
76
89
|
```
|
|
77
90
|
|
|
91
|
+
Methods: `get`, `take`, `skip`, `range`, `chunk`, `count`, `prune`.
|
|
92
|
+
|
|
78
93
|
### struct
|
|
79
94
|
|
|
80
|
-
|
|
95
|
+
`struct<T>(input): StructChain<T>` — safe record access and shaping.
|
|
96
|
+
|
|
97
|
+
Methods: `get`, `pick`, `omit`, `merge`, `countBy`, `keyBy`, `prune`.
|
|
81
98
|
|
|
82
99
|
```ts
|
|
83
100
|
const port = struct(config).get(["server", "port"], 3000);
|
|
84
101
|
```
|
|
85
102
|
|
|
103
|
+
- `prune` drops Slang-falsy values (`null`, `undefined`, `""`, `NaN`, `Infinity`, `-Infinity`) but keeps `0` and `false`. Its predicate receives `(value, key)` and returns `true` to keep a value. It needs a plain object.
|
|
104
|
+
- Wrong shape for the operation fails with `INVALID_INPUT`. A missing `get` path fails with `OUT_OF_BOUNDS`. Once a chain fails, later steps don't run, and the category survives so you can check `.category`.
|
|
105
|
+
|
|
86
106
|
### sets
|
|
87
107
|
|
|
88
|
-
|
|
108
|
+
`sets<T>(input): SetsChain<T>` — membership and set operations.
|
|
109
|
+
|
|
110
|
+
Methods: `has`, `unique`, `union`, `intersection`, `difference`. Objects compare by identity; primitives by value.
|
|
89
111
|
|
|
90
112
|
### Ok.iterate
|
|
91
113
|
|
|
92
|
-
|
|
114
|
+
`Ok<T>(value).iterate(): IterateChain<T>` — lazy pipelines over a successful `Ok`.
|
|
115
|
+
|
|
116
|
+
Methods: `map`, `filter`, `flatMap`, `take`, `drop`, `chunk`, `reduce`, `count`, `toArray`.
|
|
93
117
|
|
|
94
118
|
```ts
|
|
95
119
|
const names = Ok(users)
|
|
@@ -104,7 +128,9 @@ const names = Ok(users)
|
|
|
104
128
|
|
|
105
129
|
### SuperPromise
|
|
106
130
|
|
|
107
|
-
|
|
131
|
+
`superPromise<T>(fn, options?): SuperPromise<T>` — one async operation with start, retry, timeout, and cancel controls. Your function gets an `AbortSignal`. Tasks end as `Ok` or `Err`.
|
|
132
|
+
|
|
133
|
+
Options: `defer`, `delay`, `timeout`, `retry`, `retryUntil`, `group`.
|
|
108
134
|
|
|
109
135
|
```ts
|
|
110
136
|
const user = await superPromise(
|
|
@@ -118,7 +144,7 @@ const user = await superPromise(
|
|
|
118
144
|
|
|
119
145
|
### SuperRunner
|
|
120
146
|
|
|
121
|
-
|
|
147
|
+
`superRunner(config): SuperRunner<Value>` — manages several already-created `SuperPromise`s: sequence, parallel, or race. `start()` runs once.
|
|
122
148
|
|
|
123
149
|
```ts
|
|
124
150
|
const all = superRunner({ type: "all", runners: { a, b } });
|
|
@@ -127,7 +153,7 @@ const result = await all.start();
|
|
|
127
153
|
|
|
128
154
|
### taskGroup
|
|
129
155
|
|
|
130
|
-
|
|
156
|
+
`taskGroup(name, options?): TaskGroup` — shared concurrency limit and start spacing across tasks and runners. Retries wait for a new slot.
|
|
131
157
|
|
|
132
158
|
```ts
|
|
133
159
|
const api = taskGroup("user-api", { concurrency: 3, throttle: 200 });
|
|
@@ -136,27 +162,27 @@ const task = superPromise(fetchUsers, { group: api });
|
|
|
136
162
|
|
|
137
163
|
### Signal / Channel
|
|
138
164
|
|
|
139
|
-
`createSignal()` — a trigger that fires once (`fire()`), AbortSignal-compatible.
|
|
165
|
+
`createSignal(): Signal` — a trigger that fires once (`fire()`), AbortSignal-compatible.
|
|
140
166
|
|
|
141
|
-
`createChannel()` — in-process publish/subscribe. `subscribe(topic, handler)` returns an unsubscribe function.
|
|
167
|
+
`createChannel(): Channel` — in-process publish/subscribe. `subscribe(topic, handler)` returns an unsubscribe function.
|
|
142
168
|
|
|
143
169
|
## Other utilities
|
|
144
170
|
|
|
145
|
-
- `atom(
|
|
146
|
-
- `to` — convert between `Option`, `Atom`, `Result` (e.g. `option("ready").to("atom")`).
|
|
147
|
-
- `pipe` — run `Result`-returning steps in order, stopping at the first failure.
|
|
171
|
+
- `atom<T>(name): Atom<T>` — unique symbol with a readable description; matches in `matchAll` by description.
|
|
172
|
+
- `to(target)` — convert between `Option`, `Atom`, `Result` (e.g. `option("ready").to("atom")`).
|
|
173
|
+
- `pipe(value, ...fns)` — run `Result`-returning steps in order, stopping at the first failure.
|
|
148
174
|
- `zip` / `zipWith` / `unzip` — combine parallel collections.
|
|
149
|
-
- `memoized` — cache a function by argument identity.
|
|
175
|
+
- `memoized(fn)` — cache a function by argument identity.
|
|
150
176
|
- `panic(message)` — throw immediately for unrecoverable states.
|
|
151
177
|
- `println` — dev logging, silent in production unless `setEnvironment` routes it.
|
|
152
178
|
|
|
153
179
|
## Conventions agents should follow
|
|
154
180
|
|
|
155
181
|
- Named params in an object for multi-arg functions, not positional.
|
|
156
|
-
- No classes
|
|
182
|
+
- No classes: use functions and plain objects.
|
|
157
183
|
- Error categories are strings (`.category`), not numeric codes. Never call them "error codes."
|
|
158
184
|
- `get` accepts a fallback; don't chain `.else()` after a `get` that already has one.
|
|
159
|
-
- `.panic()` throws
|
|
185
|
+
- `.panic()` throws: only for unrecoverable states. Use `Result` / `.else()` for expected failures.
|
|
160
186
|
- Zero runtime dependencies; ESM only.
|
|
161
187
|
|
|
162
188
|
## Package contents
|