functype 0.8.55 → 0.8.60

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 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,245 @@
1
- # Functype
2
-
3
- ![NPM Version](https://img.shields.io/npm/v/functype?link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Ffunctype)
4
- [![Node.js Build](https://github.com/jordanburke/functype/actions/workflows/pnpm-build.yml/badge.svg)](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
- ## Core Principles
11
-
12
- - **Immutability**: All data structures are immutable, promoting predictable and side-effect-free code
13
- - **Type Safety**: Leverages TypeScript's type system to ensure compile-time safety
14
- - **Composability**: Provides abstractions for building complex programs from simple components
15
- - **Functional Paradigms**: Embraces concepts like monads, functors, and type classes
16
-
17
- ## Key Features
18
-
19
- - **Option Type**: Handle nullable values with `Some` and `None` types
20
- - **Either Type**: Express computation results with potential failures using `Left` and `Right`
21
- - **List, Set, Map**: Immutable collection types with functional operators
22
- - **Try Type**: Safely execute operations that might throw exceptions
23
- - **Task**: Handle synchronous and asynchronous operations with error handling
24
- - **Tuple**: Type-safe fixed-length arrays
25
- - **Typeable**: Runtime type identification with compile-time safety
26
-
27
- ## Installation
28
-
29
- ```bash
30
- # NPM
31
- npm install functype
32
-
33
- # Yarn
34
- yarn add functype
35
-
36
- # PNPM
37
- pnpm add functype
38
-
39
- # Bun
40
- bun add functype
41
- ```
42
-
43
- ## Usage Examples
44
-
45
- ### Option
46
-
47
- ```typescript
48
- import { Option, Some, None } from "functype"
49
-
50
- // Create options
51
- const value = Option("hello") // Some("hello")
52
- const empty = Option(null) // None
53
- const explicit = Some(42) // Some(42)
54
-
55
- // Transform values
56
- const length = value.map((s) => s.length) // Some(5)
57
- const nothing = empty.map((s) => s.length) // None
58
-
59
- // Handle default values
60
- const result = value.getOrElse("world") // "hello"
61
- const fallback = empty.getOrElse("world") // "world"
62
-
63
- // Conditionally filter
64
- const filtered = value.filter((s) => s.length > 10) // None
65
- ```
66
-
67
- ### Either
68
-
69
- ```typescript
70
- import { Either, Right, Left } from "functype"
71
-
72
- // Success case
73
- const success = Right<string, number>(42)
74
- // Error case
75
- const failure = Left<string, number>("error")
76
-
77
- // Transform values (map only applies to Right)
78
- const doubled = success.map((x) => x * 2) // Right(84)
79
- const stillError = failure.map((x) => x * 2) // Left("error")
80
-
81
- // Handle errors
82
- const value = success.getOrElse(0) // 42
83
- const fallback = failure.getOrElse(0) // 0
84
-
85
- // Pattern matching with fold
86
- const result = success.fold(
87
- (err) => `Error: ${err}`,
88
- (val) => `Success: ${val}`,
89
- ) // "Success: 42"
90
- ```
91
-
92
- ### List
93
-
94
- ```typescript
95
- import { List } from "functype"
96
-
97
- const numbers = List([1, 2, 3, 4])
98
-
99
- // Transform
100
- const doubled = numbers.map((x) => x * 2) // List([2, 4, 6, 8])
101
-
102
- // Filter
103
- const evens = numbers.filter((x) => x % 2 === 0) // List([2, 4])
104
-
105
- // Reduce
106
- const sum = numbers.foldLeft(0)((acc, x) => acc + x) // 10
107
-
108
- // Add/remove elements (immutably)
109
- const withFive = numbers.add(5) // List([1, 2, 3, 4, 5])
110
- const without3 = numbers.remove(3) // List([1, 2, 4])
111
- ```
112
-
113
- ### Try
114
-
115
- ```typescript
116
- import { Try } from "functype"
117
-
118
- // Safely execute code that might throw
119
- const result = Try(() => {
120
- // Potentially throwing operation
121
- return JSON.parse('{"name": "John"}')
122
- })
123
-
124
- // Handle success/failure
125
- if (result.isSuccess()) {
126
- console.log("Result:", result.get())
127
- } else {
128
- console.error("Error:", result.error)
129
- }
130
-
131
- // Transform with map (only applies on Success)
132
- const name = result.map((obj) => obj.name)
133
-
134
- // Convert to Either
135
- const either = result.toEither()
136
- ```
137
-
138
- ### Task
139
-
140
- ```typescript
141
- import { Task } from "functype"
142
-
143
- // Synchronous operations with error handling
144
- const syncResult = Task().Task(
145
- () => "success",
146
- (error) => new Error(`Failed: ${error}`),
147
- )
148
-
149
- // Asynchronous operations
150
- const asyncTask = async () => {
151
- const result = await Task().Async(
152
- async () => await fetchData(),
153
- async (error) => new Error(`Fetch failed: ${error}`),
154
- )
155
- return result
156
- }
157
- ```
158
-
159
- ## Type Safety
160
-
161
- Functype leverages TypeScript's advanced type system to provide compile-time safety for functional patterns, ensuring that your code is both robust and maintainable.
162
-
163
- ```typescript
164
- // Type inference works seamlessly
165
- const option = Option(42)
166
- // Inferred as number
167
- const mappedValue = option.map((x) => x.toString())
168
- // Inferred as string
169
- ```
170
-
171
- ## Contributing
172
-
173
- Contributions are welcome! Please feel free to submit a Pull Request.
174
-
175
- ## License
176
-
177
- MIT License
178
-
179
- Copyright (c) 2025 Jordan Burke
180
-
181
- Permission is hereby granted, free of charge, to any person obtaining a copy
182
- of this software and associated documentation files (the "Software"), to deal
183
- in the Software without restriction, including without limitation the rights
184
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
185
- copies of the Software, and to permit persons to whom the Software is
186
- furnished to do so, subject to the following conditions:
187
-
188
- The above copyright notice and this permission notice shall be included in all
189
- copies or substantial portions of the Software.
190
-
191
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
192
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
194
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
195
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
196
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
197
- SOFTWARE.
1
+ # Functype
2
+
3
+ ![NPM Version](https://img.shields.io/npm/v/functype?link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Ffunctype)
4
+ [![Node.js Build](https://github.com/jordanburke/functype/actions/workflows/pnpm-build.yml/badge.svg)](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
+
47
+ ### API Consistency
48
+
49
+ - [ ] Ensure all modules follow the Scala-inspired pattern:
50
+ - Constructor functions that return objects with methods
51
+ - Object methods for common operations
52
+ - Companion functions for additional utilities
53
+ - [ ] Align Task API with other monadic structures
54
+ - [ ] Standardize import patterns (@ imports vs relative paths)
55
+ - [ ] Implement consistent error handling strategy for async operations
56
+
57
+ ### Testing and Documentation
58
+
59
+ - [ ] Add observable test coverage metrics
60
+ - [x] Implement property-based testing
61
+ - [ ] Expand error handling tests
62
+ - [ ] Add interoperability tests with other libraries
63
+
64
+ ### TypeScript Improvements
65
+
66
+ - [x] Enable stricter TypeScript settings (noImplicitAny: true)
67
+ - [x] Add noUncheckedIndexedAccess for safer array indexing
68
+ - [ ] Improve support for higher-kinded types:
69
+ - Current type parameters work well for first-order types
70
+ - Expand to support type constructors as parameters (F<A> => F<B>)
71
+ - [ ] Add branded/nominal types for stronger type safety
72
+ - [ ] Implement more type-level utilities (conditional types, template literals)
73
+ - [ ] Leverage newer TypeScript features (const type parameters, tuple manipulation)
74
+
75
+ ## Installation
76
+
77
+ ```bash
78
+ # NPM
79
+ npm install functype
80
+
81
+ # Yarn
82
+ yarn add functype
83
+
84
+ # PNPM
85
+ pnpm add functype
86
+
87
+ # Bun
88
+ bun add functype
89
+ ```
90
+
91
+ ## Usage Examples
92
+
93
+ ### Option
94
+
95
+ ```typescript
96
+ import { Option, Some, None } from "functype"
97
+
98
+ // Create options
99
+ const value = Option("hello") // Some("hello")
100
+ const empty = Option(null) // None
101
+ const explicit = Some(42) // Some(42)
102
+
103
+ // Transform values
104
+ const length = value.map((s) => s.length) // Some(5)
105
+ const nothing = empty.map((s) => s.length) // None
106
+
107
+ // Handle default values
108
+ const result = value.getOrElse("world") // "hello"
109
+ const fallback = empty.getOrElse("world") // "world"
110
+
111
+ // Conditionally filter
112
+ const filtered = value.filter((s) => s.length > 10) // None
113
+ ```
114
+
115
+ ### Either
116
+
117
+ ```typescript
118
+ import { Either, Right, Left } from "functype"
119
+
120
+ // Success case
121
+ const success = Right<string, number>(42)
122
+ // Error case
123
+ const failure = Left<string, number>("error")
124
+
125
+ // Transform values (map only applies to Right)
126
+ const doubled = success.map((x) => x * 2) // Right(84)
127
+ const stillError = failure.map((x) => x * 2) // Left("error")
128
+
129
+ // Handle errors
130
+ const value = success.getOrElse(0) // 42
131
+ const fallback = failure.getOrElse(0) // 0
132
+
133
+ // Pattern matching with fold
134
+ const result = success.fold(
135
+ (err) => `Error: ${err}`,
136
+ (val) => `Success: ${val}`,
137
+ ) // "Success: 42"
138
+ ```
139
+
140
+ ### List
141
+
142
+ ```typescript
143
+ import { List } from "functype"
144
+
145
+ const numbers = List([1, 2, 3, 4])
146
+
147
+ // Transform
148
+ const doubled = numbers.map((x) => x * 2) // List([2, 4, 6, 8])
149
+
150
+ // Filter
151
+ const evens = numbers.filter((x) => x % 2 === 0) // List([2, 4])
152
+
153
+ // Reduce
154
+ const sum = numbers.foldLeft(0)((acc, x) => acc + x) // 10
155
+
156
+ // Add/remove elements (immutably)
157
+ const withFive = numbers.add(5) // List([1, 2, 3, 4, 5])
158
+ const without3 = numbers.remove(3) // List([1, 2, 4])
159
+ ```
160
+
161
+ ### Try
162
+
163
+ ```typescript
164
+ import { Try } from "functype"
165
+
166
+ // Safely execute code that might throw
167
+ const result = Try(() => {
168
+ // Potentially throwing operation
169
+ return JSON.parse('{"name": "John"}')
170
+ })
171
+
172
+ // Handle success/failure
173
+ if (result.isSuccess()) {
174
+ console.log("Result:", result.get())
175
+ } else {
176
+ console.error("Error:", result.error)
177
+ }
178
+
179
+ // Transform with map (only applies on Success)
180
+ const name = result.map((obj) => obj.name)
181
+
182
+ // Convert to Either
183
+ const either = result.toEither()
184
+ ```
185
+
186
+ ### Task
187
+
188
+ ```typescript
189
+ import { Task } from "functype"
190
+
191
+ // Synchronous operations with error handling
192
+ const syncResult = Task().Task(
193
+ () => "success",
194
+ (error) => new Error(`Failed: ${error}`),
195
+ )
196
+
197
+ // Asynchronous operations
198
+ const asyncTask = async () => {
199
+ const result = await Task().Async(
200
+ async () => await fetchData(),
201
+ async (error) => new Error(`Fetch failed: ${error}`),
202
+ )
203
+ return result
204
+ }
205
+ ```
206
+
207
+ ## Type Safety
208
+
209
+ Functype leverages TypeScript's advanced type system to provide compile-time safety for functional patterns, ensuring that your code is both robust and maintainable.
210
+
211
+ ```typescript
212
+ // Type inference works seamlessly
213
+ const option = Option(42)
214
+ // Inferred as number
215
+ const mappedValue = option.map((x) => x.toString())
216
+ // Inferred as string
217
+ ```
218
+
219
+ ## Contributing
220
+
221
+ Contributions are welcome! Please feel free to submit a Pull Request.
222
+
223
+ ## License
224
+
225
+ MIT License
226
+
227
+ Copyright (c) 2025 Jordan Burke
228
+
229
+ Permission is hereby granted, free of charge, to any person obtaining a copy
230
+ of this software and associated documentation files (the "Software"), to deal
231
+ in the Software without restriction, including without limitation the rights
232
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
233
+ copies of the Software, and to permit persons to whom the Software is
234
+ furnished to do so, subject to the following conditions:
235
+
236
+ The above copyright notice and this permission notice shall be included in all
237
+ copies or substantial portions of the Software.
238
+
239
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
240
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
242
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
243
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
244
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
245
+ SOFTWARE.