@consolidados/results 0.2.1 → 0.3.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.
@@ -12,6 +12,20 @@ function match(matcher, cases, discriminant) {
12
12
  }
13
13
  throw new Error(`No case found for value: ${String(matcher)}`);
14
14
  }
15
+ if (typeof matcher === "object" && matcher !== null && !discriminant) {
16
+ for (const key in cases) {
17
+ if (key === "default") continue;
18
+ if (key in matcher) {
19
+ const handler = cases[key];
20
+ if (handler) {
21
+ return typeof handler === "function" ? handler(matcher[key]) : handler();
22
+ }
23
+ }
24
+ }
25
+ if (cases.default) {
26
+ return cases.default();
27
+ }
28
+ }
15
29
  if (discriminant && typeof matcher === "object" && matcher !== null) {
16
30
  const discriminantValue = matcher[discriminant];
17
31
  const handler = cases[discriminantValue];
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";;;AA4EO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,OAAA,GAAU,MAAM,OAAO,CAAA;AAE7B,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,OAAQ,EAAA;AAAA;AAGjB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAA,OAAO,MAAM,OAAQ,EAAA;AAAA;AAGvB,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,yBAAA,EAA4B,MAAO,CAAA,OAAO,CAAC,CAAE,CAAA,CAAA;AAAA;AAI/D,EAAA,IAAI,YAAgB,IAAA,OAAO,OAAY,KAAA,QAAA,IAAY,YAAY,IAAM,EAAA;AACnE,IAAM,MAAA,iBAAA,GAAoB,QAAQ,YAAY,CAAA;AAC9C,IAAM,MAAA,OAAA,GAAU,MAAM,iBAAiB,CAAA;AAEvC,IAAA,IAAI,OAA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AAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGpB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AACnD;AAGC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.cjs","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n"]}
1
+ {"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";;;AAkHO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,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result } from \"../result\";\n\n// Utility types for mixed primitive + object unions\ntype PrimitiveMembers<T> = Extract<T, PropertyKey>;\ntype ObjectKeys<T> = T extends object ? keyof T : never;\ntype ObjectPropertyType<T, K extends PropertyKey> = T extends object\n ? K extends keyof T\n ? T[K]\n : never\n : never;\n\n// Helper to determine if a key K is a primitive member or object key\ntype HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T>\n ? () => R\n : K extends ObjectKeys<T>\n ? (value: ObjectPropertyType<T, K>) => R\n : never;\n\n// Build cases type with a single mapped type\ntype MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true\n ? Partial<{\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }>\n : {\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }) &\n (HasDefault extends true ? { default: () => R } : {});\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for mixed primitive + object unions WITH default (cases optional)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, true>,\n): R;\n\n// Overload for mixed primitive + object unions WITHOUT default (exhaustive)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, false>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // NEW: Handle objects with specific property keys (like { Other: [...] })\n // This must come BEFORE discriminated union check to handle mixed unions\n if (typeof matcher === \"object\" && matcher !== null && !discriminant) {\n // Check if any case key matches a property in the matcher object\n for (const key in cases) {\n if (key === \"default\") continue;\n\n if (key in matcher) {\n const handler = cases[key];\n if (handler) {\n return typeof handler === \"function\"\n ? handler(matcher[key])\n : handler();\n }\n }\n }\n\n // If no match found and there's a default, use it\n if (cases.default) {\n return cases.default();\n }\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n"]}
@@ -1,5 +1,16 @@
1
1
  import { R as Result, O as Option } from '../option-B_KKIecf.cjs';
2
2
 
3
+ type PrimitiveMembers<T> = Extract<T, PropertyKey>;
4
+ type ObjectKeys<T> = T extends object ? keyof T : never;
5
+ type ObjectPropertyType<T, K extends PropertyKey> = T extends object ? K extends keyof T ? T[K] : never : never;
6
+ type HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T> ? () => R : K extends ObjectKeys<T> ? (value: ObjectPropertyType<T, K>) => R : never;
7
+ type MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true ? Partial<{
8
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
9
+ }> : {
10
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
11
+ }) & (HasDefault extends true ? {
12
+ default: () => R;
13
+ } : {});
3
14
  declare function match<T, E, R>(matcher: Result<T, E>, cases: {
4
15
  Ok: (value: T) => R;
5
16
  Err: (error: E) => R;
@@ -8,10 +19,8 @@ declare function match<T, R>(matcher: Option<T>, cases: {
8
19
  Some: (value: T) => R;
9
20
  None: () => R;
10
21
  }): R;
11
- declare function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
12
- default: () => R;
13
- }): R;
14
- declare function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
22
+ declare function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, true>): R;
23
+ declare function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, false>): R;
15
24
  declare function match<T extends {
16
25
  [K in D]: string | number | symbol;
17
26
  }, D extends keyof T, R>(matcher: T, cases: {
@@ -28,6 +37,10 @@ declare function match<T extends {
28
37
  [P in D]: K;
29
38
  }>) => R;
30
39
  }, discriminant: D): R;
40
+ declare function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
41
+ default: () => R;
42
+ }): R;
43
+ declare function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
31
44
  declare function match<T, E>(matcher: Result<T, E>, cases: {
32
45
  Ok: (value: T) => Option<T>;
33
46
  Err: (error: E) => Option<T>;
@@ -1,5 +1,16 @@
1
1
  import { R as Result, O as Option } from '../option-B_KKIecf.js';
2
2
 
3
+ type PrimitiveMembers<T> = Extract<T, PropertyKey>;
4
+ type ObjectKeys<T> = T extends object ? keyof T : never;
5
+ type ObjectPropertyType<T, K extends PropertyKey> = T extends object ? K extends keyof T ? T[K] : never : never;
6
+ type HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T> ? () => R : K extends ObjectKeys<T> ? (value: ObjectPropertyType<T, K>) => R : never;
7
+ type MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true ? Partial<{
8
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
9
+ }> : {
10
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
11
+ }) & (HasDefault extends true ? {
12
+ default: () => R;
13
+ } : {});
3
14
  declare function match<T, E, R>(matcher: Result<T, E>, cases: {
4
15
  Ok: (value: T) => R;
5
16
  Err: (error: E) => R;
@@ -8,10 +19,8 @@ declare function match<T, R>(matcher: Option<T>, cases: {
8
19
  Some: (value: T) => R;
9
20
  None: () => R;
10
21
  }): R;
11
- declare function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
12
- default: () => R;
13
- }): R;
14
- declare function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
22
+ declare function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, true>): R;
23
+ declare function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, false>): R;
15
24
  declare function match<T extends {
16
25
  [K in D]: string | number | symbol;
17
26
  }, D extends keyof T, R>(matcher: T, cases: {
@@ -28,6 +37,10 @@ declare function match<T extends {
28
37
  [P in D]: K;
29
38
  }>) => R;
30
39
  }, discriminant: D): R;
40
+ declare function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
41
+ default: () => R;
42
+ }): R;
43
+ declare function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
31
44
  declare function match<T, E>(matcher: Result<T, E>, cases: {
32
45
  Ok: (value: T) => Option<T>;
33
46
  Err: (error: E) => Option<T>;
@@ -10,6 +10,20 @@ function match(matcher, cases, discriminant) {
10
10
  }
11
11
  throw new Error(`No case found for value: ${String(matcher)}`);
12
12
  }
13
+ if (typeof matcher === "object" && matcher !== null && !discriminant) {
14
+ for (const key in cases) {
15
+ if (key === "default") continue;
16
+ if (key in matcher) {
17
+ const handler = cases[key];
18
+ if (handler) {
19
+ return typeof handler === "function" ? handler(matcher[key]) : handler();
20
+ }
21
+ }
22
+ }
23
+ if (cases.default) {
24
+ return cases.default();
25
+ }
26
+ }
13
27
  if (discriminant && typeof matcher === "object" && matcher !== null) {
14
28
  const discriminantValue = matcher[discriminant];
15
29
  const handler = cases[discriminantValue];
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";AA4EO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,OAAA,GAAU,MAAM,OAAO,CAAA;AAE7B,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,OAAQ,EAAA;AAAA;AAGjB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAA,OAAO,MAAM,OAAQ,EAAA;AAAA;AAGvB,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,yBAAA,EAA4B,MAAO,CAAA,OAAO,CAAC,CAAE,CAAA,CAAA;AAAA;AAI/D,EAAA,IAAI,YAAgB,IAAA,OAAO,OAAY,KAAA,QAAA,IAAY,YAAY,IAAM,EAAA;AACnE,IAAM,MAAA,iBAAA,GAAoB,QAAQ,YAAY,CAAA;AAC9C,IAAM,MAAA,OAAA,GAAU,MAAM,iBAAiB,CAAA;AAEvC,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,QAAQ,OAAO,CAAA;AAAA;AAGxB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAO,OAAA,KAAA,CAAM,QAAQ,OAAO,CAAA;AAAA;AAG9B,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,sCAAA,EAAyC,MAAO,CAAA,iBAAiB,CAAC,CAAA;AAAA,KACpE;AAAA;AAIF,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAA,KAAY,QACZ,MAAU,IAAA,OAAA,IACV,OAAQ,CAAA,IAAA,EACR,EAAA;AACA,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAIlC,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAA,KAAY,QACZ,OAAW,IAAA,OAAA,IACX,OAAQ,CAAA,KAAA,EACR,EAAA;AACA,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAI3C,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAA,KAAY,QACZ,QAAY,IAAA,OAAA,IACZ,OAAQ,CAAA,MAAA,EACR,EAAA;AACA,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAIpC,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAA,KAAY,QACZ,QAAY,IAAA,OAAA,IACZ,OAAQ,CAAA,MAAA,EACR,EAAA;AACA,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGpB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AACnD;AAGC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n"]}
1
+ {"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";AAkHO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,OAAA,GAAU,MAAM,OAAO,CAAA;AAE7B,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,OAAQ,EAAA;AAAA;AAGjB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAA,OAAO,MAAM,OAAQ,EAAA;AAAA;AAGvB,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,yBAAA,EAA4B,MAAO,CAAA,OAAO,CAAC,CAAE,CAAA,CAAA;AAAA;AAK/D,EAAA,IAAI,OAAO,OAAY,KAAA,QAAA,IAAY,OAAY,KAAA,IAAA,IAAQ,CAAC,YAAc,EAAA;AAEpE,IAAA,KAAA,MAAW,OAAO,KAAO,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A,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAI3C,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAA,KAAY,QACZ,QAAY,IAAA,OAAA,IACZ,OAAQ,CAAA,MAAA,EACR,EAAA;AACA,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAIpC,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAA,KAAY,QACZ,QAAY,IAAA,OAAA,IACZ,OAAQ,CAAA,MAAA,EACR,EAAA;AACA,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGpB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AACnD;AAGC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Utility types for mixed primitive + object unions\ntype PrimitiveMembers<T> = Extract<T, PropertyKey>;\ntype ObjectKeys<T> = T extends object ? keyof T : never;\ntype ObjectPropertyType<T, K extends PropertyKey> = T extends object\n ? K extends keyof T\n ? T[K]\n : never\n : never;\n\n// Helper to determine if a key K is a primitive member or object key\ntype HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T>\n ? () => R\n : K extends ObjectKeys<T>\n ? (value: ObjectPropertyType<T, K>) => R\n : never;\n\n// Build cases type with a single mapped type\ntype MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true\n ? Partial<{\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }>\n : {\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }) &\n (HasDefault extends true ? { default: () => R } : {});\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for mixed primitive + object unions WITH default (cases optional)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, true>,\n): R;\n\n// Overload for mixed primitive + object unions WITHOUT default (exhaustive)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, false>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // NEW: Handle objects with specific property keys (like { Other: [...] })\n // This must come BEFORE discriminated union check to handle mixed unions\n if (typeof matcher === \"object\" && matcher !== null && !discriminant) {\n // Check if any case key matches a property in the matcher object\n for (const key in cases) {\n if (key === \"default\") continue;\n\n if (key in matcher) {\n const handler = cases[key];\n if (handler) {\n return typeof handler === \"function\"\n ? handler(matcher[key])\n : handler();\n }\n }\n }\n\n // If no match found and there's a default, use it\n if (cases.default) {\n return cases.default();\n }\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n"]}
package/dist/index.cjs CHANGED
@@ -12,6 +12,20 @@ function match(matcher, cases, discriminant) {
12
12
  }
13
13
  throw new Error(`No case found for value: ${String(matcher)}`);
14
14
  }
15
+ if (typeof matcher === "object" && matcher !== null && !discriminant) {
16
+ for (const key in cases) {
17
+ if (key === "default") continue;
18
+ if (key in matcher) {
19
+ const handler = cases[key];
20
+ if (handler) {
21
+ return typeof handler === "function" ? handler(matcher[key]) : handler();
22
+ }
23
+ }
24
+ }
25
+ if (cases.default) {
26
+ return cases.default();
27
+ }
28
+ }
15
29
  if (discriminant && typeof matcher === "object" && matcher !== null) {
16
30
  const discriminantValue = matcher[discriminant];
17
31
  const handler = cases[discriminantValue];
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/helpers/match.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts"],"names":["Ok","Err","Some","None"],"mappings":";;;AA4EO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,OAAA,GAAU,MAAM,OAAO,CAAA;AAE7B,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,OAAQ,EAAA;AAAA;AAGjB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAA,OAAO,MAAM,OAAQ,EAAA;AAAA;AAGvB,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,yBAAA,EAA4B,MAAO,CAAA,OAAO,CAAC,CAAE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A;AACd,IAAM,MAAA,IAAI,MAAM,+BAA+B,CAAA;AAAA;AACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,KAAmB,GAAA;AACjB,IAAO,OAAA,MAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,GAAqC,EAAA;AAC1C,IAAA,OAAOA,KAAY,EAAA;AAAA;AACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QAAW,GAA6C,EAAA;AACtD,IAAA,OAAOA,KAAY,EAAA;AAAA;AACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAY,YAAoB,EAAA;AAC9B,IAAO,OAAA,YAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,aAAgB,EAAgB,EAAA;AAC9B,IAAA,OAAO,EAAG,EAAA;AAAA;AACZ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,KAAQ,KAAU,EAAA;AAChB,IAAA,OAAOF,KAAI,KAAK,CAAA;AAAA;AAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAU,UAAkD,EAAA;AAC1D,IAAA,OAAOE,KAAY,EAAA;AAAA;AAEvB,CAAA;;;ACrFA,SAASD,MAAQ,KAAuB,EAAA;AACvC,EAAO,OAAA,IAAIA,MAAS,KAAK,CAAA;AAC1B;AAEA,IAAI,aAAiC,GAAA,IAAA;AAWrC,SAASC,KAAiB,GAAA;AACzB,EAAA,IAAI,CAAC,aAAe,EAAA;AACnB,IAAA,aAAA,GAAgB,IAAIA,KAAS,EAAA;AAAA;AAE9B,EAAO,OAAA,aAAA;AACR;AAEC,MAAA,CAAe,IAAOD,GAAAA,KAAAA;AACtB,MAAA,CAAe,IAAOC,GAAAA,KAAAA","file":"index.cjs","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).\n */\nexport class Err<E> implements ResultDefinition<never, E> {\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance. Can be any type.\n */\n constructor(error: E) {\n // Store error as-is - conversion is handled by factory function\n this.error = error;\n // Object.setPrototypeOf(this, Err.prototype);\n //\n // if (Error.captureStackTrace) {\n // Error.captureStackTrace(this, Err);\n // }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error. Can be any type.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @template F The type of the error in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance cast to the new error type.\n */\n flatMap<U, F = E>(\n _fn: (value: never) => ResultDefinition<U, F>,\n ): ResultDefinition<U, E> {\n return this as unknown as ResultDefinition<U, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The error value contained in this `Err`.\n */\n value(): E {\n return this.error;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return since this is an `Err`.\n * @returns The provided default value.\n */\n unwrapOr<U>(defaultValue: U): U {\n return defaultValue;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function with the error.\n */\n unwrapOrElse<U>(fn: (error: E) => U): U {\n return fn(this.error);\n }\n\n /**\n * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.\n * @template T The type of the alternative success value.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The result of calling the provided function with the error.\n */\n orElse<T, F>(\n fn: (error: E) => ResultDefinition<T, F>,\n ): ResultDefinition<T, F> {\n return fn(this.error);\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param _value The value to wrap in the `Ok` instance.\n */\n constructor(private _value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The value contained in this `Ok`.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this._value)) as unknown as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @template E The type of the error in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U, E = never>(\n fn: (value: T) => ResultDefinition<U, E>,\n ): ResultDefinition<U, E> {\n return fn(this._value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`). Can be any type.\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return if this is an `Err`.\n * @returns The value contained in this `Ok`.\n */\n unwrapOr<U>(defaultValue: U): T {\n return this._value;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The value contained in this `Ok`.\n */\n unwrapOrElse<U>(fn: (error: never) => U): T {\n return this._value;\n }\n\n /**\n * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The original `Ok` instance.\n */\n orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F> {\n return this as unknown as ResultDefinition<T, F>;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return Some(this._value);\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates an Err - strings are converted to Error, everything else preserved\n */\nfunction Err<E>(error: E): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n","import type { None as NoneType } from \"./none\";\nimport type { Option } from \"./option\";\nimport { None } from \"../../option\";\nimport { Ok } from \"@/result\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param _value The value to be wrapped in the `Some` option.\n */\n constructor(private _value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is NoneType {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * @returns The value held by this `Some` option.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this._value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this._value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this._value;\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the function provided.\n * @template U The type of the default value.\n * @param _fn A function to compute the default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOrElse<U>(_fn: () => U): T {\n return this._value;\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value if this is `None`.\n * @template E The type of the error.\n * @param _error The error value to use if this is `None` (ignored since this is `Some`).\n * @returns An `Ok` result containing the value from this `Some`.\n */\n okOr<E>(_error: E) {\n return Ok(this._value);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param predicate The function to test the value.\n * @returns This `Some` if the predicate returns true, otherwise `None`.\n */\n filter(predicate: (value: T) => boolean): Option<T> {\n return predicate(this._value) ? this : None();\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\nimport { None as NoneFactory } from \"../../option\";\nimport { Err } from \"@/result\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * Unlike `unwrap()`, this method does not throw an error.\n * @returns `undefined` because this is a `None`.\n */\n value(): undefined {\n return undefined;\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n\n /**\n * Computes and returns the default value using the provided function, since `None` has no value.\n * @template T The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function.\n */\n unwrapOrElse<T>(fn: () => T): T {\n return fn();\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value since this is `None`.\n * @template E The type of the error.\n * @param error The error value to use.\n * @returns An `Err` result containing the provided error.\n */\n okOr<E>(error: E) {\n return Err(error);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param _predicate The function to test the value (ignored since this is `None`).\n * @returns `None` since there is no value to filter.\n */\n filter<T>(_predicate: (value: never) => boolean): Option<T> {\n return NoneFactory();\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\nlet NONE_INSTANCE: NoneType | null = null;\n\n/**\n * Returns the singleton `None` instance, representing an `Option` with no value.\n * Uses a singleton pattern to reduce memory allocations.\n * @returns The singleton `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\tif (!NONE_INSTANCE) {\n\t\tNONE_INSTANCE = new NoneType();\n\t}\n\treturn NONE_INSTANCE;\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n"]}
1
+ {"version":3,"sources":["../src/helpers/match.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts"],"names":["Ok","Err","Some","None"],"mappings":";;;AAkHO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,OAAA,GAAU,MAAM,OAAO,CAAA;AAE7B,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,OAAQ,EAAA;AAAA;AAGjB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAA,OAAO,MAAM,OAAQ,EAAA;AAAA;AAGvB,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,yBAAA,EAA4B,MAAO,CAAA,OAAO,CAAC,CAAE,CAAA,CAAA;AAAA;AAK/D,EAAA,IAAI,OAAO,OAAY,KAAA,QAAA,IAAY,OAAY,KAAA,IAAA,IAAQ,CAAC,YAAc,EAAA;AAEpE,IAAA,KAAA,MAAW,OAAO,KAAO,EAAA;AACvB,MAAA,IAAI,QAAQ,SAAW,EAAA;AAEvB,MAAA,IAAI,OAAO,OAAS,EAAA;AACl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,IAAIA,KAAS,EAAA;AAAA;AAE9B,EAAO,OAAA,aAAA;AACR;AAEC,MAAA,CAAe,IAAOD,GAAAA,KAAAA;AACtB,MAAA,CAAe,IAAOC,GAAAA,KAAAA","file":"index.cjs","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Utility types for mixed primitive + object unions\ntype PrimitiveMembers<T> = Extract<T, PropertyKey>;\ntype ObjectKeys<T> = T extends object ? keyof T : never;\ntype ObjectPropertyType<T, K extends PropertyKey> = T extends object\n ? K extends keyof T\n ? T[K]\n : never\n : never;\n\n// Helper to determine if a key K is a primitive member or object key\ntype HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T>\n ? () => R\n : K extends ObjectKeys<T>\n ? (value: ObjectPropertyType<T, K>) => R\n : never;\n\n// Build cases type with a single mapped type\ntype MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true\n ? Partial<{\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }>\n : {\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }) &\n (HasDefault extends true ? { default: () => R } : {});\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for mixed primitive + object unions WITH default (cases optional)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, true>,\n): R;\n\n// Overload for mixed primitive + object unions WITHOUT default (exhaustive)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, false>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // NEW: Handle objects with specific property keys (like { Other: [...] })\n // This must come BEFORE discriminated union check to handle mixed unions\n if (typeof matcher === \"object\" && matcher !== null && !discriminant) {\n // Check if any case key matches a property in the matcher object\n for (const key in cases) {\n if (key === \"default\") continue;\n\n if (key in matcher) {\n const handler = cases[key];\n if (handler) {\n return typeof handler === \"function\"\n ? handler(matcher[key])\n : handler();\n }\n }\n }\n\n // If no match found and there's a default, use it\n if (cases.default) {\n return cases.default();\n }\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).\n */\nexport class Err<E> implements ResultDefinition<never, E> {\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance. Can be any type.\n */\n constructor(error: E) {\n // Store error as-is - conversion is handled by factory function\n this.error = error;\n // Object.setPrototypeOf(this, Err.prototype);\n //\n // if (Error.captureStackTrace) {\n // Error.captureStackTrace(this, Err);\n // }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error. Can be any type.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @template F The type of the error in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance cast to the new error type.\n */\n flatMap<U, F = E>(\n _fn: (value: never) => ResultDefinition<U, F>,\n ): ResultDefinition<U, E> {\n return this as unknown as ResultDefinition<U, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The error value contained in this `Err`.\n */\n value(): E {\n return this.error;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return since this is an `Err`.\n * @returns The provided default value.\n */\n unwrapOr<U>(defaultValue: U): U {\n return defaultValue;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function with the error.\n */\n unwrapOrElse<U>(fn: (error: E) => U): U {\n return fn(this.error);\n }\n\n /**\n * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.\n * @template T The type of the alternative success value.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The result of calling the provided function with the error.\n */\n orElse<T, F>(\n fn: (error: E) => ResultDefinition<T, F>,\n ): ResultDefinition<T, F> {\n return fn(this.error);\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param _value The value to wrap in the `Ok` instance.\n */\n constructor(private _value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The value contained in this `Ok`.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this._value)) as unknown as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @template E The type of the error in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U, E = never>(\n fn: (value: T) => ResultDefinition<U, E>,\n ): ResultDefinition<U, E> {\n return fn(this._value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`). Can be any type.\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return if this is an `Err`.\n * @returns The value contained in this `Ok`.\n */\n unwrapOr<U>(defaultValue: U): T {\n return this._value;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The value contained in this `Ok`.\n */\n unwrapOrElse<U>(fn: (error: never) => U): T {\n return this._value;\n }\n\n /**\n * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The original `Ok` instance.\n */\n orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F> {\n return this as unknown as ResultDefinition<T, F>;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return Some(this._value);\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates an Err - strings are converted to Error, everything else preserved\n */\nfunction Err<E>(error: E): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n","import type { None as NoneType } from \"./none\";\nimport type { Option } from \"./option\";\nimport { None } from \"../../option\";\nimport { Ok } from \"@/result\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param _value The value to be wrapped in the `Some` option.\n */\n constructor(private _value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is NoneType {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * @returns The value held by this `Some` option.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this._value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this._value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this._value;\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the function provided.\n * @template U The type of the default value.\n * @param _fn A function to compute the default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOrElse<U>(_fn: () => U): T {\n return this._value;\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value if this is `None`.\n * @template E The type of the error.\n * @param _error The error value to use if this is `None` (ignored since this is `Some`).\n * @returns An `Ok` result containing the value from this `Some`.\n */\n okOr<E>(_error: E) {\n return Ok(this._value);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param predicate The function to test the value.\n * @returns This `Some` if the predicate returns true, otherwise `None`.\n */\n filter(predicate: (value: T) => boolean): Option<T> {\n return predicate(this._value) ? this : None();\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\nimport { None as NoneFactory } from \"../../option\";\nimport { Err } from \"@/result\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * Unlike `unwrap()`, this method does not throw an error.\n * @returns `undefined` because this is a `None`.\n */\n value(): undefined {\n return undefined;\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n\n /**\n * Computes and returns the default value using the provided function, since `None` has no value.\n * @template T The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function.\n */\n unwrapOrElse<T>(fn: () => T): T {\n return fn();\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value since this is `None`.\n * @template E The type of the error.\n * @param error The error value to use.\n * @returns An `Err` result containing the provided error.\n */\n okOr<E>(error: E) {\n return Err(error);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param _predicate The function to test the value (ignored since this is `None`).\n * @returns `None` since there is no value to filter.\n */\n filter<T>(_predicate: (value: never) => boolean): Option<T> {\n return NoneFactory();\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\nlet NONE_INSTANCE: NoneType | null = null;\n\n/**\n * Returns the singleton `None` instance, representing an `Option` with no value.\n * Uses a singleton pattern to reduce memory allocations.\n * @returns The singleton `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\tif (!NONE_INSTANCE) {\n\t\tNONE_INSTANCE = new NoneType();\n\t}\n\treturn NONE_INSTANCE;\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n"]}
package/dist/index.js CHANGED
@@ -10,6 +10,20 @@ function match(matcher, cases, discriminant) {
10
10
  }
11
11
  throw new Error(`No case found for value: ${String(matcher)}`);
12
12
  }
13
+ if (typeof matcher === "object" && matcher !== null && !discriminant) {
14
+ for (const key in cases) {
15
+ if (key === "default") continue;
16
+ if (key in matcher) {
17
+ const handler = cases[key];
18
+ if (handler) {
19
+ return typeof handler === "function" ? handler(matcher[key]) : handler();
20
+ }
21
+ }
22
+ }
23
+ if (cases.default) {
24
+ return cases.default();
25
+ }
26
+ }
13
27
  if (discriminant && typeof matcher === "object" && matcher !== null) {
14
28
  const discriminantValue = matcher[discriminant];
15
29
  const handler = cases[discriminantValue];
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/helpers/match.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts"],"names":["Ok","Err","Some","None"],"mappings":";AA4EO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,O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AA,aAAA,GAAgB,IAAIA,KAAS,EAAA;AAAA;AAE9B,EAAO,OAAA,aAAA;AACR;AAEC,MAAA,CAAe,IAAOD,GAAAA,KAAAA;AACtB,MAAA,CAAe,IAAOC,GAAAA,KAAAA","file":"index.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).\n */\nexport class Err<E> implements ResultDefinition<never, E> {\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance. Can be any type.\n */\n constructor(error: E) {\n // Store error as-is - conversion is handled by factory function\n this.error = error;\n // Object.setPrototypeOf(this, Err.prototype);\n //\n // if (Error.captureStackTrace) {\n // Error.captureStackTrace(this, Err);\n // }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error. Can be any type.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @template F The type of the error in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance cast to the new error type.\n */\n flatMap<U, F = E>(\n _fn: (value: never) => ResultDefinition<U, F>,\n ): ResultDefinition<U, E> {\n return this as unknown as ResultDefinition<U, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The error value contained in this `Err`.\n */\n value(): E {\n return this.error;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return since this is an `Err`.\n * @returns The provided default value.\n */\n unwrapOr<U>(defaultValue: U): U {\n return defaultValue;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function with the error.\n */\n unwrapOrElse<U>(fn: (error: E) => U): U {\n return fn(this.error);\n }\n\n /**\n * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.\n * @template T The type of the alternative success value.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The result of calling the provided function with the error.\n */\n orElse<T, F>(\n fn: (error: E) => ResultDefinition<T, F>,\n ): ResultDefinition<T, F> {\n return fn(this.error);\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param _value The value to wrap in the `Ok` instance.\n */\n constructor(private _value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The value contained in this `Ok`.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this._value)) as unknown as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @template E The type of the error in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U, E = never>(\n fn: (value: T) => ResultDefinition<U, E>,\n ): ResultDefinition<U, E> {\n return fn(this._value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`). Can be any type.\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return if this is an `Err`.\n * @returns The value contained in this `Ok`.\n */\n unwrapOr<U>(defaultValue: U): T {\n return this._value;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The value contained in this `Ok`.\n */\n unwrapOrElse<U>(fn: (error: never) => U): T {\n return this._value;\n }\n\n /**\n * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The original `Ok` instance.\n */\n orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F> {\n return this as unknown as ResultDefinition<T, F>;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return Some(this._value);\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates an Err - strings are converted to Error, everything else preserved\n */\nfunction Err<E>(error: E): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n","import type { None as NoneType } from \"./none\";\nimport type { Option } from \"./option\";\nimport { None } from \"../../option\";\nimport { Ok } from \"@/result\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param _value The value to be wrapped in the `Some` option.\n */\n constructor(private _value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is NoneType {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * @returns The value held by this `Some` option.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this._value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this._value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this._value;\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the function provided.\n * @template U The type of the default value.\n * @param _fn A function to compute the default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOrElse<U>(_fn: () => U): T {\n return this._value;\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value if this is `None`.\n * @template E The type of the error.\n * @param _error The error value to use if this is `None` (ignored since this is `Some`).\n * @returns An `Ok` result containing the value from this `Some`.\n */\n okOr<E>(_error: E) {\n return Ok(this._value);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param predicate The function to test the value.\n * @returns This `Some` if the predicate returns true, otherwise `None`.\n */\n filter(predicate: (value: T) => boolean): Option<T> {\n return predicate(this._value) ? this : None();\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\nimport { None as NoneFactory } from \"../../option\";\nimport { Err } from \"@/result\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * Unlike `unwrap()`, this method does not throw an error.\n * @returns `undefined` because this is a `None`.\n */\n value(): undefined {\n return undefined;\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n\n /**\n * Computes and returns the default value using the provided function, since `None` has no value.\n * @template T The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function.\n */\n unwrapOrElse<T>(fn: () => T): T {\n return fn();\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value since this is `None`.\n * @template E The type of the error.\n * @param error The error value to use.\n * @returns An `Err` result containing the provided error.\n */\n okOr<E>(error: E) {\n return Err(error);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param _predicate The function to test the value (ignored since this is `None`).\n * @returns `None` since there is no value to filter.\n */\n filter<T>(_predicate: (value: never) => boolean): Option<T> {\n return NoneFactory();\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\nlet NONE_INSTANCE: NoneType | null = null;\n\n/**\n * Returns the singleton `None` instance, representing an `Option` with no value.\n * Uses a singleton pattern to reduce memory allocations.\n * @returns The singleton `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\tif (!NONE_INSTANCE) {\n\t\tNONE_INSTANCE = new NoneType();\n\t}\n\treturn NONE_INSTANCE;\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n"]}
1
+ {"version":3,"sources":["../src/helpers/match.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts"],"names":["Ok","Err","Some","None"],"mappings":";AAkHO,SAAS,KAAA,CACd,OACA,EAAA,KAAA,EACA,YACG,EAAA;AAEH,EACE,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,IAAA,OAAO,YAAY,QACnB,EAAA;AACA,IAAM,MAAA,OAAA,GAAU,MAAM,OAAO,CAAA;AAE7B,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,OAAO,OAAQ,EAAA;AAAA;AAGjB,IAAA,IAAI,MAAM,OAAS,EAAA;AACjB,MAAA,OAAO,MAAM,OAAQ,EAAA;AAAA;AAGvB,IAAA,MAAM,IAAI,KAAM,CAAA,CAAA,yBAAA,EAA4B,MAA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A;AAC1D,IAAA,OAAOE,KAAY,EAAA;AAAA;AAEvB,CAAA;;;ACrFA,SAASD,MAAQ,KAAuB,EAAA;AACvC,EAAO,OAAA,IAAIA,MAAS,KAAK,CAAA;AAC1B;AAEA,IAAI,aAAiC,GAAA,IAAA;AAWrC,SAASC,KAAiB,GAAA;AACzB,EAAA,IAAI,CAAC,aAAe,EAAA;AACnB,IAAA,aAAA,GAAgB,IAAIA,KAAS,EAAA;AAAA;AAE9B,EAAO,OAAA,aAAA;AACR;AAEC,MAAA,CAAe,IAAOD,GAAAA,KAAAA;AACtB,MAAA,CAAe,IAAOC,GAAAA,KAAAA","file":"index.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\n// Utility types for mixed primitive + object unions\ntype PrimitiveMembers<T> = Extract<T, PropertyKey>;\ntype ObjectKeys<T> = T extends object ? keyof T : never;\ntype ObjectPropertyType<T, K extends PropertyKey> = T extends object\n ? K extends keyof T\n ? T[K]\n : never\n : never;\n\n// Helper to determine if a key K is a primitive member or object key\ntype HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T>\n ? () => R\n : K extends ObjectKeys<T>\n ? (value: ObjectPropertyType<T, K>) => R\n : never;\n\n// Build cases type with a single mapped type\ntype MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true\n ? Partial<{\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }>\n : {\n [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;\n }) &\n (HasDefault extends true ? { default: () => R } : {});\n\n// Overload for Result type\nexport function match<T, E, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\n\n// Overload for Option type\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\n\n// Overload for mixed primitive + object unions WITH default (cases optional)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, true>,\n): R;\n\n// Overload for mixed primitive + object unions WITHOUT default (exhaustive)\nexport function match<T extends PropertyKey | object, R>(\n matcher: T,\n cases: MatchCases<T, R, false>,\n): R;\n\n// Overload for discriminated unions with default case\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]?: (value: Extract<T, { [P in D]: K }>) => R } & {\n default: (value: T) => R;\n },\n discriminant: D,\n): R;\n\n// Overload for discriminated unions without default case (exhaustive)\nexport function match<\n T extends { [K in D]: string | number | symbol },\n D extends keyof T,\n R,\n>(\n matcher: T,\n cases: { [K in T[D]]: (value: Extract<T, { [P in D]: K }>) => R },\n discriminant: D,\n): R;\n\n// Overload for primitives with default case\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Partial<Record<T, () => R>> & { default: () => R },\n): R;\n\n// Overload for primitives without default case (exhaustive)\nexport function match<T extends PropertyKey, R>(\n matcher: T,\n cases: Record<T, () => R>,\n): R;\n\n// Result -> Option conversion\nexport function match<T, E>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => Option<T>;\n Err: (error: E) => Option<T>;\n },\n): Option<T>;\n\n// Option -> Result conversion\nexport function match<T, E>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => Result<T, E>;\n None: () => Result<T, E>;\n },\n): Result<T, E>;\n\n// Implementation\nexport function match<T, E, R>(\n matcher: Result<T, E> | Option<T> | any,\n cases: any,\n discriminant?: keyof any,\n): R {\n // Handle primitives (string, number, symbol) FIRST\n if (\n typeof matcher === \"string\" ||\n typeof matcher === \"number\" ||\n typeof matcher === \"symbol\"\n ) {\n const handler = cases[matcher];\n\n if (handler) {\n return handler();\n }\n\n if (cases.default) {\n return cases.default();\n }\n\n throw new Error(`No case found for value: ${String(matcher)}`);\n }\n\n // NEW: Handle objects with specific property keys (like { Other: [...] })\n // This must come BEFORE discriminated union check to handle mixed unions\n if (typeof matcher === \"object\" && matcher !== null && !discriminant) {\n // Check if any case key matches a property in the matcher object\n for (const key in cases) {\n if (key === \"default\") continue;\n\n if (key in matcher) {\n const handler = cases[key];\n if (handler) {\n return typeof handler === \"function\"\n ? handler(matcher[key])\n : handler();\n }\n }\n }\n\n // If no match found and there's a default, use it\n if (cases.default) {\n return cases.default();\n }\n }\n\n // Handle discriminated unions\n if (discriminant && typeof matcher === \"object\" && matcher !== null) {\n const discriminantValue = matcher[discriminant];\n const handler = cases[discriminantValue];\n\n if (handler) {\n return handler(matcher);\n }\n\n if (cases.default) {\n return cases.default(matcher);\n }\n\n throw new Error(\n `No case found for discriminant value: ${String(discriminantValue)}`,\n );\n }\n\n // Early return for Result.Ok\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isOk\" in matcher &&\n matcher.isOk()\n ) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n // Early return for Result.Err\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isErr\" in matcher &&\n matcher.isErr()\n ) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n // Early return for Option.Some\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isSome\" in matcher &&\n matcher.isSome()\n ) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n // Early return for Option.None\n if (\n typeof matcher === \"object\" &&\n matcher !== null &&\n \"isNone\" in matcher &&\n matcher.isNone()\n ) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).\n */\nexport class Err<E> implements ResultDefinition<never, E> {\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance. Can be any type.\n */\n constructor(error: E) {\n // Store error as-is - conversion is handled by factory function\n this.error = error;\n // Object.setPrototypeOf(this, Err.prototype);\n //\n // if (Error.captureStackTrace) {\n // Error.captureStackTrace(this, Err);\n // }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error. Can be any type.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @template F The type of the error in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance cast to the new error type.\n */\n flatMap<U, F = E>(\n _fn: (value: never) => ResultDefinition<U, F>,\n ): ResultDefinition<U, E> {\n return this as unknown as ResultDefinition<U, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The error value contained in this `Err`.\n */\n value(): E {\n return this.error;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return since this is an `Err`.\n * @returns The provided default value.\n */\n unwrapOr<U>(defaultValue: U): U {\n return defaultValue;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function with the error.\n */\n unwrapOrElse<U>(fn: (error: E) => U): U {\n return fn(this.error);\n }\n\n /**\n * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.\n * @template T The type of the alternative success value.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The result of calling the provided function with the error.\n */\n orElse<T, F>(\n fn: (error: E) => ResultDefinition<T, F>,\n ): ResultDefinition<T, F> {\n return fn(this.error);\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param _value The value to wrap in the `Ok` instance.\n */\n constructor(private _value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The value contained in this `Ok`.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this._value)) as unknown as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @template E The type of the error in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U, E = never>(\n fn: (value: T) => ResultDefinition<U, E>,\n ): ResultDefinition<U, E> {\n return fn(this._value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`). Can be any type.\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return if this is an `Err`.\n * @returns The value contained in this `Ok`.\n */\n unwrapOr<U>(defaultValue: U): T {\n return this._value;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The value contained in this `Ok`.\n */\n unwrapOrElse<U>(fn: (error: never) => U): T {\n return this._value;\n }\n\n /**\n * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The original `Ok` instance.\n */\n orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F> {\n return this as unknown as ResultDefinition<T, F>;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return Some(this._value);\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates an Err - strings are converted to Error, everything else preserved\n */\nfunction Err<E>(error: E): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n","import type { None as NoneType } from \"./none\";\nimport type { Option } from \"./option\";\nimport { None } from \"../../option\";\nimport { Ok } from \"@/result\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param _value The value to be wrapped in the `Some` option.\n */\n constructor(private _value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is NoneType {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * @returns The value held by this `Some` option.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this._value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this._value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this._value;\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the function provided.\n * @template U The type of the default value.\n * @param _fn A function to compute the default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOrElse<U>(_fn: () => U): T {\n return this._value;\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value if this is `None`.\n * @template E The type of the error.\n * @param _error The error value to use if this is `None` (ignored since this is `Some`).\n * @returns An `Ok` result containing the value from this `Some`.\n */\n okOr<E>(_error: E) {\n return Ok(this._value);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param predicate The function to test the value.\n * @returns This `Some` if the predicate returns true, otherwise `None`.\n */\n filter(predicate: (value: T) => boolean): Option<T> {\n return predicate(this._value) ? this : None();\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\nimport { None as NoneFactory } from \"../../option\";\nimport { Err } from \"@/result\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`.\n * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).\n * Unlike `unwrap()`, this method does not throw an error.\n * @returns `undefined` because this is a `None`.\n */\n value(): undefined {\n return undefined;\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The singleton `None` instance.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return NoneFactory();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n\n /**\n * Computes and returns the default value using the provided function, since `None` has no value.\n * @template T The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function.\n */\n unwrapOrElse<T>(fn: () => T): T {\n return fn();\n }\n\n /**\n * Converts this `Option` to a `Result`, using the provided error value since this is `None`.\n * @template E The type of the error.\n * @param error The error value to use.\n * @returns An `Err` result containing the provided error.\n */\n okOr<E>(error: E) {\n return Err(error);\n }\n\n /**\n * Filters this `Option` based on a predicate function.\n * @param _predicate The function to test the value (ignored since this is `None`).\n * @returns `None` since there is no value to filter.\n */\n filter<T>(_predicate: (value: never) => boolean): Option<T> {\n return NoneFactory();\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\nlet NONE_INSTANCE: NoneType | null = null;\n\n/**\n * Returns the singleton `None` instance, representing an `Option` with no value.\n * Uses a singleton pattern to reduce memory allocations.\n * @returns The singleton `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\tif (!NONE_INSTANCE) {\n\t\tNONE_INSTANCE = new NoneType();\n\t}\n\treturn NONE_INSTANCE;\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n"]}
@@ -1,5 +1,16 @@
1
1
  import { R as Result, O as Option, a as Ok, E as Err, S as Some, N as None } from '../option-B_KKIecf.cjs';
2
2
 
3
+ type PrimitiveMembers<T> = Extract<T, PropertyKey>;
4
+ type ObjectKeys<T> = T extends object ? keyof T : never;
5
+ type ObjectPropertyType<T, K extends PropertyKey> = T extends object ? K extends keyof T ? T[K] : never : never;
6
+ type HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T> ? () => R : K extends ObjectKeys<T> ? (value: ObjectPropertyType<T, K>) => R : never;
7
+ type MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true ? Partial<{
8
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
9
+ }> : {
10
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
11
+ }) & (HasDefault extends true ? {
12
+ default: () => R;
13
+ } : {});
3
14
  declare global {
4
15
  export function match<T, E, R>(matcher: Result<T, E>, cases: {
5
16
  Ok: (value: T) => R;
@@ -9,10 +20,8 @@ declare global {
9
20
  Some: (value: T) => R;
10
21
  None: () => R;
11
22
  }): R;
12
- export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
13
- default: () => R;
14
- }): R;
15
- export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
23
+ export function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, true>): R;
24
+ export function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, false>): R;
16
25
  export function match<T extends {
17
26
  [K in D]: string | number | symbol;
18
27
  }, D extends keyof T, R>(matcher: T, cases: {
@@ -29,6 +38,10 @@ declare global {
29
38
  [P in D]: K;
30
39
  }>) => R;
31
40
  }, discriminant: D): R;
41
+ export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
42
+ default: () => R;
43
+ }): R;
44
+ export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
32
45
  export function match<T, E>(matcher: Result<T, E>, cases: {
33
46
  Ok: (value: T) => Option<T>;
34
47
  Err: (error: E) => Option<T>;
@@ -1,5 +1,16 @@
1
1
  import { R as Result, O as Option, a as Ok, E as Err, S as Some, N as None } from '../option-B_KKIecf.js';
2
2
 
3
+ type PrimitiveMembers<T> = Extract<T, PropertyKey>;
4
+ type ObjectKeys<T> = T extends object ? keyof T : never;
5
+ type ObjectPropertyType<T, K extends PropertyKey> = T extends object ? K extends keyof T ? T[K] : never : never;
6
+ type HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T> ? () => R : K extends ObjectKeys<T> ? (value: ObjectPropertyType<T, K>) => R : never;
7
+ type MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true ? Partial<{
8
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
9
+ }> : {
10
+ [K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
11
+ }) & (HasDefault extends true ? {
12
+ default: () => R;
13
+ } : {});
3
14
  declare global {
4
15
  export function match<T, E, R>(matcher: Result<T, E>, cases: {
5
16
  Ok: (value: T) => R;
@@ -9,10 +20,8 @@ declare global {
9
20
  Some: (value: T) => R;
10
21
  None: () => R;
11
22
  }): R;
12
- export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
13
- default: () => R;
14
- }): R;
15
- export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
23
+ export function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, true>): R;
24
+ export function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, false>): R;
16
25
  export function match<T extends {
17
26
  [K in D]: string | number | symbol;
18
27
  }, D extends keyof T, R>(matcher: T, cases: {
@@ -29,6 +38,10 @@ declare global {
29
38
  [P in D]: K;
30
39
  }>) => R;
31
40
  }, discriminant: D): R;
41
+ export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
42
+ default: () => R;
43
+ }): R;
44
+ export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
32
45
  export function match<T, E>(matcher: Result<T, E>, cases: {
33
46
  Ok: (value: T) => Option<T>;
34
47
  Err: (error: E) => Option<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@consolidados/results",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Result types, ease and simple",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",