functype 0.8.62 → 0.8.64
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 -277
- package/dist/Tuple-DfdXAbL_.d.ts +52 -0
- package/dist/branded/index.d.ts +48 -1
- package/dist/branded/index.mjs +2 -23
- package/dist/branded/index.mjs.map +1 -1
- package/dist/chunk-42EDYQ3O.mjs +2 -0
- package/dist/chunk-42EDYQ3O.mjs.map +1 -0
- package/dist/chunk-DWRQQO3M.mjs +2 -0
- package/dist/chunk-DWRQQO3M.mjs.map +1 -0
- package/dist/chunk-SNAYIJNJ.mjs +2 -0
- package/dist/chunk-SNAYIJNJ.mjs.map +1 -0
- package/dist/either/index.d.ts +3 -1
- package/dist/either/index.mjs +2 -13
- package/dist/either/index.mjs.map +1 -1
- package/dist/fpromise/index.d.ts +3 -2
- package/dist/fpromise/index.mjs +2 -6
- package/dist/fpromise/index.mjs.map +1 -1
- package/dist/index.d.ts +3 -28
- package/dist/index.mjs +2 -174
- package/dist/index.mjs.map +1 -1
- package/dist/list/index.d.ts +3 -1
- package/dist/list/index.mjs +2 -5
- package/dist/list/index.mjs.map +1 -1
- package/dist/map/index.d.ts +3 -2
- package/dist/map/index.mjs +2 -7
- package/dist/map/index.mjs.map +1 -1
- package/dist/option/index.d.ts +870 -1
- package/dist/option/index.mjs +2 -7
- package/dist/option/index.mjs.map +1 -1
- package/dist/set/index.d.ts +3 -2
- package/dist/set/index.mjs +2 -5
- package/dist/set/index.mjs.map +1 -1
- package/dist/try/index.d.ts +3 -1
- package/dist/try/index.mjs +2 -44
- package/dist/try/index.mjs.map +1 -1
- package/dist/tuple/index.d.ts +1 -1
- package/dist/tuple/index.mjs +2 -27
- package/dist/tuple/index.mjs.map +1 -1
- package/package.json +6 -7
- package/dist/Map-BNxKlujn.mjs +0 -46
- package/dist/Map-BNxKlujn.mjs.map +0 -1
- package/dist/branded/Brand.d.ts +0 -46
- package/dist/collections/index.d.ts +0 -8
- package/dist/companion/Companion.d.ts +0 -23
- package/dist/companion/index.d.ts +0 -1
- package/dist/core/base/Base.d.ts +0 -10
- package/dist/core/base/index.d.ts +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/info/Info.d.ts +0 -26
- package/dist/core/info/index.d.ts +0 -1
- package/dist/core/task/Task.d.ts +0 -57
- package/dist/core/task/index.d.ts +0 -1
- package/dist/core/throwable/Throwable.d.ts +0 -18
- package/dist/core/throwable/index.d.ts +0 -1
- package/dist/either/Either.d.ts +0 -57
- package/dist/error/ParseError.d.ts +0 -6
- package/dist/error/index.d.ts +0 -1
- package/dist/fpromise/FPromise.d.ts +0 -369
- package/dist/fpromise/retry.d.ts +0 -106
- package/dist/functor/index.d.ts +0 -18
- package/dist/hkt/index.d.ts +0 -49
- package/dist/identity/Identity.d.ts +0 -5
- package/dist/identity/index.d.ts +0 -1
- package/dist/index-CgMYHgdG.mjs +0 -1286
- package/dist/index-CgMYHgdG.mjs.map +0 -1
- package/dist/iterable/index.d.ts +0 -25
- package/dist/list/List.d.ts +0 -45
- package/dist/map/Map.d.ts +0 -19
- package/dist/map/shim.d.ts +0 -2
- package/dist/option/Option.d.ts +0 -164
- package/dist/serializable/Serializable.d.ts +0 -15
- package/dist/serializable/index.d.ts +0 -1
- package/dist/set/Set.d.ts +0 -16
- package/dist/set/shim.d.ts +0 -2
- package/dist/try/Try.d.ts +0 -20
- package/dist/tuple/Tuple.d.ts +0 -11
- package/dist/typeable/Typeable.d.ts +0 -10
- package/dist/typeable/index.d.ts +0 -1
- package/dist/util/index.d.ts +0 -3
- package/dist/util/isIterable.d.ts +0 -1
- package/dist/valuable/Valuable.d.ts +0 -13
- package/dist/valuable/index.d.ts +0 -1
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,277 +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
|
-
[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.
|
|
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,52 @@
|
|
|
1
|
+
type Type = unknown;
|
|
2
|
+
type AbstractFunctor<A extends Type> = {
|
|
3
|
+
map(f: (value: A) => Type): AbstractFunctor<Type>;
|
|
4
|
+
flatMap(f: (value: A) => AbstractFunctor<Type>): AbstractFunctor<Type>;
|
|
5
|
+
};
|
|
6
|
+
type Functor<A extends Type> = AbstractFunctor<A> & {
|
|
7
|
+
map<B extends Type>(f: (value: A) => B): Functor<B>;
|
|
8
|
+
flatMap<B extends Type>(f: (value: A) => Functor<B>): Functor<B>;
|
|
9
|
+
};
|
|
10
|
+
type AsyncFunctor<A extends Type> = {
|
|
11
|
+
flatMapAsync(f: (value: A) => PromiseLike<AsyncFunctor<A>>): PromiseLike<AsyncFunctor<A>>;
|
|
12
|
+
};
|
|
13
|
+
type ArrayFunctor<A extends Type[]> = AbstractFunctor<A> & {
|
|
14
|
+
map<U extends Type[]>(f: (value: A) => U): ArrayFunctor<U>;
|
|
15
|
+
flatMap<U extends Type[]>(f: (value: A) => ArrayFunctor<U>): ArrayFunctor<U>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type TypeableParams<Tag extends string, T> = {
|
|
19
|
+
_tag: Tag;
|
|
20
|
+
impl: T;
|
|
21
|
+
};
|
|
22
|
+
type ExtractTag<T> = T extends Typeable<infer Tag, unknown> ? Tag : never;
|
|
23
|
+
type Typeable<Tag extends string, T = object> = T & {
|
|
24
|
+
readonly _tag: Tag;
|
|
25
|
+
};
|
|
26
|
+
declare function Typeable<Tag extends string, T>({ _tag, impl }: TypeableParams<Tag, T>): Typeable<Tag, T>;
|
|
27
|
+
declare function isTypeable<T>(value: unknown, tag: string): value is T;
|
|
28
|
+
|
|
29
|
+
type ValuableParams<Tag extends string, T, V> = {
|
|
30
|
+
_tag: Tag;
|
|
31
|
+
impl: T;
|
|
32
|
+
value: V;
|
|
33
|
+
};
|
|
34
|
+
declare function Valuable<Tag extends string, V, T = object>(params: ValuableParams<Tag, T, V>): T & {
|
|
35
|
+
toValue: () => {
|
|
36
|
+
_tag: Tag;
|
|
37
|
+
value: V;
|
|
38
|
+
};
|
|
39
|
+
_tag: Tag;
|
|
40
|
+
};
|
|
41
|
+
type Valuable<Tag extends string, V, T = object> = ReturnType<typeof Valuable<Tag, V, T>>;
|
|
42
|
+
|
|
43
|
+
type Tuple<T extends Type[]> = {
|
|
44
|
+
get<K extends number>(index: K): T[K];
|
|
45
|
+
map<U extends Type[]>(f: (value: T) => U): Tuple<U>;
|
|
46
|
+
flatMap<U extends Type[]>(f: (value: T) => Tuple<U>): Tuple<U>;
|
|
47
|
+
toArray(): T;
|
|
48
|
+
[Symbol.iterator](): Iterator<T[number]>;
|
|
49
|
+
} & ArrayFunctor<T> & Typeable<"Tuple"> & Valuable<"Tuple", T>;
|
|
50
|
+
declare const Tuple: <T extends Type[]>(values: T) => Tuple<T>;
|
|
51
|
+
|
|
52
|
+
export { type AsyncFunctor as A, type ExtractTag as E, type Functor as F, Typeable as T, Valuable as V, type Type as a, Tuple as b, type AbstractFunctor as c, type ArrayFunctor as d, type TypeableParams as e, isTypeable as i };
|