@ls-stack/utils 2.14.1 → 3.2.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.
Files changed (50) hide show
  1. package/dist/assertions.cjs +5 -5
  2. package/dist/assertions.d.cts +2 -2
  3. package/dist/assertions.d.ts +2 -2
  4. package/dist/assertions.js +1 -1
  5. package/dist/cache.cjs +4 -1
  6. package/dist/cache.js +3 -3
  7. package/dist/castValues.cjs +12 -4
  8. package/dist/castValues.js +1 -1
  9. package/dist/{chunk-4UGSP3L3.js → chunk-3XCS7FVO.js} +5 -5
  10. package/dist/{chunk-RK6PT7JY.js → chunk-5MNYPLZI.js} +1 -1
  11. package/dist/chunk-II4R3VVX.js +25 -0
  12. package/dist/{chunk-KBFP7INB.js → chunk-J73KJABC.js} +67 -38
  13. package/dist/{chunk-T5WDDPFI.js → chunk-OHHF4CJZ.js} +1 -1
  14. package/dist/createThrottleController.js +4 -4
  15. package/dist/enhancedMap.js +2 -2
  16. package/dist/exhaustiveMatch.cjs +13 -2
  17. package/dist/exhaustiveMatch.d.cts +4 -1
  18. package/dist/exhaustiveMatch.d.ts +4 -1
  19. package/dist/exhaustiveMatch.js +11 -1
  20. package/dist/getValueStableKey.cjs +83 -0
  21. package/dist/getValueStableKey.d.cts +10 -0
  22. package/dist/getValueStableKey.d.ts +10 -0
  23. package/dist/getValueStableKey.js +55 -0
  24. package/dist/interpolate.js +1 -1
  25. package/dist/main.d.ts +2 -4
  26. package/dist/parallelAsyncCalls.cjs +51 -18
  27. package/dist/parallelAsyncCalls.d.cts +1 -2
  28. package/dist/parallelAsyncCalls.d.ts +1 -2
  29. package/dist/parallelAsyncCalls.js +5 -5
  30. package/dist/testUtils.js +1 -1
  31. package/dist/time.cjs +12 -4
  32. package/dist/time.js +2 -2
  33. package/dist/{rsResult.cjs → tsResult.cjs} +74 -43
  34. package/dist/{rsResult.d.cts → tsResult.d.cts} +34 -23
  35. package/dist/{rsResult.d.ts → tsResult.d.ts} +34 -23
  36. package/dist/tsResult.js +16 -0
  37. package/dist/typingTestUtils.cjs +4 -1
  38. package/dist/typingTestUtils.d.cts +26 -0
  39. package/dist/typingTestUtils.d.ts +26 -0
  40. package/dist/typingTestUtils.js +4 -1
  41. package/dist/typingUtils.d.cts +4 -1
  42. package/dist/typingUtils.d.ts +4 -1
  43. package/dist/yamlStringify.js +1 -1
  44. package/package.json +15 -31
  45. package/dist/chunk-GBFS2I67.js +0 -17
  46. package/dist/getObjStableKey.cjs +0 -83
  47. package/dist/getObjStableKey.d.cts +0 -3
  48. package/dist/getObjStableKey.d.ts +0 -3
  49. package/dist/getObjStableKey.js +0 -44
  50. package/dist/rsResult.js +0 -20
@@ -34,6 +34,11 @@ function isObject(value) {
34
34
  return typeof value === "object" && value !== null && !Array.isArray(value);
35
35
  }
36
36
 
37
+ // src/sleep.ts
38
+ function sleep(ms) {
39
+ return new Promise((resolve) => setTimeout(resolve, ms));
40
+ }
41
+
37
42
  // src/safeJson.ts
38
43
  function safeJsonStringify(value) {
39
44
  try {
@@ -43,7 +48,7 @@ function safeJsonStringify(value) {
43
48
  }
44
49
  }
45
50
 
46
- // src/rsResult.ts
51
+ // src/tsResult.ts
47
52
  function okUnwrapOr() {
48
53
  return this.value;
49
54
  }
@@ -62,28 +67,40 @@ function mapOkAndErr({
62
67
  function returnResult() {
63
68
  return this;
64
69
  }
70
+ function okOnOk(fn) {
71
+ if (this.ok) {
72
+ fn(this.value);
73
+ }
74
+ return this;
75
+ }
76
+ function errOnErr(fn) {
77
+ if (!this.ok) {
78
+ fn(this.error);
79
+ }
80
+ return this;
81
+ }
65
82
  function ok(value = void 0) {
66
- return {
67
- ok: true,
68
- error: false,
69
- value,
83
+ const methods = {
70
84
  unwrapOrNull: okUnwrapOr,
71
85
  unwrapOr: okUnwrapOr,
72
86
  unwrap: okUnwrapOr,
73
87
  mapOk: okMap,
74
88
  mapErr: returnResult,
75
- mapOkAndErr
89
+ mapOkAndErr,
90
+ ifOk: okOnOk,
91
+ ifErr: returnResult
92
+ };
93
+ return {
94
+ ok: true,
95
+ error: false,
96
+ value,
97
+ ...methods
76
98
  };
77
99
  }
78
100
  function err(error) {
79
- return {
80
- ok: false,
81
- error,
101
+ const methods = {
82
102
  unwrapOrNull: () => null,
83
103
  unwrapOr: (defaultValue) => defaultValue,
84
- errorResult() {
85
- return err(error);
86
- },
87
104
  unwrap: () => {
88
105
  if (error instanceof Error) {
89
106
  throw error;
@@ -92,7 +109,17 @@ function err(error) {
92
109
  },
93
110
  mapOk: returnResult,
94
111
  mapErr: errMap,
95
- mapOkAndErr
112
+ mapOkAndErr,
113
+ ifOk: returnResult,
114
+ ifErr: errOnErr
115
+ };
116
+ return {
117
+ ok: false,
118
+ error,
119
+ errorResult() {
120
+ return err(error);
121
+ },
122
+ ...methods
96
123
  };
97
124
  }
98
125
  function unknownToResultError(error) {
@@ -132,7 +159,8 @@ var Result = {
132
159
  err,
133
160
  unknownToError: unknownToResultError,
134
161
  asyncUnwrap,
135
- asyncMap
162
+ asyncMap,
163
+ getOkErr
136
164
  };
137
165
  function unknownToError(error) {
138
166
  if (error instanceof Error) return error;
@@ -149,10 +177,15 @@ function unknownToError(error) {
149
177
  cause: error
150
178
  });
151
179
  }
152
-
153
- // src/sleep.ts
154
- function sleep(ms) {
155
- return new Promise((resolve) => setTimeout(resolve, ms));
180
+ var typedResult = {
181
+ ok,
182
+ err,
183
+ get _type() {
184
+ throw new Error("usage as value is not allowed");
185
+ }
186
+ };
187
+ function getOkErr() {
188
+ return typedResult;
156
189
  }
157
190
 
158
191
  // src/parallelAsyncCalls.ts
@@ -1,5 +1,4 @@
1
- import { Result } from './rsResult.cjs';
2
- import './safeJson.cjs';
1
+ import { Result } from './tsResult.cjs';
3
2
 
4
3
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
5
4
  type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
@@ -1,5 +1,4 @@
1
- import { Result } from './rsResult.js';
2
- import './safeJson.js';
1
+ import { Result } from './tsResult.js';
3
2
 
4
3
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
5
4
  type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
@@ -1,15 +1,15 @@
1
- import {
2
- sleep
3
- } from "./chunk-5DZT3Z5Z.js";
4
1
  import {
5
2
  Result,
6
3
  unknownToError
7
- } from "./chunk-KBFP7INB.js";
4
+ } from "./chunk-J73KJABC.js";
5
+ import {
6
+ sleep
7
+ } from "./chunk-5DZT3Z5Z.js";
8
8
  import "./chunk-VAAMRG4K.js";
9
9
  import {
10
10
  invariant,
11
11
  isObject
12
- } from "./chunk-4UGSP3L3.js";
12
+ } from "./chunk-3XCS7FVO.js";
13
13
 
14
14
  // src/parallelAsyncCalls.ts
15
15
  var ParallelAsyncResultCalls = class {
package/dist/testUtils.js CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  } from "./chunk-HTCYUMDR.js";
15
15
  import {
16
16
  isObject
17
- } from "./chunk-4UGSP3L3.js";
17
+ } from "./chunk-3XCS7FVO.js";
18
18
 
19
19
  // src/testUtils.ts
20
20
  function createLoggerStore({
package/dist/time.cjs CHANGED
@@ -41,11 +41,19 @@ module.exports = __toCommonJS(time_exports);
41
41
 
42
42
  // src/castValues.ts
43
43
  function castToNumber(value) {
44
- return isNumeric(value) ? Number(value) : null;
44
+ return isFiniteNumeric(value) ? Number(value) : null;
45
45
  }
46
- function isNumeric(num) {
47
- const str = String(num);
48
- return !isNaN(str) && !isNaN(parseFloat(str));
46
+ function isFiniteNumeric(num) {
47
+ switch (typeof num) {
48
+ case "number":
49
+ return num - num === 0;
50
+ case "string":
51
+ return num.trim() !== "" && Number.isFinite(+num);
52
+ case "bigint":
53
+ return Number.isFinite(Number(num));
54
+ default:
55
+ return false;
56
+ }
49
57
  }
50
58
 
51
59
  // src/mathUtils.ts
package/dist/time.js CHANGED
@@ -15,9 +15,9 @@ import {
15
15
  getUnixSeconds,
16
16
  msToTimeString,
17
17
  parseTimeStringToMs
18
- } from "./chunk-RK6PT7JY.js";
18
+ } from "./chunk-5MNYPLZI.js";
19
19
  import "./chunk-HTCYUMDR.js";
20
- import "./chunk-GBFS2I67.js";
20
+ import "./chunk-II4R3VVX.js";
21
21
  export {
22
22
  DAY_AS_MS,
23
23
  DAY_AS_SECS,
@@ -17,23 +17,27 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/rsResult.ts
21
- var rsResult_exports = {};
22
- __export(rsResult_exports, {
20
+ // src/tsResult.ts
21
+ var tsResult_exports = {};
22
+ __export(tsResult_exports, {
23
23
  Result: () => Result,
24
- asyncResultify: () => asyncResultify,
25
- createTypedResult: () => createTypedResult,
26
- normalizeError: () => normalizeError,
24
+ err: () => err,
25
+ ok: () => ok,
27
26
  resultify: () => resultify,
28
- safeJsonStringify: () => safeJsonStringify2,
29
27
  unknownToError: () => unknownToError
30
28
  });
31
- module.exports = __toCommonJS(rsResult_exports);
29
+ module.exports = __toCommonJS(tsResult_exports);
32
30
 
33
31
  // src/assertions.ts
34
32
  function isObject(value) {
35
33
  return typeof value === "object" && value !== null && !Array.isArray(value);
36
34
  }
35
+ function isFunction(value) {
36
+ return typeof value === "function";
37
+ }
38
+ function isPromise(value) {
39
+ return isObject(value) && "then" in value && isFunction(value.then);
40
+ }
37
41
 
38
42
  // src/safeJson.ts
39
43
  function safeJsonStringify(value) {
@@ -44,7 +48,7 @@ function safeJsonStringify(value) {
44
48
  }
45
49
  }
46
50
 
47
- // src/rsResult.ts
51
+ // src/tsResult.ts
48
52
  function okUnwrapOr() {
49
53
  return this.value;
50
54
  }
@@ -63,28 +67,40 @@ function mapOkAndErr({
63
67
  function returnResult() {
64
68
  return this;
65
69
  }
70
+ function okOnOk(fn) {
71
+ if (this.ok) {
72
+ fn(this.value);
73
+ }
74
+ return this;
75
+ }
76
+ function errOnErr(fn) {
77
+ if (!this.ok) {
78
+ fn(this.error);
79
+ }
80
+ return this;
81
+ }
66
82
  function ok(value = void 0) {
67
- return {
68
- ok: true,
69
- error: false,
70
- value,
83
+ const methods = {
71
84
  unwrapOrNull: okUnwrapOr,
72
85
  unwrapOr: okUnwrapOr,
73
86
  unwrap: okUnwrapOr,
74
87
  mapOk: okMap,
75
88
  mapErr: returnResult,
76
- mapOkAndErr
89
+ mapOkAndErr,
90
+ ifOk: okOnOk,
91
+ ifErr: returnResult
92
+ };
93
+ return {
94
+ ok: true,
95
+ error: false,
96
+ value,
97
+ ...methods
77
98
  };
78
99
  }
79
100
  function err(error) {
80
- return {
81
- ok: false,
82
- error,
101
+ const methods = {
83
102
  unwrapOrNull: () => null,
84
103
  unwrapOr: (defaultValue) => defaultValue,
85
- errorResult() {
86
- return err(error);
87
- },
88
104
  unwrap: () => {
89
105
  if (error instanceof Error) {
90
106
  throw error;
@@ -93,7 +109,17 @@ function err(error) {
93
109
  },
94
110
  mapOk: returnResult,
95
111
  mapErr: errMap,
96
- mapOkAndErr
112
+ mapOkAndErr,
113
+ ifOk: returnResult,
114
+ ifErr: errOnErr
115
+ };
116
+ return {
117
+ ok: false,
118
+ error,
119
+ errorResult() {
120
+ return err(error);
121
+ },
122
+ ...methods
97
123
  };
98
124
  }
99
125
  function unknownToResultError(error) {
@@ -133,20 +159,27 @@ var Result = {
133
159
  err,
134
160
  unknownToError: unknownToResultError,
135
161
  asyncUnwrap,
136
- asyncMap
162
+ asyncMap,
163
+ getOkErr
137
164
  };
138
165
  function resultify(fn, errorNormalizer) {
139
- try {
140
- return ok(fn());
141
- } catch (error) {
142
- return err(
143
- errorNormalizer ? errorNormalizer(error) : unknownToError(error)
166
+ if (!isFunction(fn)) {
167
+ return fn.then((value) => ok(value)).catch(
168
+ (error) => err(
169
+ errorNormalizer ? errorNormalizer(error) : unknownToError(error)
170
+ )
144
171
  );
145
172
  }
146
- }
147
- async function asyncResultify(fn, errorNormalizer) {
148
173
  try {
149
- return ok(await fn());
174
+ const result = fn();
175
+ if (isPromise(result)) {
176
+ return result.then((value) => ok(value)).catch(
177
+ (error) => err(
178
+ errorNormalizer ? errorNormalizer(error) : unknownToError(error)
179
+ )
180
+ );
181
+ }
182
+ return ok(result);
150
183
  } catch (error) {
151
184
  return err(
152
185
  errorNormalizer ? errorNormalizer(error) : unknownToError(error)
@@ -168,23 +201,21 @@ function unknownToError(error) {
168
201
  cause: error
169
202
  });
170
203
  }
171
- function normalizeError(error) {
172
- return unknownToError(error);
173
- }
174
- var safeJsonStringify2 = safeJsonStringify;
175
- function createTypedResult() {
176
- return {
177
- ok,
178
- err
179
- };
204
+ var typedResult = {
205
+ ok,
206
+ err,
207
+ get _type() {
208
+ throw new Error("usage as value is not allowed");
209
+ }
210
+ };
211
+ function getOkErr() {
212
+ return typedResult;
180
213
  }
181
214
  // Annotate the CommonJS export names for ESM import in node:
182
215
  0 && (module.exports = {
183
216
  Result,
184
- asyncResultify,
185
- createTypedResult,
186
- normalizeError,
217
+ err,
218
+ ok,
187
219
  resultify,
188
- safeJsonStringify,
189
220
  unknownToError
190
221
  });
@@ -1,16 +1,15 @@
1
- import { safeJsonStringify as safeJsonStringify$1 } from './safeJson.cjs';
2
-
3
1
  type Ok<T> = {
4
2
  ok: true;
5
3
  error: false;
6
4
  value: T;
7
- };
5
+ } & AnyResultMethods;
6
+ type AnyResultMethods = Record<ResultMethodsKeys, never>;
8
7
  type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
9
8
  type Err<E extends ResultValidErrors> = {
10
9
  ok: false;
11
10
  error: E;
12
11
  errorResult: () => Result<any, E>;
13
- };
12
+ } & AnyResultMethods;
14
13
  type ResultMethods<T, E extends ResultValidErrors> = {
15
14
  /** Returns the value if the result is Ok, otherwise returns null */
16
15
  unwrapOrNull: () => T | null;
@@ -23,13 +22,14 @@ type ResultMethods<T, E extends ResultValidErrors> = {
23
22
  ok: (value: T) => NewValue;
24
23
  err: (error: E) => NewError;
25
24
  }) => Result<NewValue, NewError>;
25
+ ifOk: (fn: (value: T) => void) => Result<T, E>;
26
+ ifErr: (fn: (error: E) => void) => Result<T, E>;
26
27
  };
27
- type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
28
- declare function ok(): OkResult<void, any>;
29
- declare function ok<T>(value: T): OkResult<T, any>;
30
- type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
31
- declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
32
- declare function unknownToResultError(error: unknown): ErrResult<Error, any>;
28
+ type ResultMethodsKeys = keyof ResultMethods<any, any>;
29
+ declare function ok(): Ok<void>;
30
+ declare function ok<T>(value: T): Ok<T>;
31
+ declare function err<E extends ResultValidErrors>(error: E): Err<E>;
32
+ declare function unknownToResultError(error: unknown): Err<Error>;
33
33
  /** Unwraps a promise result */
34
34
  declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
35
35
  declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
@@ -62,18 +62,18 @@ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise
62
62
  * // result.error is an Error
63
63
  * }
64
64
  */
65
- type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrResult<E, T>;
65
+ type Result<T, E extends ResultValidErrors = Error> = (Omit<Ok<T>, ResultMethodsKeys> | Omit<Err<E>, ResultMethodsKeys>) & ResultMethods<T, E>;
66
66
  declare const Result: {
67
67
  ok: typeof ok;
68
68
  err: typeof err;
69
69
  unknownToError: typeof unknownToResultError;
70
70
  asyncUnwrap: typeof asyncUnwrap;
71
71
  asyncMap: typeof asyncMap;
72
+ getOkErr: typeof getOkErr;
72
73
  };
73
- /** transform a function in a result function */
74
- declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
75
- /** transform a async function in a result function */
76
- declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
74
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T extends Promise<any> ? never : T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
75
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
76
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<T, E>>;
77
77
  /**
78
78
  * Converts an unknown error value into an Error object.
79
79
  *
@@ -96,14 +96,25 @@ declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>
96
96
  * The original error value is preserved as the `cause` property of the returned Error.
97
97
  */
98
98
  declare function unknownToError(error: unknown): Error;
99
- /** @deprecated use unknownToError instead, this will be removed in the next major version */
100
- declare function normalizeError(error: unknown): Error;
101
- /** @deprecated use safeJsonStringify from `@ls-stack/utils/safeJson` instead, this will be removed in the next major version */
102
- declare const safeJsonStringify: typeof safeJsonStringify$1;
103
99
  type TypedResult<T, E extends ResultValidErrors = Error> = {
104
- ok: (value: T) => OkResult<T, E, T>;
105
- err: (error: E) => ErrResult<E, T>;
100
+ ok: (value: T) => Ok<T>;
101
+ err: (error: E) => Err<E>;
102
+ /**
103
+ * Use in combination with `typeof` to get the return type of the result
104
+ *
105
+ * @example
106
+ * const typedResult = createTypedResult<{ a: 'test' }>();
107
+ *
108
+ * function foo(): typeof typedResult.type {
109
+ * return typedResult.ok({ a: 'test' });
110
+ * }
111
+ */
112
+ _type: Result<T, E>;
106
113
  };
107
- declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
114
+ type GetTypedResult<R extends Result<any, any>> = TypedResult<R extends Result<infer T, any> ? T : never, R extends Result<any, infer E> ? E : never>;
115
+ declare function getOkErr<F extends (...args: any[]) => Promise<Result<any, any>>>(): TypedResult<Awaited<ReturnType<F>> extends Result<infer T, any> ? T : never, Awaited<ReturnType<F>> extends Result<any, infer E> ? E : never>;
116
+ declare function getOkErr<F extends (...args: any[]) => Result<any, any>>(): TypedResult<ReturnType<F> extends Result<infer T, any> ? T : never, ReturnType<F> extends Result<any, infer E> ? E : never>;
117
+ declare function getOkErr<R extends Result<any, any>>(): TypedResult<R extends Result<infer T, any> ? T : never, R extends Result<any, infer E> ? E : never>;
118
+ declare function getOkErr<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
108
119
 
109
- export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify, unknownToError };
120
+ export { type GetTypedResult, Result, type ResultValidErrors, type TypedResult, err, ok, resultify, unknownToError };
@@ -1,16 +1,15 @@
1
- import { safeJsonStringify as safeJsonStringify$1 } from './safeJson.js';
2
-
3
1
  type Ok<T> = {
4
2
  ok: true;
5
3
  error: false;
6
4
  value: T;
7
- };
5
+ } & AnyResultMethods;
6
+ type AnyResultMethods = Record<ResultMethodsKeys, never>;
8
7
  type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
9
8
  type Err<E extends ResultValidErrors> = {
10
9
  ok: false;
11
10
  error: E;
12
11
  errorResult: () => Result<any, E>;
13
- };
12
+ } & AnyResultMethods;
14
13
  type ResultMethods<T, E extends ResultValidErrors> = {
15
14
  /** Returns the value if the result is Ok, otherwise returns null */
16
15
  unwrapOrNull: () => T | null;
@@ -23,13 +22,14 @@ type ResultMethods<T, E extends ResultValidErrors> = {
23
22
  ok: (value: T) => NewValue;
24
23
  err: (error: E) => NewError;
25
24
  }) => Result<NewValue, NewError>;
25
+ ifOk: (fn: (value: T) => void) => Result<T, E>;
26
+ ifErr: (fn: (error: E) => void) => Result<T, E>;
26
27
  };
27
- type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
28
- declare function ok(): OkResult<void, any>;
29
- declare function ok<T>(value: T): OkResult<T, any>;
30
- type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
31
- declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
32
- declare function unknownToResultError(error: unknown): ErrResult<Error, any>;
28
+ type ResultMethodsKeys = keyof ResultMethods<any, any>;
29
+ declare function ok(): Ok<void>;
30
+ declare function ok<T>(value: T): Ok<T>;
31
+ declare function err<E extends ResultValidErrors>(error: E): Err<E>;
32
+ declare function unknownToResultError(error: unknown): Err<Error>;
33
33
  /** Unwraps a promise result */
34
34
  declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
35
35
  declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
@@ -62,18 +62,18 @@ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise
62
62
  * // result.error is an Error
63
63
  * }
64
64
  */
65
- type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrResult<E, T>;
65
+ type Result<T, E extends ResultValidErrors = Error> = (Omit<Ok<T>, ResultMethodsKeys> | Omit<Err<E>, ResultMethodsKeys>) & ResultMethods<T, E>;
66
66
  declare const Result: {
67
67
  ok: typeof ok;
68
68
  err: typeof err;
69
69
  unknownToError: typeof unknownToResultError;
70
70
  asyncUnwrap: typeof asyncUnwrap;
71
71
  asyncMap: typeof asyncMap;
72
+ getOkErr: typeof getOkErr;
72
73
  };
73
- /** transform a function in a result function */
74
- declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
75
- /** transform a async function in a result function */
76
- declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
74
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T extends Promise<any> ? never : T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
75
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
76
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<T, E>>;
77
77
  /**
78
78
  * Converts an unknown error value into an Error object.
79
79
  *
@@ -96,14 +96,25 @@ declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>
96
96
  * The original error value is preserved as the `cause` property of the returned Error.
97
97
  */
98
98
  declare function unknownToError(error: unknown): Error;
99
- /** @deprecated use unknownToError instead, this will be removed in the next major version */
100
- declare function normalizeError(error: unknown): Error;
101
- /** @deprecated use safeJsonStringify from `@ls-stack/utils/safeJson` instead, this will be removed in the next major version */
102
- declare const safeJsonStringify: typeof safeJsonStringify$1;
103
99
  type TypedResult<T, E extends ResultValidErrors = Error> = {
104
- ok: (value: T) => OkResult<T, E, T>;
105
- err: (error: E) => ErrResult<E, T>;
100
+ ok: (value: T) => Ok<T>;
101
+ err: (error: E) => Err<E>;
102
+ /**
103
+ * Use in combination with `typeof` to get the return type of the result
104
+ *
105
+ * @example
106
+ * const typedResult = createTypedResult<{ a: 'test' }>();
107
+ *
108
+ * function foo(): typeof typedResult.type {
109
+ * return typedResult.ok({ a: 'test' });
110
+ * }
111
+ */
112
+ _type: Result<T, E>;
106
113
  };
107
- declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
114
+ type GetTypedResult<R extends Result<any, any>> = TypedResult<R extends Result<infer T, any> ? T : never, R extends Result<any, infer E> ? E : never>;
115
+ declare function getOkErr<F extends (...args: any[]) => Promise<Result<any, any>>>(): TypedResult<Awaited<ReturnType<F>> extends Result<infer T, any> ? T : never, Awaited<ReturnType<F>> extends Result<any, infer E> ? E : never>;
116
+ declare function getOkErr<F extends (...args: any[]) => Result<any, any>>(): TypedResult<ReturnType<F> extends Result<infer T, any> ? T : never, ReturnType<F> extends Result<any, infer E> ? E : never>;
117
+ declare function getOkErr<R extends Result<any, any>>(): TypedResult<R extends Result<infer T, any> ? T : never, R extends Result<any, infer E> ? E : never>;
118
+ declare function getOkErr<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
108
119
 
109
- export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify, unknownToError };
120
+ export { type GetTypedResult, Result, type ResultValidErrors, type TypedResult, err, ok, resultify, unknownToError };
@@ -0,0 +1,16 @@
1
+ import {
2
+ Result,
3
+ err,
4
+ ok,
5
+ resultify,
6
+ unknownToError
7
+ } from "./chunk-J73KJABC.js";
8
+ import "./chunk-VAAMRG4K.js";
9
+ import "./chunk-3XCS7FVO.js";
10
+ export {
11
+ Result,
12
+ err,
13
+ ok,
14
+ resultify,
15
+ unknownToError
16
+ };
@@ -32,10 +32,13 @@ function describe(title, func) {
32
32
  function expectType() {
33
33
  return {};
34
34
  }
35
+ function expectTypesAre(result) {
36
+ }
35
37
  var typingTest = {
36
38
  test,
37
39
  describe,
38
- expectType
40
+ expectType,
41
+ expectTypesAre
39
42
  };
40
43
  // Annotate the CommonJS export names for ESM import in node:
41
44
  0 && (module.exports = {
@@ -2,11 +2,37 @@ type TestTypeIsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T e
2
2
  type TestTypeNotEqual<X, Y> = true extends TestTypeIsEqual<X, Y> ? false : true;
3
3
  declare function test(title: string, func: () => any): void;
4
4
  declare function describe(title: string, func: () => any): void;
5
+ /**
6
+ * Helper function for type testing that ensures a type extends `true`.
7
+ * Used in combination with `TestTypeIsEqual` to verify type equality at compile time.
8
+ *
9
+ * @example
10
+ * expectType<TestTypeIsEqual<string, string>>(); // OK
11
+ * expectType<TestTypeIsEqual<string, number>>(); // Type error
12
+ *
13
+ * @template T Type that must extend `true`
14
+ * @returns An empty object cast to type T
15
+ */
5
16
  declare function expectType<T extends true>(): T;
17
+ /**
18
+ * Helper function for type testing that compares two types and expects a specific result.
19
+ * This function allows for more explicit type equality assertions with a descriptive result.
20
+ *
21
+ * @template X First type to compare
22
+ * @template Y Second type to compare
23
+ * @param result Expected comparison result: 'equal' if types are equal, 'notEqual' if they differ
24
+ *
25
+ * @example
26
+ * expectTypesAre<string, string>('equal'); // OK
27
+ * expectTypesAre<string, number>('notEqual'); // OK
28
+ * expectTypesAre<string, string>('notEqual'); // Type error
29
+ */
30
+ declare function expectTypesAre<X, Y>(result: TestTypeIsEqual<X, Y> extends true ? 'equal' : 'notEqual'): void;
6
31
  declare const typingTest: {
7
32
  test: typeof test;
8
33
  describe: typeof describe;
9
34
  expectType: typeof expectType;
35
+ expectTypesAre: typeof expectTypesAre;
10
36
  };
11
37
 
12
38
  export { type TestTypeIsEqual, type TestTypeNotEqual, typingTest };