functype 0.49.0 → 0.50.0
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/dist/Brand-BJIRbUKB.d.ts +54 -0
- package/dist/{Typeable-lfO2nRVW.d.ts → Tuple-DmywDCE9.d.ts} +75 -1
- package/dist/Tuple-eVwTSeSr.js +1 -0
- package/dist/branded/index.d.ts +1 -53
- package/dist/cli/exports.js +1 -1
- package/dist/cli/index.js +4 -4
- package/dist/do/index.d.ts +2 -184
- package/dist/do/index.js +1 -1
- package/dist/either/index.d.ts +1 -187
- package/dist/either/index.js +1 -2
- package/dist/{full-interfaces-DzRVS-A-.js → full-interfaces-DVRjnPWW.js} +13 -1
- package/dist/{Functype-rgysbeU6.d.ts → index-OdnROQtM.d.ts} +915 -8
- package/dist/index.d.ts +4 -12
- package/dist/index.js +1 -1
- package/dist/list/index.d.ts +1 -84
- package/dist/list/index.js +1 -2
- package/dist/map/index.d.ts +1 -72
- package/dist/map/index.js +1 -2
- package/dist/option/index.d.ts +1 -242
- package/dist/option/index.js +1 -2
- package/dist/set/index.d.ts +1 -53
- package/dist/set/index.js +1 -2
- package/dist/src-ByDeJzWj.js +19 -0
- package/dist/try/index.d.ts +1 -96
- package/dist/try/index.js +1 -2
- package/dist/tuple/index.d.ts +1 -76
- package/dist/tuple/index.js +1 -1
- package/package.json +7 -7
- package/dist/Valuable-GZAXcYok.js +0 -13
- package/dist/stringify-DepodSy0.js +0 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//#region src/branded/Brand.d.ts
|
|
2
|
+
type Brand<K extends string, T> = T & {
|
|
3
|
+
readonly __brand: K;
|
|
4
|
+
};
|
|
5
|
+
type Unwrap<T> = T extends Brand<string, infer U> ? U : T;
|
|
6
|
+
type ExtractBrand<T> = T extends Brand<infer K, unknown> ? K : never;
|
|
7
|
+
/**
|
|
8
|
+
* Brand is a utility for creating nominal typing in TypeScript.
|
|
9
|
+
* It creates phantom types that exist only at compile time.
|
|
10
|
+
* At runtime, the branded value IS the primitive value.
|
|
11
|
+
*
|
|
12
|
+
* @param _brand
|
|
13
|
+
* @param value - The value to brand
|
|
14
|
+
* @returns The value with phantom type brand
|
|
15
|
+
*/
|
|
16
|
+
declare function Brand<K extends string, T>(_brand: K, value: T): Brand<K, T>;
|
|
17
|
+
/**
|
|
18
|
+
* Helper to unwrap a branded value to its underlying type
|
|
19
|
+
* Works with both Brand and ValidatedBrand
|
|
20
|
+
* @param branded - The branded value (can be null or undefined)
|
|
21
|
+
* @returns The original value without the brand
|
|
22
|
+
*
|
|
23
|
+
* Note: Also exported as 'unwrap' from 'functype/branded' for convenience
|
|
24
|
+
*/
|
|
25
|
+
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T>): T;
|
|
26
|
+
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T> | null): T | null;
|
|
27
|
+
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T> | undefined): T | undefined;
|
|
28
|
+
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T> | null | undefined): T | null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Type guard for checking if a value has a specific brand
|
|
31
|
+
* @param value - The value to check
|
|
32
|
+
* @param _brand - The brand to check for (unused at runtime)
|
|
33
|
+
* @returns True if the value has the specified brand
|
|
34
|
+
*
|
|
35
|
+
* Note: Since brands are phantom types that exist only at compile time,
|
|
36
|
+
* this function can only provide a runtime approximation. It always returns true
|
|
37
|
+
* for non-null values, as we have no way to actually check the brand at runtime.
|
|
38
|
+
* This function is primarily for API consistency and documentation purposes.
|
|
39
|
+
*/
|
|
40
|
+
declare function hasBrand<K extends string, T>(value: unknown, _brand: K): value is Brand<K, T>;
|
|
41
|
+
/**
|
|
42
|
+
* Create a branded type constructor for a specific brand
|
|
43
|
+
* @param brand - The brand name
|
|
44
|
+
* @returns A function that brands values with the specified brand
|
|
45
|
+
*/
|
|
46
|
+
declare function createBrander<K extends string, T>(brand: K): (value: T) => Brand<K, T>;
|
|
47
|
+
type BrandedString<K extends string> = Brand<K, string>;
|
|
48
|
+
type BrandedNumber<K extends string> = Brand<K, number>;
|
|
49
|
+
type BrandedBoolean<K extends string> = Brand<K, boolean>;
|
|
50
|
+
declare const BrandedString: <K extends string>(brand: K) => (value: string) => BrandedString<K>;
|
|
51
|
+
declare const BrandedNumber: <K extends string>(brand: K) => (value: number) => BrandedNumber<K>;
|
|
52
|
+
declare const BrandedBoolean: <K extends string>(brand: K) => (value: boolean) => BrandedBoolean<K>;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { ExtractBrand as a, hasBrand as c, BrandedString as i, unwrapBrand as l, BrandedBoolean as n, Unwrap as o, BrandedNumber as r, createBrander as s, Brand as t };
|
|
@@ -101,4 +101,78 @@ declare function Typeable<Tag extends string, T>({
|
|
|
101
101
|
*/
|
|
102
102
|
declare function isTypeable<T>(value: unknown, tag: string): value is T;
|
|
103
103
|
//#endregion
|
|
104
|
-
|
|
104
|
+
//#region src/tuple/Tuple.d.ts
|
|
105
|
+
interface Tuple<T extends Type[]> extends Foldable<T[number]>, Pipe<Tuple<T>>, Serializable<Tuple<T>>, Typeable<"Tuple"> {
|
|
106
|
+
readonly [Symbol.toStringTag]: string;
|
|
107
|
+
get<K extends number>(index: K): T[K];
|
|
108
|
+
map<U extends Type[]>(f: (value: T) => U): Tuple<U>;
|
|
109
|
+
flatMap<U extends Type[]>(f: (value: T) => Tuple<U>): Tuple<U>;
|
|
110
|
+
toArray(): T;
|
|
111
|
+
length: number;
|
|
112
|
+
[Symbol.iterator](): Iterator<T[number]>;
|
|
113
|
+
toString(): string;
|
|
114
|
+
toValue(): {
|
|
115
|
+
_tag: "Tuple";
|
|
116
|
+
value: T;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Tuple provides a type-safe, fixed-length array with functional operations.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* // Creating tuples
|
|
124
|
+
* const t1 = Tuple([1, "hello", true])
|
|
125
|
+
* const t2 = Tuple.of(1, "hello", true)
|
|
126
|
+
* const pair = Tuple.pair("key", 42)
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // Type-safe access
|
|
130
|
+
* const triple = Tuple.triple("x", 10, true)
|
|
131
|
+
* const first = triple.get(0) // string
|
|
132
|
+
* const second = triple.get(1) // number
|
|
133
|
+
* const third = triple.get(2) // boolean
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* // Functional operations
|
|
137
|
+
* const doubled = Tuple([1, 2, 3])
|
|
138
|
+
* .map(arr => arr.map(x => x * 2))
|
|
139
|
+
* .toArray() // [2, 4, 6]
|
|
140
|
+
*/
|
|
141
|
+
declare const Tuple: (<T extends Type[]>(values: T) => Tuple<T>) & {
|
|
142
|
+
/**
|
|
143
|
+
* Create a Tuple from multiple arguments
|
|
144
|
+
* @example
|
|
145
|
+
* const t = Tuple.of(1, "hello", true)
|
|
146
|
+
* // TypeScript infers: Tuple<[number, string, boolean]>
|
|
147
|
+
*/
|
|
148
|
+
of: <T extends Type[]>(...values: T) => Tuple<T>;
|
|
149
|
+
/**
|
|
150
|
+
* Create a Tuple of size 2 (pair)
|
|
151
|
+
* @example
|
|
152
|
+
* const pair = Tuple.pair("key", 42)
|
|
153
|
+
* // TypeScript infers: Tuple<[string, number]>
|
|
154
|
+
*/
|
|
155
|
+
pair: <A extends Type, B extends Type>(first: A, second: B) => Tuple<[A, B]>;
|
|
156
|
+
/**
|
|
157
|
+
* Create a Tuple of size 3 (triple)
|
|
158
|
+
* @example
|
|
159
|
+
* const triple = Tuple.triple("x", 10, true)
|
|
160
|
+
* // TypeScript infers: Tuple<[string, number, boolean]>
|
|
161
|
+
*/
|
|
162
|
+
triple: <A extends Type, B extends Type, C extends Type>(first: A, second: B, third: C) => Tuple<[A, B, C]>;
|
|
163
|
+
/**
|
|
164
|
+
* Create an empty Tuple
|
|
165
|
+
* @example
|
|
166
|
+
* const empty = Tuple.empty()
|
|
167
|
+
* // TypeScript infers: Tuple<[]>
|
|
168
|
+
*/
|
|
169
|
+
empty: () => Tuple<[]>;
|
|
170
|
+
/**
|
|
171
|
+
* Create a Tuple from an array (alias for constructor)
|
|
172
|
+
* @example
|
|
173
|
+
* const t = Tuple.from([1, 2, 3])
|
|
174
|
+
*/
|
|
175
|
+
from: <T extends Type[]>(values: T) => Tuple<T>;
|
|
176
|
+
};
|
|
177
|
+
//#endregion
|
|
178
|
+
export { isTypeable as a, Pipe as c, TypeableParams as i, Foldable as l, ExtractTag as n, Serializable as o, Typeable as r, SerializationMethods as s, Tuple as t, Type as u };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t){return Object.assign(e,t)}function t(t,n){return e(t,n)}function n(e){if(e===void 0||typeof e==`symbol`||typeof e==`function`)return;let t=new Set;function n(e){if(e===null)return`null`;switch(typeof e){case`string`:return JSON.stringify(e);case`number`:return isFinite(e)?String(e):`null`;case`boolean`:return String(e);case`bigint`:return`"${e}"`;case`undefined`:case`symbol`:case`function`:return}let r=e;if(t.has(r))return`"[Circular]"`;t.add(r);try{if(`toJSON`in r&&typeof r.toJSON==`function`)return n(r.toJSON());if(Array.isArray(r))return`[${r.map(e=>n(e)??`null`).join(`,`)}]`;let e=Object.keys(r).sort(),t=[];for(let i of e){let e=n(r[i]);e!==void 0&&t.push(`${JSON.stringify(i)}:${e}`)}return`{${t.join(`,`)}}`}finally{t.delete(r)}}try{return n(e)}catch{return}}const r=e=>{let t={[Symbol.toStringTag]:`Tuple`,_tag:`Tuple`,map:t=>i(t(e)),flatMap:t=>t(e),get:t=>e[t],toArray:()=>e,length:e.length,[Symbol.iterator](){let t=0;return{next:()=>t<e.length?{value:e[t++],done:!1}:{value:void 0,done:!0}}},fold:(t,n)=>e.length===0?t():n(e[0]),foldLeft:t=>n=>e.reduce(n,t),foldRight:t=>n=>e.reduceRight((e,t)=>n(t,e),t),pipe:e=>e(t),serialize:()=>({toJSON:()=>JSON.stringify({_tag:`Tuple`,value:e}),toYAML:()=>`_tag: Tuple\nvalue: ${n(e)}`,toBinary:()=>Buffer.from(JSON.stringify({_tag:`Tuple`,value:e})).toString(`base64`)}),toValue:()=>({_tag:`Tuple`,value:e}),toString:()=>`Tuple(${e.map(e=>String(e)).join(`, `)})`};return t},i=t(e=>r(e),{of:(...e)=>r(e),pair:(e,t)=>r([e,t]),triple:(e,t,n)=>r([e,t,n]),empty:()=>r([]),from:e=>r(e)});export{e as i,n,t as r,i as t};
|
package/dist/branded/index.d.ts
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
type Brand<K extends string, T> = T & {
|
|
3
|
-
readonly __brand: K;
|
|
4
|
-
};
|
|
5
|
-
type Unwrap<T> = T extends Brand<string, infer U> ? U : T;
|
|
6
|
-
type ExtractBrand<T> = T extends Brand<infer K, unknown> ? K : never;
|
|
7
|
-
/**
|
|
8
|
-
* Brand is a utility for creating nominal typing in TypeScript.
|
|
9
|
-
* It creates phantom types that exist only at compile time.
|
|
10
|
-
* At runtime, the branded value IS the primitive value.
|
|
11
|
-
*
|
|
12
|
-
* @param _brand
|
|
13
|
-
* @param value - The value to brand
|
|
14
|
-
* @returns The value with phantom type brand
|
|
15
|
-
*/
|
|
16
|
-
declare function Brand<K extends string, T>(_brand: K, value: T): Brand<K, T>;
|
|
17
|
-
/**
|
|
18
|
-
* Helper to unwrap a branded value to its underlying type
|
|
19
|
-
* Works with both Brand and ValidatedBrand
|
|
20
|
-
* @param branded - The branded value (can be null or undefined)
|
|
21
|
-
* @returns The original value without the brand
|
|
22
|
-
*
|
|
23
|
-
* Note: Also exported as 'unwrap' from 'functype/branded' for convenience
|
|
24
|
-
*/
|
|
25
|
-
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T>): T;
|
|
26
|
-
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T> | null): T | null;
|
|
27
|
-
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T> | undefined): T | undefined;
|
|
28
|
-
declare function unwrapBrand<K extends string, T>(branded: Brand<K, T> | null | undefined): T | null | undefined;
|
|
29
|
-
/**
|
|
30
|
-
* Type guard for checking if a value has a specific brand
|
|
31
|
-
* @param value - The value to check
|
|
32
|
-
* @param _brand - The brand to check for (unused at runtime)
|
|
33
|
-
* @returns True if the value has the specified brand
|
|
34
|
-
*
|
|
35
|
-
* Note: Since brands are phantom types that exist only at compile time,
|
|
36
|
-
* this function can only provide a runtime approximation. It always returns true
|
|
37
|
-
* for non-null values, as we have no way to actually check the brand at runtime.
|
|
38
|
-
* This function is primarily for API consistency and documentation purposes.
|
|
39
|
-
*/
|
|
40
|
-
declare function hasBrand<K extends string, T>(value: unknown, _brand: K): value is Brand<K, T>;
|
|
41
|
-
/**
|
|
42
|
-
* Create a branded type constructor for a specific brand
|
|
43
|
-
* @param brand - The brand name
|
|
44
|
-
* @returns A function that brands values with the specified brand
|
|
45
|
-
*/
|
|
46
|
-
declare function createBrander<K extends string, T>(brand: K): (value: T) => Brand<K, T>;
|
|
47
|
-
type BrandedString<K extends string> = Brand<K, string>;
|
|
48
|
-
type BrandedNumber<K extends string> = Brand<K, number>;
|
|
49
|
-
type BrandedBoolean<K extends string> = Brand<K, boolean>;
|
|
50
|
-
declare const BrandedString: <K extends string>(brand: K) => (value: string) => BrandedString<K>;
|
|
51
|
-
declare const BrandedNumber: <K extends string>(brand: K) => (value: number) => BrandedNumber<K>;
|
|
52
|
-
declare const BrandedBoolean: <K extends string>(brand: K) => (value: boolean) => BrandedBoolean<K>;
|
|
53
|
-
//#endregion
|
|
1
|
+
import { a as ExtractBrand, c as hasBrand, i as BrandedString, l as unwrapBrand, n as BrandedBoolean, o as Unwrap, r as BrandedNumber, s as createBrander, t as Brand } from "../Brand-BJIRbUKB.js";
|
|
54
2
|
export { Brand, BrandedBoolean, BrandedNumber, BrandedString, ExtractBrand, Unwrap, createBrander, hasBrand, unwrapBrand as unwrap, unwrapBrand };
|
package/dist/cli/exports.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as e,i as t,n,r,t as i}from"../full-interfaces-
|
|
1
|
+
import{a as e,i as t,n,r,t as i}from"../full-interfaces-DVRjnPWW.js";export{n as CATEGORIES,i as FULL_INTERFACES,r as INTERFACES,t as TYPES,e as VERSION};
|
package/dist/cli/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{
|
|
3
|
-
`)},l=(
|
|
2
|
+
import{Ct as e,W as t,ht as n}from"../src-ByDeJzWj.js";import{a as r,i,n as a,r as o,t as s}from"../full-interfaces-DVRjnPWW.js";const c=()=>{let t=n([`functype ${r} - Scala-inspired FP for TypeScript`,``]);return n(Object.entries(a)).foldLeft(t)((t,[r,a])=>{let o=t.add(r.toUpperCase());return n(a).foldLeft(o)((t,n)=>e(i[n]).fold(()=>t,e=>{let r=e.interfaces.length>0?` [${e.interfaces.join(`, `)}]`:``;return t.add(` ${n}${r}`).add(` ${e.description}`)})).add(``)}).concat(n([`Use: npx functype <Type> for details`,`Use: npx functype interfaces for interface reference`])).toArray().join(`
|
|
3
|
+
`)},l=(t,r)=>{let i=r.interfaces.length>0?` [${r.interfaces.join(`, `)}]`:``,a=n([`create`,`transform`,`extract`,`check`,`other`]),o=n([`${t}<T>${i}`,``,r.description,``]);return a.foldLeft(o)((t,i)=>e(r.methods[i]).filter(e=>e.length>0).fold(()=>t,e=>{let r=t.add(i.toUpperCase());return n(e).foldLeft(r)((e,t)=>e.add(` ${t}`)).add(``)})).toArray().join(`
|
|
4
4
|
`).trimEnd()},u=()=>{let e=n([`INTERFACES`,``]);return n(Object.entries(o)).foldLeft(e)((e,[t,r])=>{let i=r.extends?` extends ${r.extends}`:``,a=e.add(`${t}<A>${i}`).add(` ${r.description}`);return n(r.methods).foldLeft(a)((e,t)=>e.add(` ${t}`)).add(``)}).toArray().join(`
|
|
5
|
-
`).trimEnd()},d=e=>JSON.stringify(e,null,2),f=()=>({version:r,categories:a,types:i}),p=e
|
|
5
|
+
`).trimEnd()},d=e=>JSON.stringify(e,null,2),f=()=>({version:r,categories:a,types:i}),p=t=>e(i[t]).map(e=>({name:t,data:e})).or(n(Object.entries(i)).find(([e])=>e.toLowerCase()===t.toLowerCase()).map(([e,t])=>({name:e,data:t}))).orUndefined(),m=()=>Object.keys(i),h=()=>o,g=e=>{let t=n(e.slice(2));return{flags:{json:t.contains(`--json`),full:t.contains(`--full`),help:t.exists(e=>e===`--help`||e===`-h`)},args:t.filter(e=>!e.startsWith(`--`)&&e!==`-h`)}},_=t=>e(s[t]).or(n(Object.entries(s)).find(([e])=>e.toLowerCase()===t.toLowerCase()).map(([,e])=>e)),v=()=>{let e=n([`FULL INTERFACE DEFINITIONS`,`=`.repeat(60),``]);return n(Object.entries(s)).foldLeft(e)((e,[t,r])=>e.concat(n([`// ${t}`,r,``,`-`.repeat(60),``]))).toArray().join(`
|
|
6
6
|
`).trimEnd()},y=()=>{console.log(`functype - API documentation for LLMs
|
|
7
7
|
|
|
8
8
|
USAGE
|
|
@@ -24,4 +24,4 @@ EXAMPLES
|
|
|
24
24
|
npx functype Option --json # Option as JSON
|
|
25
25
|
npx functype Option --full # Full TypeScript interface
|
|
26
26
|
npx functype --full # All full interfaces (large output!)
|
|
27
|
-
`)},b=e=>{console.error(`Unknown type: ${e}`),console.error(``),console.error(`Available types: ${m().join(`, `)}`),console.error(``),console.error(`Use: npx functype interfaces - for interface reference`),process.exit(1)},x=e=>console.log(e),S=(
|
|
27
|
+
`)},b=e=>{console.error(`Unknown type: ${e}`),console.error(``),console.error(`Available types: ${m().join(`, `)}`),console.error(``),console.error(`Use: npx functype interfaces - for interface reference`),process.exit(1)},x=e=>console.log(e),S=(t,n)=>e(p(t)).fold(()=>b(t),e=>{n.full?_(e.name).fold(()=>x(n.json?d({[e.name]:e.data}):l(e.name,e.data)),t=>x(n.json?d({[e.name]:{...e.data,fullInterface:t}}):t)):x(n.json?d({[e.name]:e.data}):l(e.name,e.data))});(()=>{let{flags:e,args:n}=g(process.argv);t(!0).when(()=>e.help,()=>y()).when(()=>n.isEmpty,()=>e.full?x(e.json?d(s):v()):x(e.json?d(f()):c())).when(()=>n.headOption.contains(`interfaces`),()=>x(e.json?d(h()):u())).default(()=>n.headOption.fold(()=>x(e.json?d(f()):c()),t=>S(t,e)))})();
|
package/dist/do/index.d.ts
CHANGED
|
@@ -1,184 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { Option } from "../option/index.js";
|
|
4
|
-
import { List } from "../list/index.js";
|
|
5
|
-
import { Either } from "../either/index.js";
|
|
6
|
-
|
|
7
|
-
//#region src/do/index.d.ts
|
|
8
|
-
type OptionLike = {
|
|
9
|
-
_tag: "Some" | "None";
|
|
10
|
-
isSome(): boolean;
|
|
11
|
-
get(): unknown;
|
|
12
|
-
};
|
|
13
|
-
type EitherLike = {
|
|
14
|
-
_tag: "Left" | "Right";
|
|
15
|
-
isLeft(): boolean;
|
|
16
|
-
isRight(): boolean;
|
|
17
|
-
value: unknown;
|
|
18
|
-
};
|
|
19
|
-
type ListLike = {
|
|
20
|
-
_tag: "List";
|
|
21
|
-
toArray(): unknown[];
|
|
22
|
-
};
|
|
23
|
-
type TryLike = {
|
|
24
|
-
_tag: "Success" | "Failure";
|
|
25
|
-
isSuccess(): boolean;
|
|
26
|
-
get(): unknown;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Executes a generator-based monadic comprehension
|
|
30
|
-
* Returns the same monad type as the first yielded monad (Scala semantics)
|
|
31
|
-
*
|
|
32
|
-
* - Option comprehensions return Option (None on short-circuit)
|
|
33
|
-
* - Either comprehensions return Either (Left with error on short-circuit)
|
|
34
|
-
* - List comprehensions return List (empty or cartesian product)
|
|
35
|
-
* - Try comprehensions return Try (Failure with error on short-circuit)
|
|
36
|
-
*
|
|
37
|
-
* Type Inference Notes:
|
|
38
|
-
* - TypeScript infers the correct return type for homogeneous comprehensions
|
|
39
|
-
* - For mixed monad types, TypeScript returns a union type
|
|
40
|
-
* - Use DoTyped<T> or type assertions for mixed scenarios
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* // Option comprehension returns Option:
|
|
45
|
-
* const result = Do(function* () {
|
|
46
|
-
* const x = yield* $(Option(5));
|
|
47
|
-
* const y = yield* $(Option(10));
|
|
48
|
-
* return x + y;
|
|
49
|
-
* });
|
|
50
|
-
* // result: Option(15)
|
|
51
|
-
*
|
|
52
|
-
* // Either comprehension returns Either:
|
|
53
|
-
* const result = Do(function* () {
|
|
54
|
-
* const x = yield* $(Right(5));
|
|
55
|
-
* const y = yield* $(Left("error"));
|
|
56
|
-
* return x + y;
|
|
57
|
-
* });
|
|
58
|
-
* // result: Left("error") - error is preserved
|
|
59
|
-
*
|
|
60
|
-
* // List comprehension returns List with cartesian product:
|
|
61
|
-
* const result = Do(function* () {
|
|
62
|
-
* const x = yield* $(List([1, 2]));
|
|
63
|
-
* const y = yield* $(List([3, 4]));
|
|
64
|
-
* return x + y;
|
|
65
|
-
* });
|
|
66
|
-
* // result: List([4, 5, 5, 6])
|
|
67
|
-
*
|
|
68
|
-
* // Mixed types - use type assertion or DoTyped:
|
|
69
|
-
* const result = Do(function* () {
|
|
70
|
-
* const x = yield* $(Option(5));
|
|
71
|
-
* const y = yield* $(Right<string, number>(10));
|
|
72
|
-
* return x + y;
|
|
73
|
-
* }) as Option<number>;
|
|
74
|
-
* // result: Option(15)
|
|
75
|
-
* ```
|
|
76
|
-
*
|
|
77
|
-
* @param gen - Generator function that yields monads and returns a result
|
|
78
|
-
* @returns The same monad type as the first yield
|
|
79
|
-
*/
|
|
80
|
-
declare function Do<T>(gen: () => Generator<OptionLike, T, unknown>): Option<T>;
|
|
81
|
-
declare function Do<L, R>(gen: () => Generator<EitherLike, R, unknown>): Either<L, R>;
|
|
82
|
-
declare function Do<T>(gen: () => Generator<ListLike, T, unknown>): List<T>;
|
|
83
|
-
declare function Do<T>(gen: () => Generator<TryLike, T, unknown>): Try<T>;
|
|
84
|
-
declare function Do<T>(gen: () => Generator<OptionLike | EitherLike | ListLike | TryLike, T, unknown>): Reshapeable<T>;
|
|
85
|
-
declare function Do<T>(gen: () => Generator<unknown, T, unknown>): unknown;
|
|
86
|
-
/**
|
|
87
|
-
* Executes an async generator-based monadic comprehension
|
|
88
|
-
* Returns the same monad type as the first yielded monad
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* const result = await DoAsync(async function* () {
|
|
93
|
-
* const user = yield* $(await fetchUser(id)); // Promise<Option<User>> → User
|
|
94
|
-
* const profile = yield* $(await getProfile(user)); // Promise<Either<Error, Profile>> → Profile
|
|
95
|
-
* return { user, profile };
|
|
96
|
-
* });
|
|
97
|
-
* // result type matches first yield
|
|
98
|
-
* ```
|
|
99
|
-
*
|
|
100
|
-
* @param gen - Async generator function that yields monads/promises and returns a result
|
|
101
|
-
* @returns Promise of the same monad type as first yield
|
|
102
|
-
*/
|
|
103
|
-
declare function DoAsync<T>(gen: () => AsyncGenerator<OptionLike, T, unknown>): Promise<Option<T>>;
|
|
104
|
-
declare function DoAsync<L, R>(gen: () => AsyncGenerator<EitherLike, R, unknown>): Promise<Either<L, R>>;
|
|
105
|
-
declare function DoAsync<T>(gen: () => AsyncGenerator<ListLike, T, unknown>): Promise<List<T>>;
|
|
106
|
-
declare function DoAsync<T>(gen: () => AsyncGenerator<TryLike, T, unknown>): Promise<Try<T>>;
|
|
107
|
-
declare function DoAsync<T>(gen: () => AsyncGenerator<OptionLike | EitherLike | ListLike | TryLike, T, unknown>): Promise<Reshapeable<T>>;
|
|
108
|
-
declare function DoAsync<T>(gen: () => AsyncGenerator<unknown, T, unknown>): Promise<unknown>;
|
|
109
|
-
/**
|
|
110
|
-
* Helper function to check if a value implements the Doable interface
|
|
111
|
-
* @param value - Value to check
|
|
112
|
-
* @returns True if the value implements Doable
|
|
113
|
-
*/
|
|
114
|
-
declare function isDoCapable<T>(value: unknown): value is Doable<T>;
|
|
115
|
-
/**
|
|
116
|
-
* Manually unwrap a monad using the Doable interface
|
|
117
|
-
* Useful for testing or when you need to unwrap outside of a Do-comprehension
|
|
118
|
-
*
|
|
119
|
-
* @param monad - Monad to unwrap
|
|
120
|
-
* @returns The unwrapped value
|
|
121
|
-
* @throws Error if the monad cannot be unwrapped
|
|
122
|
-
*/
|
|
123
|
-
declare function unwrap<T>(monad: Doable<T>): T;
|
|
124
|
-
/**
|
|
125
|
-
* Type helper for Do-notation generators.
|
|
126
|
-
* Provides better type hints in IDEs.
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* ```typescript
|
|
130
|
-
* const result = Do(function* (): DoGenerator<number> {
|
|
131
|
-
* const x = yield* $(List([1, 2])) // x is still unknown but return type is clear
|
|
132
|
-
* const y = yield* $(List([3, 4]))
|
|
133
|
-
* return x + y
|
|
134
|
-
* })
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
type DoGenerator<T, TYield = unknown> = Generator<TYield, T, unknown>;
|
|
138
|
-
/**
|
|
139
|
-
* Extracts values from monads in Do-notation with type inference.
|
|
140
|
-
* The '$' symbol is the universal extraction operator in functional programming.
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```typescript
|
|
144
|
-
* const result = Do(function* () {
|
|
145
|
-
* const x = yield* $(Option(5)) // x: number
|
|
146
|
-
* const y = yield* $(List([1, 2, 3])) // y: number (for cartesian product)
|
|
147
|
-
* const name = yield* $(Right("Alice")) // name: string
|
|
148
|
-
* return `${name}: ${x + y}`
|
|
149
|
-
* })
|
|
150
|
-
* ```
|
|
151
|
-
*
|
|
152
|
-
* @param monad - Any monad that can be unwrapped (Option, Either, List, Try, etc.)
|
|
153
|
-
* @returns A generator that yields the monad and returns its extracted value
|
|
154
|
-
*/
|
|
155
|
-
declare function $<T>(monad: Option<T>): Generator<Option<T>, T, T>;
|
|
156
|
-
declare function $<L, R>(monad: Either<L, R>): Generator<Either<L, R>, R, R>;
|
|
157
|
-
declare function $<T>(monad: List<T>): Generator<List<T>, T, T>;
|
|
158
|
-
declare function $<T>(monad: Try<T>): Generator<Try<T>, T, T>;
|
|
159
|
-
declare function $<T>(monad: Doable<T>): Generator<Doable<T>, T, T>;
|
|
160
|
-
declare function $<M>(monad: M): Generator<M, InferYieldType<M>, InferYieldType<M>>;
|
|
161
|
-
type InferYieldType<M> = M extends {
|
|
162
|
-
isSome(): boolean;
|
|
163
|
-
get(): infer T;
|
|
164
|
-
} ? T : M extends {
|
|
165
|
-
isRight(): boolean;
|
|
166
|
-
value: infer R;
|
|
167
|
-
} ? R : M extends {
|
|
168
|
-
toArray(): (infer T)[];
|
|
169
|
-
} ? T : M extends {
|
|
170
|
-
isSuccess(): boolean;
|
|
171
|
-
get(): infer T;
|
|
172
|
-
} ? T : M extends Doable<infer T> ? T : unknown;
|
|
173
|
-
declare const NoneError: (message?: string) => Error;
|
|
174
|
-
interface LeftErrorType<L> extends Error {
|
|
175
|
-
value: L;
|
|
176
|
-
}
|
|
177
|
-
declare const LeftError: <L>(value: L, message?: string) => LeftErrorType<L>;
|
|
178
|
-
declare const EmptyListError: (message?: string) => Error;
|
|
179
|
-
interface FailureErrorType extends Error {
|
|
180
|
-
cause: Error;
|
|
181
|
-
}
|
|
182
|
-
declare const FailureError: (cause: Error, message?: string) => FailureErrorType;
|
|
183
|
-
//#endregion
|
|
184
|
-
export { $, Do, DoAsync, DoGenerator, type DoResult, type Doable, EmptyListError, FailureError, FailureErrorType, LeftError, LeftErrorType, NoneError, isDoCapable, unwrap };
|
|
1
|
+
import { Gn as Doable, Wn as DoResult, a as EmptyListError, c as LeftError, d as isDoCapable, f as unwrap, i as DoGenerator, l as LeftErrorType, n as Do, o as FailureError, r as DoAsync, s as FailureErrorType, t as $, u as NoneError } from "../index-OdnROQtM.js";
|
|
2
|
+
export { $, Do, DoAsync, DoGenerator, DoResult, Doable, EmptyListError, FailureError, FailureErrorType, LeftError, LeftErrorType, NoneError, isDoCapable, unwrap };
|
package/dist/do/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{A as e,D as t,F as n,M as r,N as i,O as a,P as o,j as s,k as c}from"../src-ByDeJzWj.js";export{t as $,a as Do,c as DoAsync,e as EmptyListError,s as FailureError,r as LeftError,i as NoneError,o as isDoCapable,n as unwrap};
|
package/dist/either/index.d.ts
CHANGED
|
@@ -1,188 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { l as Type } from "../Typeable-lfO2nRVW.js";
|
|
3
|
-
import { Option } from "../option/index.js";
|
|
4
|
-
import { List } from "../list/index.js";
|
|
5
|
-
|
|
6
|
-
//#region src/either/Either.d.ts
|
|
7
|
-
/**
|
|
8
|
-
* Either type module
|
|
9
|
-
* @module Either
|
|
10
|
-
* @category Core
|
|
11
|
-
*/
|
|
12
|
-
interface Either<L extends Type, R extends Type> extends FunctypeBase<R, "Left" | "Right">, Promisable<R>, Doable<R>, Reshapeable<R>, Extractable<R> {
|
|
13
|
-
readonly _tag: "Left" | "Right";
|
|
14
|
-
value: L | R;
|
|
15
|
-
isLeft(): this is Either<L, R> & {
|
|
16
|
-
readonly _tag: "Left";
|
|
17
|
-
value: L;
|
|
18
|
-
};
|
|
19
|
-
isRight(): this is Either<L, R> & {
|
|
20
|
-
readonly _tag: "Right";
|
|
21
|
-
value: R;
|
|
22
|
-
};
|
|
23
|
-
orElse: (defaultValue: R) => R;
|
|
24
|
-
orThrow: (error?: Error) => R;
|
|
25
|
-
or(alternative: Either<L, R>): Either<L, R>;
|
|
26
|
-
orNull: () => R | null;
|
|
27
|
-
orUndefined: () => R | undefined;
|
|
28
|
-
readonly map: <U extends Type>(f: (value: R) => U) => Either<L, U>;
|
|
29
|
-
ap: <U extends Type>(ff: Either<L, (value: R) => U>) => Either<L, U>;
|
|
30
|
-
merge: <L1 extends Type, R1 extends Type>(other: Either<L1, R1>) => Either<L | L1, [R, R1]>;
|
|
31
|
-
mapAsync: <U extends Type>(f: (value: R) => Promise<U>) => Promise<Either<L, U>>;
|
|
32
|
-
flatMap: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U>;
|
|
33
|
-
flatMapAsync: <U extends Type>(f: (value: R) => Promise<Either<L, U>>) => Promise<Either<L, U>>;
|
|
34
|
-
toOption: () => Option<R>;
|
|
35
|
-
toList: () => List<R>;
|
|
36
|
-
toString: () => string;
|
|
37
|
-
[Symbol.iterator]: () => Iterator<R>;
|
|
38
|
-
yield: () => Generator<R, void, unknown>;
|
|
39
|
-
traverse: <U extends Type>(f: (value: R) => Either<L, U>) => Either<L, U[]>;
|
|
40
|
-
lazyMap: <U extends Type>(f: (value: R) => U) => Generator<Either<L, U>, void, unknown>;
|
|
41
|
-
tap: (f: (value: R) => void) => Either<L, R>;
|
|
42
|
-
tapLeft: (f: (value: L) => void) => Either<L, R>;
|
|
43
|
-
mapLeft: <L2 extends Type>(f: (value: L) => L2) => Either<L2, R>;
|
|
44
|
-
bimap: <L2 extends Type, R2 extends Type>(fl: (value: L) => L2, fr: (value: R) => R2) => Either<L2, R2>;
|
|
45
|
-
fold: <T extends Type>(onLeft: (value: L) => T, onRight: (value: R) => T) => T;
|
|
46
|
-
swap: () => Either<R, L>;
|
|
47
|
-
/**
|
|
48
|
-
* Pipes the value through the provided function based on whether this is a Left or Right
|
|
49
|
-
* @param onLeft - The function to apply if this is a Left
|
|
50
|
-
* @param onRight - The function to apply if this is a Right
|
|
51
|
-
* @returns The result of applying the appropriate function
|
|
52
|
-
*/
|
|
53
|
-
pipeEither<U extends Type>(onLeft: (value: L) => U, onRight: (value: R) => U): U;
|
|
54
|
-
/**
|
|
55
|
-
* Pipes the Either value through the provided function
|
|
56
|
-
* @param f - The function to apply to the value (Left or Right)
|
|
57
|
-
* @returns The result of applying the function to the value
|
|
58
|
-
*/
|
|
59
|
-
pipe<U extends Type>(f: (value: L | R) => U): U;
|
|
60
|
-
/**
|
|
61
|
-
* Pattern matches over the Either, applying a handler function based on the variant
|
|
62
|
-
* @param patterns - Object with handler functions for Left and Right variants
|
|
63
|
-
* @returns The result of applying the matching handler function
|
|
64
|
-
*/
|
|
65
|
-
match<T>(patterns: {
|
|
66
|
-
Left: (value: L) => T;
|
|
67
|
-
Right: (value: R) => T;
|
|
68
|
-
}): T;
|
|
69
|
-
/**
|
|
70
|
-
* Returns the value and tag for inspection
|
|
71
|
-
*/
|
|
72
|
-
toValue(): {
|
|
73
|
-
_tag: "Left" | "Right";
|
|
74
|
-
value: L | R;
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Custom JSON serialization that excludes getter properties
|
|
78
|
-
*/
|
|
79
|
-
toJSON(): {
|
|
80
|
-
_tag: "Left" | "Right";
|
|
81
|
-
value: L | R;
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
type TestEither<L extends Type, R extends Type> = Either<L, R> & AsyncMonad<R>;
|
|
85
|
-
declare const Right: <L extends Type, R extends Type>(value: R) => Either<L, R>;
|
|
86
|
-
declare const Left: <L extends Type, R extends Type>(value: L) => Either<L, R>;
|
|
87
|
-
declare const isRight: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
|
|
88
|
-
value: R;
|
|
89
|
-
};
|
|
90
|
-
declare const isLeft: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
|
|
91
|
-
value: L;
|
|
92
|
-
};
|
|
93
|
-
declare const tryCatch: <L extends Type, R extends Type>(f: () => R, onError: (error: unknown) => L) => Either<L, R>;
|
|
94
|
-
declare const TypeCheckRight: <L extends Type, R extends Type>(value: R) => TestEither<L, R>;
|
|
95
|
-
declare const TypeCheckLeft: <L extends Type, R extends Type>(value: L) => TestEither<L, R>;
|
|
96
|
-
declare const tryCatchAsync: <L extends Type, R extends Type>(f: () => Promise<R>, onError: (error: unknown) => L) => Promise<Either<L, R>>;
|
|
97
|
-
declare const Either: (<L extends Type, R extends Type>(value: R | L, isRight: boolean) => Either<L, R>) & {
|
|
98
|
-
/**
|
|
99
|
-
* Creates a Left instance
|
|
100
|
-
* @param value - The left value
|
|
101
|
-
* @returns Left Either
|
|
102
|
-
*/
|
|
103
|
-
left: <L extends Type, R extends Type>(value: L) => Either<L, R>;
|
|
104
|
-
/**
|
|
105
|
-
* Creates a Right instance
|
|
106
|
-
* @param value - The right value
|
|
107
|
-
* @returns Right Either
|
|
108
|
-
*/
|
|
109
|
-
right: <L extends Type, R extends Type>(value: R) => Either<L, R>;
|
|
110
|
-
/**
|
|
111
|
-
* Type guard to check if an Either is Right
|
|
112
|
-
* @param either - The Either to check
|
|
113
|
-
* @returns True if Either is Right
|
|
114
|
-
*/
|
|
115
|
-
isRight: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
|
|
116
|
-
value: R;
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* Type guard to check if an Either is Left
|
|
120
|
-
* @param either - The Either to check
|
|
121
|
-
* @returns True if Either is Left
|
|
122
|
-
*/
|
|
123
|
-
isLeft: <L extends Type, R extends Type>(either: Either<L, R>) => either is Either<L, R> & {
|
|
124
|
-
value: L;
|
|
125
|
-
};
|
|
126
|
-
/**
|
|
127
|
-
* Combines an array of Eithers into a single Either containing an array
|
|
128
|
-
* @param eithers - Array of Either values
|
|
129
|
-
* @returns Either with array of values or first Left encountered
|
|
130
|
-
*/
|
|
131
|
-
sequence: <L extends Type, R extends Type>(eithers: Either<L, R>[]) => Either<L, R[]>;
|
|
132
|
-
/**
|
|
133
|
-
* Maps an array through a function that returns Either, then sequences the results
|
|
134
|
-
* @param arr - Array of values
|
|
135
|
-
* @param f - Function that returns Either
|
|
136
|
-
* @returns Either with array of results or first Left encountered
|
|
137
|
-
*/
|
|
138
|
-
traverse: <L extends Type, R extends Type, U extends Type>(arr: R[], f: (value: R) => Either<L, U>) => Either<L, U[]>;
|
|
139
|
-
/**
|
|
140
|
-
* Creates an Either from a nullable value
|
|
141
|
-
* @param value - The value that might be null or undefined
|
|
142
|
-
* @param leftValue - The value to use for Left if value is null/undefined
|
|
143
|
-
* @returns Right if value is not null/undefined, Left otherwise
|
|
144
|
-
*/
|
|
145
|
-
fromNullable: <L extends Type, R extends Type>(value: R | null | undefined, leftValue: L) => Either<L, R>;
|
|
146
|
-
/**
|
|
147
|
-
* Creates an Either based on a predicate
|
|
148
|
-
* @param value - The value to test
|
|
149
|
-
* @param predicate - The predicate function
|
|
150
|
-
* @param leftValue - The value to use for Left if predicate fails
|
|
151
|
-
* @returns Right if predicate passes, Left otherwise
|
|
152
|
-
*/
|
|
153
|
-
fromPredicate: <L extends Type, R extends Type>(value: R, predicate: (value: R) => boolean, leftValue: L) => Either<L, R>;
|
|
154
|
-
/**
|
|
155
|
-
* Applicative apply - applies a wrapped function to a wrapped value
|
|
156
|
-
* @param eitherF - Either containing a function
|
|
157
|
-
* @param eitherV - Either containing a value
|
|
158
|
-
* @returns Either with function applied to value
|
|
159
|
-
*/
|
|
160
|
-
ap: <L extends Type, R extends Type, U extends Type>(eitherF: Either<L, (value: R) => U>, eitherV: Either<L, R>) => Either<L, U>;
|
|
161
|
-
/**
|
|
162
|
-
* Creates an Either from a Promise
|
|
163
|
-
* @param promise - The Promise to convert
|
|
164
|
-
* @param onRejected - Function to convert rejection reason to Left value
|
|
165
|
-
* @returns Promise that resolves to Either
|
|
166
|
-
*/
|
|
167
|
-
fromPromise: <L, R>(promise: Promise<R>, onRejected: (reason: unknown) => L) => Promise<Either<L, R>>;
|
|
168
|
-
/**
|
|
169
|
-
* Creates an Either from JSON string
|
|
170
|
-
* @param json - The JSON string
|
|
171
|
-
* @returns Either instance
|
|
172
|
-
*/
|
|
173
|
-
fromJSON: <L extends Type, R extends Type>(json: string) => Either<L, R>;
|
|
174
|
-
/**
|
|
175
|
-
* Creates an Either from YAML string
|
|
176
|
-
* @param yaml - The YAML string
|
|
177
|
-
* @returns Either instance
|
|
178
|
-
*/
|
|
179
|
-
fromYAML: <L extends Type, R extends Type>(yaml: string) => Either<L, R>;
|
|
180
|
-
/**
|
|
181
|
-
* Creates an Either from binary string
|
|
182
|
-
* @param binary - The binary string
|
|
183
|
-
* @returns Either instance
|
|
184
|
-
*/
|
|
185
|
-
fromBinary: <L extends Type, R extends Type>(binary: string) => Either<L, R>;
|
|
186
|
-
};
|
|
187
|
-
//#endregion
|
|
1
|
+
import { S as tryCatchAsync, _ as TypeCheckLeft, b as isRight, g as TestEither, h as Right, m as Left, p as Either, v as TypeCheckRight, x as tryCatch, y as isLeft } from "../index-OdnROQtM.js";
|
|
188
2
|
export { Either, Left, Right, TestEither, TypeCheckLeft, TypeCheckRight, isLeft, isRight, tryCatch, tryCatchAsync };
|
package/dist/either/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import{
|
|
2
|
-
`),n=t[0]?.split(`: `)[1],r=t[1]?.split(`: `)[1];if(!n||!r)throw Error(`Invalid YAML format for Either`);let i=JSON.parse(r);return n===`Right`?l(i):u(i)},fromBinary:e=>{let t=Buffer.from(e,`base64`).toString();return v.fromJSON(t)}},y=t(_,v);export{y as Either,u as Left,l as Right,h as TypeCheckLeft,m as TypeCheckRight,f as isLeft,d as isRight,p as tryCatch,g as tryCatchAsync};
|
|
1
|
+
import{ct as e,dt as t,ft as n,lt as r,mt as i,ot as a,pt as o,st as s,ut as c}from"../src-ByDeJzWj.js";export{a as Either,s as Left,e as Right,r as TypeCheckLeft,c as TypeCheckRight,t as isLeft,n as isRight,o as tryCatch,i as tryCatchAsync};
|