functype 0.8.55 → 0.8.61
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/LICENSE +20 -20
- package/README.md +277 -197
- package/dist/Tuple-GXgoHfiN.d.ts +54 -0
- package/dist/branded/index.d.ts +48 -0
- package/dist/branded/index.mjs +2 -0
- package/dist/branded/index.mjs.map +1 -0
- package/dist/chunk-A3EHNBZM.mjs +2 -0
- package/dist/chunk-A3EHNBZM.mjs.map +1 -0
- package/dist/chunk-OQWAJKZJ.mjs +2 -0
- package/dist/chunk-OQWAJKZJ.mjs.map +1 -0
- package/dist/chunk-TQJDL6YW.mjs +2 -0
- package/dist/chunk-TQJDL6YW.mjs.map +1 -0
- package/dist/either/index.d.ts +3 -0
- package/dist/either/index.mjs +2 -0
- package/dist/either/index.mjs.map +1 -0
- package/dist/fpromise/index.d.ts +3 -0
- package/dist/fpromise/index.mjs +2 -0
- package/dist/fpromise/index.mjs.map +1 -0
- package/dist/index.d.ts +3 -383
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/list/index.d.ts +3 -0
- package/dist/list/index.mjs +2 -0
- package/dist/list/index.mjs.map +1 -0
- package/dist/map/index.d.ts +3 -0
- package/dist/map/index.mjs +2 -0
- package/dist/map/index.mjs.map +1 -0
- package/dist/option/index.d.ts +870 -0
- package/dist/option/index.mjs +2 -0
- package/dist/option/index.mjs.map +1 -0
- package/dist/set/index.d.ts +3 -0
- package/dist/set/index.mjs +2 -0
- package/dist/set/index.mjs.map +1 -0
- package/dist/try/index.d.ts +3 -0
- package/dist/try/index.mjs +2 -0
- package/dist/try/index.mjs.map +1 -0
- package/dist/tuple/index.d.ts +1 -0
- package/dist/tuple/index.mjs +2 -0
- package/dist/tuple/index.mjs.map +1 -0
- package/package.json +84 -16
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Jordan Burke
|
|
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
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jordan Burke
|
|
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
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,197 +1,277 @@
|
|
|
1
|
-
# Functype
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
[](https://github.com/jordanburke/functype/actions/workflows/pnpm-build.yml)
|
|
5
|
-
|
|
6
|
-
## A Functional Programming Library for TypeScript
|
|
7
|
-
|
|
8
|
-
Functype is a lightweight functional programming library for TypeScript, drawing inspiration from functional programming paradigms, the Scala Standard Library, and ZIO. It provides a comprehensive set of utilities and abstractions designed to facilitate functional programming within TypeScript applications.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
- **
|
|
22
|
-
- **
|
|
23
|
-
- **
|
|
24
|
-
- **
|
|
25
|
-
- **
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
import
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
// Handle
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
1
|
+
# Functype
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://github.com/jordanburke/functype/actions/workflows/pnpm-build.yml)
|
|
5
|
+
|
|
6
|
+
## A Functional Programming Library for TypeScript
|
|
7
|
+
|
|
8
|
+
Functype is a lightweight functional programming library for TypeScript, drawing inspiration from functional programming paradigms, the Scala Standard Library, and ZIO. It provides a comprehensive set of utilities and abstractions designed to facilitate functional programming within TypeScript applications.
|
|
9
|
+
|
|
10
|
+
[API Documentation](https://jordanburke.github.io/functype/)
|
|
11
|
+
|
|
12
|
+
## Core Principles
|
|
13
|
+
|
|
14
|
+
- **Immutability**: All data structures are immutable, promoting predictable and side-effect-free code
|
|
15
|
+
- **Type Safety**: Leverages TypeScript's type system to ensure compile-time safety
|
|
16
|
+
- **Composability**: Provides abstractions for building complex programs from simple components
|
|
17
|
+
- **Functional Paradigms**: Embraces concepts like monads, functors, and type classes
|
|
18
|
+
|
|
19
|
+
## Key Features
|
|
20
|
+
|
|
21
|
+
- **Option Type**: Handle nullable values with `Some` and `None` types
|
|
22
|
+
- **Either Type**: Express computation results with potential failures using `Left` and `Right`
|
|
23
|
+
- **List, Set, Map**: Immutable collection types with functional operators
|
|
24
|
+
- **Try Type**: Safely execute operations that might throw exceptions
|
|
25
|
+
- **Task**: Handle synchronous and asynchronous operations with error handling
|
|
26
|
+
- **Tuple**: Type-safe fixed-length arrays
|
|
27
|
+
- **Typeable**: Runtime type identification with compile-time safety
|
|
28
|
+
|
|
29
|
+
## Roadmap / TODO
|
|
30
|
+
|
|
31
|
+
### Missing Functionality
|
|
32
|
+
|
|
33
|
+
- [ ] Add lazy evaluation structures (LazyList/Stream)
|
|
34
|
+
- [ ] Implement Validation type for applicative validation
|
|
35
|
+
- [ ] Add Reader/State/IO monads for more functional patterns
|
|
36
|
+
- [ ] Implement lens/optics for immutable updates
|
|
37
|
+
- [ ] Expand concurrent execution utilities beyond FPromise.all
|
|
38
|
+
|
|
39
|
+
### Performance Optimizations
|
|
40
|
+
|
|
41
|
+
- [ ] Add memoization utilities
|
|
42
|
+
- [ ] Improve recursive operations for large collections
|
|
43
|
+
- [ ] Implement immutable data structures with structural sharing
|
|
44
|
+
- [ ] Add performance benchmarks
|
|
45
|
+
- [x] Optimize TreeShaking with sideEffects flag in package.json
|
|
46
|
+
- [x] Support selective module imports for smaller bundles
|
|
47
|
+
- [x] Add bundle size monitoring to CI/CD
|
|
48
|
+
|
|
49
|
+
### API Consistency
|
|
50
|
+
|
|
51
|
+
- [ ] Ensure all modules follow the Scala-inspired pattern:
|
|
52
|
+
- Constructor functions that return objects with methods
|
|
53
|
+
- Object methods for common operations
|
|
54
|
+
- Companion functions for additional utilities
|
|
55
|
+
- [x] Align Task API with other monadic structures
|
|
56
|
+
- [ ] Standardize import patterns (@ imports vs relative paths)
|
|
57
|
+
- [x] Implement consistent error handling strategy for async operations
|
|
58
|
+
|
|
59
|
+
### Testing and Documentation
|
|
60
|
+
|
|
61
|
+
- [ ] Add observable test coverage metrics
|
|
62
|
+
- [x] Implement property-based testing
|
|
63
|
+
- [ ] Expand error handling tests
|
|
64
|
+
- [ ] Add interoperability tests with other libraries
|
|
65
|
+
|
|
66
|
+
### TypeScript Improvements
|
|
67
|
+
|
|
68
|
+
- [x] Enable stricter TypeScript settings (noImplicitAny: true)
|
|
69
|
+
- [x] Add noUncheckedIndexedAccess for safer array indexing
|
|
70
|
+
- [ ] Improve support for higher-kinded types:
|
|
71
|
+
- Current type parameters work well for first-order types
|
|
72
|
+
- Expand to support type constructors as parameters (F<A> => F<B>)
|
|
73
|
+
- [ ] Add branded/nominal types for stronger type safety
|
|
74
|
+
- [ ] Implement more type-level utilities (conditional types, template literals)
|
|
75
|
+
- [ ] Leverage newer TypeScript features (const type parameters, tuple manipulation)
|
|
76
|
+
|
|
77
|
+
## Installation
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# NPM
|
|
81
|
+
npm install functype
|
|
82
|
+
|
|
83
|
+
# Yarn
|
|
84
|
+
yarn add functype
|
|
85
|
+
|
|
86
|
+
# PNPM
|
|
87
|
+
pnpm add functype
|
|
88
|
+
|
|
89
|
+
# Bun
|
|
90
|
+
bun add functype
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Bundle Size Optimization
|
|
94
|
+
|
|
95
|
+
Functype is optimized for tree-shaking and offers multiple import strategies to minimize bundle size:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Selective module imports (recommended for production)
|
|
99
|
+
import { Option } from "functype/option"
|
|
100
|
+
import { Either } from "functype/either"
|
|
101
|
+
|
|
102
|
+
// Direct constructor imports (smallest bundle)
|
|
103
|
+
import { some, none } from "functype/option"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
For detailed optimization strategies, see the [Bundle Optimization Guide](docs/BUNDLE_OPTIMIZATION.md).
|
|
107
|
+
|
|
108
|
+
## Usage Examples
|
|
109
|
+
|
|
110
|
+
### Option
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { Option, Some, None } from "functype"
|
|
114
|
+
|
|
115
|
+
// Create options
|
|
116
|
+
const value = Option("hello") // Some("hello")
|
|
117
|
+
const empty = Option(null) // None
|
|
118
|
+
const explicit = Some(42) // Some(42)
|
|
119
|
+
|
|
120
|
+
// Transform values
|
|
121
|
+
const length = value.map((s) => s.length) // Some(5)
|
|
122
|
+
const nothing = empty.map((s) => s.length) // None
|
|
123
|
+
|
|
124
|
+
// Handle default values
|
|
125
|
+
const result = value.getOrElse("world") // "hello"
|
|
126
|
+
const fallback = empty.getOrElse("world") // "world"
|
|
127
|
+
|
|
128
|
+
// Conditionally filter
|
|
129
|
+
const filtered = value.filter((s) => s.length > 10) // None
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Either
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { Either, Right, Left } from "functype"
|
|
136
|
+
|
|
137
|
+
// Success case
|
|
138
|
+
const success = Right<string, number>(42)
|
|
139
|
+
// Error case
|
|
140
|
+
const failure = Left<string, number>("error")
|
|
141
|
+
|
|
142
|
+
// Transform values (map only applies to Right)
|
|
143
|
+
const doubled = success.map((x) => x * 2) // Right(84)
|
|
144
|
+
const stillError = failure.map((x) => x * 2) // Left("error")
|
|
145
|
+
|
|
146
|
+
// Handle errors
|
|
147
|
+
const value = success.getOrElse(0) // 42
|
|
148
|
+
const fallback = failure.getOrElse(0) // 0
|
|
149
|
+
|
|
150
|
+
// Pattern matching with fold
|
|
151
|
+
const result = success.fold(
|
|
152
|
+
(err) => `Error: ${err}`,
|
|
153
|
+
(val) => `Success: ${val}`,
|
|
154
|
+
) // "Success: 42"
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### List
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { List } from "functype"
|
|
161
|
+
|
|
162
|
+
const numbers = List([1, 2, 3, 4])
|
|
163
|
+
|
|
164
|
+
// Transform
|
|
165
|
+
const doubled = numbers.map((x) => x * 2) // List([2, 4, 6, 8])
|
|
166
|
+
|
|
167
|
+
// Filter
|
|
168
|
+
const evens = numbers.filter((x) => x % 2 === 0) // List([2, 4])
|
|
169
|
+
|
|
170
|
+
// Reduce
|
|
171
|
+
const sum = numbers.foldLeft(0)((acc, x) => acc + x) // 10
|
|
172
|
+
|
|
173
|
+
// Add/remove elements (immutably)
|
|
174
|
+
const withFive = numbers.add(5) // List([1, 2, 3, 4, 5])
|
|
175
|
+
const without3 = numbers.remove(3) // List([1, 2, 4])
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Try
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { Try } from "functype"
|
|
182
|
+
|
|
183
|
+
// Safely execute code that might throw
|
|
184
|
+
const result = Try(() => {
|
|
185
|
+
// Potentially throwing operation
|
|
186
|
+
return JSON.parse('{"name": "John"}')
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
// Handle success/failure
|
|
190
|
+
if (result.isSuccess()) {
|
|
191
|
+
console.log("Result:", result.get())
|
|
192
|
+
} else {
|
|
193
|
+
console.error("Error:", result.error)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Transform with map (only applies on Success)
|
|
197
|
+
const name = result.map((obj) => obj.name)
|
|
198
|
+
|
|
199
|
+
// Convert to Either
|
|
200
|
+
const either = result.toEither()
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Task
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
import { Task } from "functype"
|
|
207
|
+
|
|
208
|
+
// Synchronous operations with error handling
|
|
209
|
+
const syncResult = Task().Sync(
|
|
210
|
+
() => "success",
|
|
211
|
+
(error) => new Error(`Failed: ${error}`),
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
// Asynchronous operations
|
|
215
|
+
const asyncTask = async () => {
|
|
216
|
+
const result = await Task().Async(
|
|
217
|
+
async () => await fetchData(),
|
|
218
|
+
async (error) => new Error(`Fetch failed: ${error}`),
|
|
219
|
+
)
|
|
220
|
+
return result
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Converting promise-based functions to Task
|
|
224
|
+
const fetchUserAPI = (userId: string): Promise<User> => fetch(`/api/users/${userId}`).then((r) => r.json())
|
|
225
|
+
|
|
226
|
+
// Use the adapter pattern for seamless integration
|
|
227
|
+
const fetchUser = Task({ name: "UserFetch" }).fromPromise(fetchUserAPI)
|
|
228
|
+
|
|
229
|
+
// Later use it with standard promise patterns
|
|
230
|
+
fetchUser("user123")
|
|
231
|
+
.then((user) => console.log(user))
|
|
232
|
+
.catch((error) => console.error(error))
|
|
233
|
+
|
|
234
|
+
// Or convert Task results back to promises
|
|
235
|
+
const taskResult = Task().Sync(() => "hello world")
|
|
236
|
+
const promise = Task().toPromise(taskResult) // Promise<string>
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Type Safety
|
|
240
|
+
|
|
241
|
+
Functype leverages TypeScript's advanced type system to provide compile-time safety for functional patterns, ensuring that your code is both robust and maintainable.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
// Type inference works seamlessly
|
|
245
|
+
const option = Option(42)
|
|
246
|
+
// Inferred as number
|
|
247
|
+
const mappedValue = option.map((x) => x.toString())
|
|
248
|
+
// Inferred as string
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Contributing
|
|
252
|
+
|
|
253
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
254
|
+
|
|
255
|
+
## License
|
|
256
|
+
|
|
257
|
+
MIT License
|
|
258
|
+
|
|
259
|
+
Copyright (c) 2025 Jordan Burke
|
|
260
|
+
|
|
261
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
262
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
263
|
+
in the Software without restriction, including without limitation the rights
|
|
264
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
265
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
266
|
+
furnished to do so, subject to the following conditions:
|
|
267
|
+
|
|
268
|
+
The above copyright notice and this permission notice shall be included in all
|
|
269
|
+
copies or substantial portions of the Software.
|
|
270
|
+
|
|
271
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
272
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
273
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
274
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
275
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
276
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
277
|
+
SOFTWARE.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
type SingleType = unknown;
|
|
2
|
+
type ArrayType = SingleType[];
|
|
3
|
+
type Type = SingleType | ArrayType;
|
|
4
|
+
type AbstractFunctor<A extends Type> = {
|
|
5
|
+
map(f: (value: A) => Type): AbstractFunctor<Type>;
|
|
6
|
+
flatMap(f: (value: A) => AbstractFunctor<Type>): AbstractFunctor<Type>;
|
|
7
|
+
};
|
|
8
|
+
type Functor<A extends Type> = AbstractFunctor<A> & {
|
|
9
|
+
map<B extends Type>(f: (value: A) => B): Functor<B>;
|
|
10
|
+
flatMap<B extends Type>(f: (value: A) => Functor<B>): Functor<B>;
|
|
11
|
+
};
|
|
12
|
+
type AsyncFunctor<A extends Type> = {
|
|
13
|
+
flatMapAsync(f: (value: A) => PromiseLike<AsyncFunctor<A>>): PromiseLike<AsyncFunctor<A>>;
|
|
14
|
+
};
|
|
15
|
+
type ArrayFunctor<A extends ArrayType> = AbstractFunctor<A> & {
|
|
16
|
+
map<U extends ArrayType>(f: (value: A) => U): ArrayFunctor<U>;
|
|
17
|
+
flatMap<U extends ArrayType>(f: (value: A) => ArrayFunctor<U>): ArrayFunctor<U>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type TypeableParams<Tag extends string, T> = {
|
|
21
|
+
_tag: Tag;
|
|
22
|
+
impl: T;
|
|
23
|
+
};
|
|
24
|
+
type ExtractTag<T> = T extends Typeable<infer Tag, unknown> ? Tag : never;
|
|
25
|
+
type Typeable<Tag extends string, T = object> = T & {
|
|
26
|
+
readonly _tag: Tag;
|
|
27
|
+
};
|
|
28
|
+
declare function Typeable<Tag extends string, T>({ _tag, impl }: TypeableParams<Tag, T>): Typeable<Tag, T>;
|
|
29
|
+
declare function isTypeable<T>(value: unknown, tag: string): value is T;
|
|
30
|
+
|
|
31
|
+
type ValuableParams<Tag extends string, T, V> = {
|
|
32
|
+
_tag: Tag;
|
|
33
|
+
impl: T;
|
|
34
|
+
value: V;
|
|
35
|
+
};
|
|
36
|
+
declare function Valuable<Tag extends string, V, T = object>(params: ValuableParams<Tag, T, V>): T & {
|
|
37
|
+
toValue: () => {
|
|
38
|
+
_tag: Tag;
|
|
39
|
+
value: V;
|
|
40
|
+
};
|
|
41
|
+
_tag: Tag;
|
|
42
|
+
};
|
|
43
|
+
type Valuable<Tag extends string, V, T = object> = ReturnType<typeof Valuable<Tag, V, T>>;
|
|
44
|
+
|
|
45
|
+
type Tuple<T extends ArrayType> = {
|
|
46
|
+
get<K extends number>(index: K): T[K];
|
|
47
|
+
map<U extends ArrayType>(f: (value: T) => U): Tuple<U>;
|
|
48
|
+
flatMap<U extends ArrayType>(f: (value: T) => Tuple<U>): Tuple<U>;
|
|
49
|
+
toArray(): T;
|
|
50
|
+
[Symbol.iterator](): Iterator<T[number]>;
|
|
51
|
+
} & ArrayFunctor<T> & Typeable<"Tuple"> & Valuable<"Tuple", T>;
|
|
52
|
+
declare const Tuple: <T extends ArrayType>(values: T) => Tuple<T>;
|
|
53
|
+
|
|
54
|
+
export { type AsyncFunctor as A, type ExtractTag as E, type Functor as F, type SingleType as S, Typeable as T, Valuable as V, type Type as a, Tuple as b, type ArrayType as c, type AbstractFunctor as d, type ArrayFunctor as e, type TypeableParams as f, isTypeable as i };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
type Unbrand<T> = T extends Brand<string, infer U> ? U : never;
|
|
2
|
+
type ExtractBrand<T> = T extends Brand<infer K, unknown> ? K : never;
|
|
3
|
+
/**
|
|
4
|
+
* Brand is a utility for creating nominal typing in TypeScript
|
|
5
|
+
* It allows for creating distinct types that are structurally identical
|
|
6
|
+
* but considered different by TypeScript's type system
|
|
7
|
+
*/
|
|
8
|
+
type Brand<K extends string, T> = T & {
|
|
9
|
+
readonly __brand: K;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Helper to create a branded type
|
|
13
|
+
* @param value - The value to brand
|
|
14
|
+
* @returns The branded value
|
|
15
|
+
*/
|
|
16
|
+
declare function Brand<K extends string, T>(brand: K, value: T): Brand<K, T>;
|
|
17
|
+
/**
|
|
18
|
+
* Helper to remove a brand from a value
|
|
19
|
+
* @param branded - The branded value
|
|
20
|
+
* @returns The original value without the brand
|
|
21
|
+
*/
|
|
22
|
+
declare function unbrand<T>(branded: Brand<string, T>): T;
|
|
23
|
+
/**
|
|
24
|
+
* Type guard for checking if a value has a specific brand
|
|
25
|
+
* @param value - The value to check
|
|
26
|
+
* @param brand - The brand to check for
|
|
27
|
+
* @returns True if the value has the specified brand
|
|
28
|
+
*
|
|
29
|
+
* Note: Since brands are phantom types that exist only at compile time,
|
|
30
|
+
* this function can only provide a runtime approximation. It always returns true
|
|
31
|
+
* for non-null values, as we have no way to actually check the brand at runtime.
|
|
32
|
+
* This function is primarily for API consistency and documentation purposes.
|
|
33
|
+
*/
|
|
34
|
+
declare function hasBrand<K extends string, T>(value: unknown, brand: K): value is Brand<K, T>;
|
|
35
|
+
/**
|
|
36
|
+
* Create a branded type constructor for a specific brand
|
|
37
|
+
* @param brand - The brand name
|
|
38
|
+
* @returns A function that brands values with the specified brand
|
|
39
|
+
*/
|
|
40
|
+
declare function createBrander<K extends string, T>(brand: K): (value: T) => Brand<K, T>;
|
|
41
|
+
type BrandedString<K extends string> = Brand<K, string>;
|
|
42
|
+
declare const BrandedString: <K extends string>(brand: K) => (value: string) => BrandedString<K>;
|
|
43
|
+
type BrandedNumber<K extends string> = Brand<K, number>;
|
|
44
|
+
declare const BrandedNumber: <K extends string>(brand: K) => (value: number) => BrandedNumber<K>;
|
|
45
|
+
type BrandedBoolean<K extends string> = Brand<K, boolean>;
|
|
46
|
+
declare const BrandedBoolean: <K extends string>(brand: K) => (value: boolean) => BrandedBoolean<K>;
|
|
47
|
+
|
|
48
|
+
export { Brand, BrandedBoolean, BrandedNumber, BrandedString, type ExtractBrand, type Unbrand, createBrander, hasBrand, unbrand };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.mjs"}
|