@ls-stack/utils 3.3.2 → 3.5.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.
@@ -26,6 +26,7 @@ __export(arrayUtils_exports, {
26
26
  findAfterIndex: () => findAfterIndex,
27
27
  findBeforeIndex: () => findBeforeIndex,
28
28
  isInArray: () => isInArray,
29
+ rejectArrayUndefinedValues: () => rejectArrayUndefinedValues,
29
30
  sortBy: () => sortBy
30
31
  });
31
32
  module.exports = __toCommonJS(arrayUtils_exports);
@@ -103,6 +104,9 @@ function findBeforeIndex(array, index, predicate) {
103
104
  }
104
105
  return void 0;
105
106
  }
107
+ function rejectArrayUndefinedValues(array) {
108
+ return array.filter((item) => item !== void 0);
109
+ }
106
110
  // Annotate the CommonJS export names for ESM import in node:
107
111
  0 && (module.exports = {
108
112
  arrayWithPrev,
@@ -111,5 +115,6 @@ function findBeforeIndex(array, index, predicate) {
111
115
  findAfterIndex,
112
116
  findBeforeIndex,
113
117
  isInArray,
118
+ rejectArrayUndefinedValues,
114
119
  sortBy
115
120
  });
@@ -49,5 +49,6 @@ declare function arrayWithPrevAndIndex<T>(array: T[]): {
49
49
  declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]): value is U;
50
50
  declare function findAfterIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
51
51
  declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
52
+ declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
52
53
 
53
- export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, sortBy };
54
+ export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, rejectArrayUndefinedValues, sortBy };
@@ -49,5 +49,6 @@ declare function arrayWithPrevAndIndex<T>(array: T[]): {
49
49
  declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]): value is U;
50
50
  declare function findAfterIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
51
51
  declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
52
+ declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
52
53
 
53
- export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, sortBy };
54
+ export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, rejectArrayUndefinedValues, sortBy };
package/lib/arrayUtils.js CHANGED
@@ -5,8 +5,9 @@ import {
5
5
  findAfterIndex,
6
6
  findBeforeIndex,
7
7
  isInArray,
8
+ rejectArrayUndefinedValues,
8
9
  sortBy
9
- } from "./chunk-QMFZE2VO.js";
10
+ } from "./chunk-5JQGMNNY.js";
10
11
  export {
11
12
  arrayWithPrev,
12
13
  arrayWithPrevAndIndex,
@@ -14,5 +15,6 @@ export {
14
15
  findAfterIndex,
15
16
  findBeforeIndex,
16
17
  isInArray,
18
+ rejectArrayUndefinedValues,
17
19
  sortBy
18
20
  };
@@ -73,6 +73,9 @@ function findBeforeIndex(array, index, predicate) {
73
73
  }
74
74
  return void 0;
75
75
  }
76
+ function rejectArrayUndefinedValues(array) {
77
+ return array.filter((item) => item !== void 0);
78
+ }
76
79
 
77
80
  export {
78
81
  filterAndMap,
@@ -81,5 +84,6 @@ export {
81
84
  arrayWithPrevAndIndex,
82
85
  isInArray,
83
86
  findAfterIndex,
84
- findBeforeIndex
87
+ findBeforeIndex,
88
+ rejectArrayUndefinedValues
85
89
  };
@@ -32,6 +32,15 @@ function omit(obj, keys) {
32
32
  function looseGetObjectProperty(obj, key) {
33
33
  return obj[key];
34
34
  }
35
+ function rejectObjUndefinedValues(obj) {
36
+ const result = {};
37
+ for (const key in obj) {
38
+ if (obj[key] !== void 0) {
39
+ result[key] = obj[key];
40
+ }
41
+ }
42
+ return result;
43
+ }
35
44
 
36
45
  export {
37
46
  objectTypedEntries,
@@ -39,5 +48,6 @@ export {
39
48
  mapArrayToObject,
40
49
  mapObjectToObject,
41
50
  omit,
42
- looseGetObjectProperty
51
+ looseGetObjectProperty,
52
+ rejectObjUndefinedValues
43
53
  };
package/lib/objUtils.cjs CHANGED
@@ -25,7 +25,8 @@ __export(objUtils_exports, {
25
25
  mapObjectToObject: () => mapObjectToObject,
26
26
  objectTypedEntries: () => objectTypedEntries,
27
27
  omit: () => omit,
28
- pick: () => pick
28
+ pick: () => pick,
29
+ rejectObjUndefinedValues: () => rejectObjUndefinedValues
29
30
  });
30
31
  module.exports = __toCommonJS(objUtils_exports);
31
32
  function objectTypedEntries(obj) {
@@ -61,6 +62,15 @@ function omit(obj, keys) {
61
62
  function looseGetObjectProperty(obj, key) {
62
63
  return obj[key];
63
64
  }
65
+ function rejectObjUndefinedValues(obj) {
66
+ const result = {};
67
+ for (const key in obj) {
68
+ if (obj[key] !== void 0) {
69
+ result[key] = obj[key];
70
+ }
71
+ }
72
+ return result;
73
+ }
64
74
  // Annotate the CommonJS export names for ESM import in node:
65
75
  0 && (module.exports = {
66
76
  looseGetObjectProperty,
@@ -68,5 +78,6 @@ function looseGetObjectProperty(obj, key) {
68
78
  mapObjectToObject,
69
79
  objectTypedEntries,
70
80
  omit,
71
- pick
81
+ pick,
82
+ rejectObjUndefinedValues
72
83
  });
@@ -4,5 +4,6 @@ declare function mapArrayToObject<T, K extends string, O>(array: T[], mapper: (i
4
4
  declare function mapObjectToObject<I extends Record<string | number | symbol, unknown>, K extends string | number | symbol, O>(obj: I, mapper: (key: keyof I, value: I[keyof I]) => [K, O]): Record<K, O>;
5
5
  declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
6
6
  declare function looseGetObjectProperty<T extends Record<string, unknown>>(obj: T, key: string): T[keyof T] | undefined;
7
+ declare function rejectObjUndefinedValues<T extends Record<string, unknown>>(obj: T): T;
7
8
 
8
- export { looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
9
+ export { looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues };
package/lib/objUtils.d.ts CHANGED
@@ -4,5 +4,6 @@ declare function mapArrayToObject<T, K extends string, O>(array: T[], mapper: (i
4
4
  declare function mapObjectToObject<I extends Record<string | number | symbol, unknown>, K extends string | number | symbol, O>(obj: I, mapper: (key: keyof I, value: I[keyof I]) => [K, O]): Record<K, O>;
5
5
  declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
6
6
  declare function looseGetObjectProperty<T extends Record<string, unknown>>(obj: T, key: string): T[keyof T] | undefined;
7
+ declare function rejectObjUndefinedValues<T extends Record<string, unknown>>(obj: T): T;
7
8
 
8
- export { looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick };
9
+ export { looseGetObjectProperty, mapArrayToObject, mapObjectToObject, objectTypedEntries, omit, pick, rejectObjUndefinedValues };
package/lib/objUtils.js CHANGED
@@ -4,13 +4,15 @@ import {
4
4
  mapObjectToObject,
5
5
  objectTypedEntries,
6
6
  omit,
7
- pick
8
- } from "./chunk-ZE3DLPMN.js";
7
+ pick,
8
+ rejectObjUndefinedValues
9
+ } from "./chunk-MWND73GM.js";
9
10
  export {
10
11
  looseGetObjectProperty,
11
12
  mapArrayToObject,
12
13
  mapObjectToObject,
13
14
  objectTypedEntries,
14
15
  omit,
15
- pick
16
+ pick,
17
+ rejectObjUndefinedValues
16
18
  };
@@ -23,6 +23,7 @@ __export(parallelAsyncCalls_exports, {
23
23
  parallelAsyncCalls: () => parallelAsyncCalls
24
24
  });
25
25
  module.exports = __toCommonJS(parallelAsyncCalls_exports);
26
+ var import_t_result = require("t-result");
26
27
 
27
28
  // src/assertions.ts
28
29
  function invariant(condition, errorMsg = "Invariant violation") {
@@ -39,155 +40,6 @@ function sleep(ms) {
39
40
  return new Promise((resolve) => setTimeout(resolve, ms));
40
41
  }
41
42
 
42
- // src/safeJson.ts
43
- function safeJsonStringify(value) {
44
- try {
45
- return JSON.stringify(value);
46
- } catch (_) {
47
- return void 0;
48
- }
49
- }
50
-
51
- // src/tsResult.ts
52
- function okUnwrapOr() {
53
- return this.value;
54
- }
55
- function okMap(mapFn) {
56
- return this.ok ? ok(mapFn(this.value)) : this;
57
- }
58
- function errMap(mapFn) {
59
- return this.ok ? this : err(mapFn(this.error));
60
- }
61
- function mapOkAndErr({
62
- ok: mapFn,
63
- err: mapErrFn
64
- }) {
65
- return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
66
- }
67
- function returnResult() {
68
- return this;
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
- }
82
- function ok(value = void 0) {
83
- const methods = {
84
- unwrapOrNull: okUnwrapOr,
85
- unwrapOr: okUnwrapOr,
86
- unwrap: okUnwrapOr,
87
- mapOk: okMap,
88
- mapErr: returnResult,
89
- mapOkAndErr,
90
- ifOk: okOnOk,
91
- ifErr: returnResult
92
- };
93
- return {
94
- ok: true,
95
- error: false,
96
- value,
97
- ...methods
98
- };
99
- }
100
- function err(error) {
101
- const methods = {
102
- unwrapOrNull: () => null,
103
- unwrapOr: (defaultValue) => defaultValue,
104
- unwrap: () => {
105
- if (error instanceof Error) {
106
- throw error;
107
- }
108
- throw unknownToError(error);
109
- },
110
- mapOk: returnResult,
111
- mapErr: errMap,
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
123
- };
124
- }
125
- function unknownToResultError(error) {
126
- return err(unknownToError(error));
127
- }
128
- async function asyncUnwrap(result) {
129
- const unwrapped = await result;
130
- if (unwrapped.ok) {
131
- return unwrapped.value;
132
- }
133
- if (unwrapped.error instanceof Error) {
134
- throw unwrapped.error;
135
- }
136
- throw unknownToError(unwrapped.error);
137
- }
138
- function asyncMap(resultPromise) {
139
- return {
140
- err: async (mapFn) => {
141
- const result = await resultPromise;
142
- return result.ok ? ok(result.value) : err(mapFn(result.error));
143
- },
144
- ok: async (mapFn) => {
145
- const result = await resultPromise;
146
- return result.ok ? ok(mapFn(result.value)) : err(result.error);
147
- },
148
- okAndErr: async ({
149
- ok: mapFn,
150
- err: mapErrFn
151
- }) => {
152
- const result = await resultPromise;
153
- return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
154
- }
155
- };
156
- }
157
- var Result = {
158
- ok,
159
- err,
160
- unknownToError: unknownToResultError,
161
- asyncUnwrap,
162
- asyncMap,
163
- getOkErr
164
- };
165
- function unknownToError(error) {
166
- if (error instanceof Error) return error;
167
- if (typeof error === "string") {
168
- return new Error(error);
169
- }
170
- if (isObject(error)) {
171
- return new Error(
172
- "message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
173
- { cause: error }
174
- );
175
- }
176
- return new Error(safeJsonStringify(error) ?? "unknown", {
177
- cause: error
178
- });
179
- }
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;
189
- }
190
-
191
43
  // src/parallelAsyncCalls.ts
192
44
  var ParallelAsyncResultCalls = class {
193
45
  pendingCalls = [];
@@ -224,7 +76,7 @@ var ParallelAsyncResultCalls = class {
224
76
  return { result, callMetadata: call.metadata };
225
77
  } catch (exception) {
226
78
  return {
227
- result: Result.unknownToError(exception),
79
+ result: import_t_result.Result.unknownToError(exception),
228
80
  callMetadata: call.metadata
229
81
  };
230
82
  }
@@ -278,14 +130,14 @@ var ParallelAsyncResultCalls = class {
278
130
  } catch (exception) {
279
131
  throw {
280
132
  metadata: call.metadata,
281
- error: unknownToError(exception)
133
+ error: (0, import_t_result.unknownToError)(exception)
282
134
  };
283
135
  }
284
136
  })
285
137
  );
286
- return Result.ok(asyncResults);
138
+ return import_t_result.Result.ok(asyncResults);
287
139
  } catch (exception) {
288
- return Result.err(
140
+ return import_t_result.Result.err(
289
141
  exception
290
142
  );
291
143
  } finally {
@@ -1,4 +1,4 @@
1
- import { Result } from './tsResult.cjs';
1
+ import { Result } from 't-result';
2
2
 
3
3
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
4
4
  type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
@@ -1,4 +1,4 @@
1
- import { Result } from './tsResult.js';
1
+ import { Result } from 't-result';
2
2
 
3
3
  type ValidMetadata = string | number | boolean | Record<string, unknown>;
4
4
  type TupleRunAllSuccess<T> = T extends () => Promise<Result<infer V>> ? Succeeded<V, undefined> : T extends {
@@ -1,17 +1,13 @@
1
- import {
2
- Result,
3
- unknownToError
4
- } from "./chunk-J73KJABC.js";
5
1
  import {
6
2
  sleep
7
3
  } from "./chunk-5DZT3Z5Z.js";
8
- import "./chunk-VAAMRG4K.js";
9
4
  import {
10
5
  invariant,
11
6
  isObject
12
7
  } from "./chunk-3XCS7FVO.js";
13
8
 
14
9
  // src/parallelAsyncCalls.ts
10
+ import { Result, unknownToError } from "t-result";
15
11
  var ParallelAsyncResultCalls = class {
16
12
  pendingCalls = [];
17
13
  alreadyRun = false;
package/lib/testUtils.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  omit,
3
3
  pick
4
- } from "./chunk-ZE3DLPMN.js";
4
+ } from "./chunk-MWND73GM.js";
5
5
  import {
6
6
  deepEqual
7
7
  } from "./chunk-JQFUKJU5.js";
8
8
  import {
9
9
  arrayWithPrevAndIndex,
10
10
  filterAndMap
11
- } from "./chunk-QMFZE2VO.js";
11
+ } from "./chunk-5JQGMNNY.js";
12
12
  import {
13
13
  clampMin
14
14
  } from "./chunk-HTCYUMDR.js";
@@ -4,30 +4,41 @@ type Ok<T> = {
4
4
  value: T;
5
5
  } & AnyResultMethods;
6
6
  type AnyResultMethods = Record<ResultMethodsKeys, never>;
7
+ /** @deprecated Use `t-result` library instead. */
7
8
  type ResultValidErrors = Error | Record<string, unknown> | unknown[] | readonly unknown[] | true;
8
9
  type Err<E extends ResultValidErrors> = {
9
10
  ok: false;
10
11
  error: E;
11
- errorResult: () => Result<any, E>;
12
+ /** @deprecated Use `t-result` library instead. */
13
+ errorResult: () => Err<E>;
12
14
  } & AnyResultMethods;
13
15
  type ResultMethods<T, E extends ResultValidErrors> = {
14
- /** Returns the value if the result is Ok, otherwise returns null */
16
+ /** @deprecated Use `t-result` library instead. */
15
17
  unwrapOrNull: () => T | null;
16
- /** Returns the value if the result is Ok, otherwise returns the provided default value */
18
+ /** @deprecated Use `t-result` library instead. */
17
19
  unwrapOr: <R extends T>(defaultValue: R) => T | R;
20
+ /** @deprecated Use `t-result` library instead. */
18
21
  unwrap: () => T;
22
+ /** @deprecated Use `t-result` library instead. */
19
23
  mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
24
+ /** @deprecated Use `t-result` library instead. */
20
25
  mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
26
+ /** @deprecated Use `t-result` library instead. */
21
27
  mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
22
28
  ok: (value: T) => NewValue;
23
29
  err: (error: E) => NewError;
24
30
  }) => Result<NewValue, NewError>;
31
+ /** @deprecated Use `t-result` library instead. */
25
32
  ifOk: (fn: (value: T) => void) => Result<T, E>;
33
+ /** @deprecated Use `t-result` library instead. */
26
34
  ifErr: (fn: (error: E) => void) => Result<T, E>;
27
35
  };
28
36
  type ResultMethodsKeys = keyof ResultMethods<any, any>;
37
+ /** @deprecated Use `t-result` library instead. */
29
38
  declare function ok(): Ok<void>;
39
+ /** @deprecated Use `t-result` library instead. */
30
40
  declare function ok<T>(value: T): Ok<T>;
41
+ /** @deprecated Use `t-result` library instead. */
31
42
  declare function err<E extends ResultValidErrors>(error: E): Err<E>;
32
43
  declare function unknownToResultError(error: unknown): Err<Error>;
33
44
  /** Unwraps a promise result */
@@ -40,29 +51,9 @@ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise
40
51
  err: (error: E) => NewError;
41
52
  }) => Promise<Result<NewValue, NewError>>;
42
53
  };
43
- /**
44
- * Util for implementing something similar to Result<T, E> in Rust, for better error handling.
45
- *
46
- * Usage:
47
- *
48
- * @example
49
- * function doSomething(): Result<string, Error> {
50
- * if (something) {
51
- * return ok('success');
52
- * }
53
- *
54
- * return err(new Error('something went wrong'));
55
- * }
56
- *
57
- * const result = doSomething();
58
- *
59
- * if (result.ok) {
60
- * // result.value is a string
61
- * } else {
62
- * // result.error is an Error
63
- * }
64
- */
54
+ /** @deprecated Use `t-result` library instead. */
65
55
  type Result<T, E extends ResultValidErrors = Error> = (Omit<Ok<T>, ResultMethodsKeys> | Omit<Err<E>, ResultMethodsKeys>) & ResultMethods<T, E>;
56
+ /** @deprecated Use `t-result` library instead. */
66
57
  declare const Result: {
67
58
  ok: typeof ok;
68
59
  err: typeof err;
@@ -71,46 +62,24 @@ declare const Result: {
71
62
  asyncMap: typeof asyncMap;
72
63
  getOkErr: typeof getOkErr;
73
64
  };
65
+ /** @deprecated Use `t-result` library instead. */
74
66
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T extends Promise<any> ? never : T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
67
+ /** @deprecated Use `t-result` library instead. */
75
68
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
69
+ /** @deprecated Use `t-result` library instead. */
76
70
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<T, E>>;
77
- /**
78
- * Converts an unknown error value into an Error object.
79
- *
80
- * @param error - The unknown value to convert to an Error
81
- * @returns An Error object
82
- *
83
- * @example
84
- * try {
85
- * // some code that might throw
86
- * } catch (err) {
87
- * const error = unknownToError(err); // Guaranteed to be Error instance
88
- * }
89
- *
90
- * The function handles different error types:
91
- * - Returns the error directly if it's already an Error instance
92
- * - Converts strings into Error objects using the string as the message
93
- * - For objects, tries to extract a message property or stringifies the object
94
- * - For other values, stringifies them or uses 'unknown' as fallback
95
- *
96
- * The original error value is preserved as the `cause` property of the returned Error.
97
- */
71
+ /** @deprecated Use `t-result` library instead. */
98
72
  declare function unknownToError(error: unknown): Error;
73
+ /** @deprecated Use `t-result` library instead. */
99
74
  type TypedResult<T, E extends ResultValidErrors = Error> = {
75
+ /** @deprecated Use `t-result` library instead. */
100
76
  ok: (value: T) => Ok<T>;
77
+ /** @deprecated Use `t-result` library instead. */
101
78
  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
- */
79
+ /** @deprecated Use `t-result` library instead. */
112
80
  _type: Result<T, E>;
113
81
  };
82
+ /** @deprecated Use `t-result` library instead. */
114
83
  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
84
  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
85
  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>;
package/lib/tsResult.d.ts CHANGED
@@ -4,30 +4,41 @@ type Ok<T> = {
4
4
  value: T;
5
5
  } & AnyResultMethods;
6
6
  type AnyResultMethods = Record<ResultMethodsKeys, never>;
7
+ /** @deprecated Use `t-result` library instead. */
7
8
  type ResultValidErrors = Error | Record<string, unknown> | unknown[] | readonly unknown[] | true;
8
9
  type Err<E extends ResultValidErrors> = {
9
10
  ok: false;
10
11
  error: E;
11
- errorResult: () => Result<any, E>;
12
+ /** @deprecated Use `t-result` library instead. */
13
+ errorResult: () => Err<E>;
12
14
  } & AnyResultMethods;
13
15
  type ResultMethods<T, E extends ResultValidErrors> = {
14
- /** Returns the value if the result is Ok, otherwise returns null */
16
+ /** @deprecated Use `t-result` library instead. */
15
17
  unwrapOrNull: () => T | null;
16
- /** Returns the value if the result is Ok, otherwise returns the provided default value */
18
+ /** @deprecated Use `t-result` library instead. */
17
19
  unwrapOr: <R extends T>(defaultValue: R) => T | R;
20
+ /** @deprecated Use `t-result` library instead. */
18
21
  unwrap: () => T;
22
+ /** @deprecated Use `t-result` library instead. */
19
23
  mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
24
+ /** @deprecated Use `t-result` library instead. */
20
25
  mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
26
+ /** @deprecated Use `t-result` library instead. */
21
27
  mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
22
28
  ok: (value: T) => NewValue;
23
29
  err: (error: E) => NewError;
24
30
  }) => Result<NewValue, NewError>;
31
+ /** @deprecated Use `t-result` library instead. */
25
32
  ifOk: (fn: (value: T) => void) => Result<T, E>;
33
+ /** @deprecated Use `t-result` library instead. */
26
34
  ifErr: (fn: (error: E) => void) => Result<T, E>;
27
35
  };
28
36
  type ResultMethodsKeys = keyof ResultMethods<any, any>;
37
+ /** @deprecated Use `t-result` library instead. */
29
38
  declare function ok(): Ok<void>;
39
+ /** @deprecated Use `t-result` library instead. */
30
40
  declare function ok<T>(value: T): Ok<T>;
41
+ /** @deprecated Use `t-result` library instead. */
31
42
  declare function err<E extends ResultValidErrors>(error: E): Err<E>;
32
43
  declare function unknownToResultError(error: unknown): Err<Error>;
33
44
  /** Unwraps a promise result */
@@ -40,29 +51,9 @@ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise
40
51
  err: (error: E) => NewError;
41
52
  }) => Promise<Result<NewValue, NewError>>;
42
53
  };
43
- /**
44
- * Util for implementing something similar to Result<T, E> in Rust, for better error handling.
45
- *
46
- * Usage:
47
- *
48
- * @example
49
- * function doSomething(): Result<string, Error> {
50
- * if (something) {
51
- * return ok('success');
52
- * }
53
- *
54
- * return err(new Error('something went wrong'));
55
- * }
56
- *
57
- * const result = doSomething();
58
- *
59
- * if (result.ok) {
60
- * // result.value is a string
61
- * } else {
62
- * // result.error is an Error
63
- * }
64
- */
54
+ /** @deprecated Use `t-result` library instead. */
65
55
  type Result<T, E extends ResultValidErrors = Error> = (Omit<Ok<T>, ResultMethodsKeys> | Omit<Err<E>, ResultMethodsKeys>) & ResultMethods<T, E>;
56
+ /** @deprecated Use `t-result` library instead. */
66
57
  declare const Result: {
67
58
  ok: typeof ok;
68
59
  err: typeof err;
@@ -71,46 +62,24 @@ declare const Result: {
71
62
  asyncMap: typeof asyncMap;
72
63
  getOkErr: typeof getOkErr;
73
64
  };
65
+ /** @deprecated Use `t-result` library instead. */
74
66
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T extends Promise<any> ? never : T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
67
+ /** @deprecated Use `t-result` library instead. */
75
68
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
69
+ /** @deprecated Use `t-result` library instead. */
76
70
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<T, E>>;
77
- /**
78
- * Converts an unknown error value into an Error object.
79
- *
80
- * @param error - The unknown value to convert to an Error
81
- * @returns An Error object
82
- *
83
- * @example
84
- * try {
85
- * // some code that might throw
86
- * } catch (err) {
87
- * const error = unknownToError(err); // Guaranteed to be Error instance
88
- * }
89
- *
90
- * The function handles different error types:
91
- * - Returns the error directly if it's already an Error instance
92
- * - Converts strings into Error objects using the string as the message
93
- * - For objects, tries to extract a message property or stringifies the object
94
- * - For other values, stringifies them or uses 'unknown' as fallback
95
- *
96
- * The original error value is preserved as the `cause` property of the returned Error.
97
- */
71
+ /** @deprecated Use `t-result` library instead. */
98
72
  declare function unknownToError(error: unknown): Error;
73
+ /** @deprecated Use `t-result` library instead. */
99
74
  type TypedResult<T, E extends ResultValidErrors = Error> = {
75
+ /** @deprecated Use `t-result` library instead. */
100
76
  ok: (value: T) => Ok<T>;
77
+ /** @deprecated Use `t-result` library instead. */
101
78
  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
- */
79
+ /** @deprecated Use `t-result` library instead. */
112
80
  _type: Result<T, E>;
113
81
  };
82
+ /** @deprecated Use `t-result` library instead. */
114
83
  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
84
  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
85
  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>;
package/lib/tsResult.js CHANGED
@@ -1,12 +1,175 @@
1
1
  import {
2
- Result,
2
+ safeJsonStringify
3
+ } from "./chunk-VAAMRG4K.js";
4
+ import {
5
+ isFunction,
6
+ isObject,
7
+ isPromise
8
+ } from "./chunk-3XCS7FVO.js";
9
+
10
+ // src/tsResult.ts
11
+ function okUnwrapOr() {
12
+ return this.value;
13
+ }
14
+ function okMap(mapFn) {
15
+ return this.ok ? ok(mapFn(this.value)) : this;
16
+ }
17
+ function errMap(mapFn) {
18
+ return this.ok ? this : err(mapFn(this.error));
19
+ }
20
+ function mapOkAndErr({
21
+ ok: mapFn,
22
+ err: mapErrFn
23
+ }) {
24
+ return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
25
+ }
26
+ function returnResult() {
27
+ return this;
28
+ }
29
+ function okOnOk(fn) {
30
+ if (this.ok) {
31
+ fn(this.value);
32
+ }
33
+ return this;
34
+ }
35
+ function errOnErr(fn) {
36
+ if (!this.ok) {
37
+ fn(this.error);
38
+ }
39
+ return this;
40
+ }
41
+ function ok(value = void 0) {
42
+ const methods = {
43
+ unwrapOrNull: okUnwrapOr,
44
+ unwrapOr: okUnwrapOr,
45
+ unwrap: okUnwrapOr,
46
+ mapOk: okMap,
47
+ mapErr: returnResult,
48
+ mapOkAndErr,
49
+ ifOk: okOnOk,
50
+ ifErr: returnResult
51
+ };
52
+ return {
53
+ ok: true,
54
+ error: false,
55
+ value,
56
+ ...methods
57
+ };
58
+ }
59
+ function err(error) {
60
+ const methods = {
61
+ unwrapOrNull: () => null,
62
+ unwrapOr: (defaultValue) => defaultValue,
63
+ unwrap: () => {
64
+ if (error instanceof Error) {
65
+ throw error;
66
+ }
67
+ throw unknownToError(error);
68
+ },
69
+ mapOk: returnResult,
70
+ mapErr: errMap,
71
+ mapOkAndErr,
72
+ ifOk: returnResult,
73
+ ifErr: errOnErr
74
+ };
75
+ return {
76
+ ok: false,
77
+ error,
78
+ errorResult() {
79
+ return err(error);
80
+ },
81
+ ...methods
82
+ };
83
+ }
84
+ function unknownToResultError(error) {
85
+ return err(unknownToError(error));
86
+ }
87
+ async function asyncUnwrap(result) {
88
+ const unwrapped = await result;
89
+ if (unwrapped.ok) {
90
+ return unwrapped.value;
91
+ }
92
+ if (unwrapped.error instanceof Error) {
93
+ throw unwrapped.error;
94
+ }
95
+ throw unknownToError(unwrapped.error);
96
+ }
97
+ function asyncMap(resultPromise) {
98
+ return {
99
+ err: async (mapFn) => {
100
+ const result = await resultPromise;
101
+ return result.ok ? ok(result.value) : err(mapFn(result.error));
102
+ },
103
+ ok: async (mapFn) => {
104
+ const result = await resultPromise;
105
+ return result.ok ? ok(mapFn(result.value)) : err(result.error);
106
+ },
107
+ okAndErr: async ({
108
+ ok: mapFn,
109
+ err: mapErrFn
110
+ }) => {
111
+ const result = await resultPromise;
112
+ return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
113
+ }
114
+ };
115
+ }
116
+ var Result = {
117
+ ok,
3
118
  err,
119
+ unknownToError: unknownToResultError,
120
+ asyncUnwrap,
121
+ asyncMap,
122
+ getOkErr
123
+ };
124
+ function resultify(fn, errorNormalizer) {
125
+ if (!isFunction(fn)) {
126
+ return fn.then((value) => ok(value)).catch(
127
+ (error) => err(
128
+ errorNormalizer ? errorNormalizer(error) : unknownToError(error)
129
+ )
130
+ );
131
+ }
132
+ try {
133
+ const result = fn();
134
+ if (isPromise(result)) {
135
+ return result.then((value) => ok(value)).catch(
136
+ (error) => err(
137
+ errorNormalizer ? errorNormalizer(error) : unknownToError(error)
138
+ )
139
+ );
140
+ }
141
+ return ok(result);
142
+ } catch (error) {
143
+ return err(
144
+ errorNormalizer ? errorNormalizer(error) : unknownToError(error)
145
+ );
146
+ }
147
+ }
148
+ function unknownToError(error) {
149
+ if (error instanceof Error) return error;
150
+ if (typeof error === "string") {
151
+ return new Error(error);
152
+ }
153
+ if (isObject(error)) {
154
+ return new Error(
155
+ "message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
156
+ { cause: error }
157
+ );
158
+ }
159
+ return new Error(safeJsonStringify(error) ?? "unknown", {
160
+ cause: error
161
+ });
162
+ }
163
+ var typedResult = {
4
164
  ok,
5
- resultify,
6
- unknownToError
7
- } from "./chunk-J73KJABC.js";
8
- import "./chunk-VAAMRG4K.js";
9
- import "./chunk-3XCS7FVO.js";
165
+ err,
166
+ get _type() {
167
+ throw new Error("usage as value is not allowed");
168
+ }
169
+ };
170
+ function getOkErr() {
171
+ return typedResult;
172
+ }
10
173
  export {
11
174
  Result,
12
175
  err,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "3.3.2",
4
+ "version": "3.5.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib"
@@ -176,6 +176,9 @@
176
176
  "vite": "^6.0.11",
177
177
  "vitest": "^3.0.4"
178
178
  },
179
+ "dependencies": {
180
+ "t-result": "^0.2.1"
181
+ },
179
182
  "scripts": {
180
183
  "test:ui": "vitest --ui",
181
184
  "test": "vitest run",
@@ -1,180 +0,0 @@
1
- import {
2
- safeJsonStringify
3
- } from "./chunk-VAAMRG4K.js";
4
- import {
5
- isFunction,
6
- isObject,
7
- isPromise
8
- } from "./chunk-3XCS7FVO.js";
9
-
10
- // src/tsResult.ts
11
- function okUnwrapOr() {
12
- return this.value;
13
- }
14
- function okMap(mapFn) {
15
- return this.ok ? ok(mapFn(this.value)) : this;
16
- }
17
- function errMap(mapFn) {
18
- return this.ok ? this : err(mapFn(this.error));
19
- }
20
- function mapOkAndErr({
21
- ok: mapFn,
22
- err: mapErrFn
23
- }) {
24
- return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
25
- }
26
- function returnResult() {
27
- return this;
28
- }
29
- function okOnOk(fn) {
30
- if (this.ok) {
31
- fn(this.value);
32
- }
33
- return this;
34
- }
35
- function errOnErr(fn) {
36
- if (!this.ok) {
37
- fn(this.error);
38
- }
39
- return this;
40
- }
41
- function ok(value = void 0) {
42
- const methods = {
43
- unwrapOrNull: okUnwrapOr,
44
- unwrapOr: okUnwrapOr,
45
- unwrap: okUnwrapOr,
46
- mapOk: okMap,
47
- mapErr: returnResult,
48
- mapOkAndErr,
49
- ifOk: okOnOk,
50
- ifErr: returnResult
51
- };
52
- return {
53
- ok: true,
54
- error: false,
55
- value,
56
- ...methods
57
- };
58
- }
59
- function err(error) {
60
- const methods = {
61
- unwrapOrNull: () => null,
62
- unwrapOr: (defaultValue) => defaultValue,
63
- unwrap: () => {
64
- if (error instanceof Error) {
65
- throw error;
66
- }
67
- throw unknownToError(error);
68
- },
69
- mapOk: returnResult,
70
- mapErr: errMap,
71
- mapOkAndErr,
72
- ifOk: returnResult,
73
- ifErr: errOnErr
74
- };
75
- return {
76
- ok: false,
77
- error,
78
- errorResult() {
79
- return err(error);
80
- },
81
- ...methods
82
- };
83
- }
84
- function unknownToResultError(error) {
85
- return err(unknownToError(error));
86
- }
87
- async function asyncUnwrap(result) {
88
- const unwrapped = await result;
89
- if (unwrapped.ok) {
90
- return unwrapped.value;
91
- }
92
- if (unwrapped.error instanceof Error) {
93
- throw unwrapped.error;
94
- }
95
- throw unknownToError(unwrapped.error);
96
- }
97
- function asyncMap(resultPromise) {
98
- return {
99
- err: async (mapFn) => {
100
- const result = await resultPromise;
101
- return result.ok ? ok(result.value) : err(mapFn(result.error));
102
- },
103
- ok: async (mapFn) => {
104
- const result = await resultPromise;
105
- return result.ok ? ok(mapFn(result.value)) : err(result.error);
106
- },
107
- okAndErr: async ({
108
- ok: mapFn,
109
- err: mapErrFn
110
- }) => {
111
- const result = await resultPromise;
112
- return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
113
- }
114
- };
115
- }
116
- var Result = {
117
- ok,
118
- err,
119
- unknownToError: unknownToResultError,
120
- asyncUnwrap,
121
- asyncMap,
122
- getOkErr
123
- };
124
- function resultify(fn, errorNormalizer) {
125
- if (!isFunction(fn)) {
126
- return fn.then((value) => ok(value)).catch(
127
- (error) => err(
128
- errorNormalizer ? errorNormalizer(error) : unknownToError(error)
129
- )
130
- );
131
- }
132
- try {
133
- const result = fn();
134
- if (isPromise(result)) {
135
- return result.then((value) => ok(value)).catch(
136
- (error) => err(
137
- errorNormalizer ? errorNormalizer(error) : unknownToError(error)
138
- )
139
- );
140
- }
141
- return ok(result);
142
- } catch (error) {
143
- return err(
144
- errorNormalizer ? errorNormalizer(error) : unknownToError(error)
145
- );
146
- }
147
- }
148
- function unknownToError(error) {
149
- if (error instanceof Error) return error;
150
- if (typeof error === "string") {
151
- return new Error(error);
152
- }
153
- if (isObject(error)) {
154
- return new Error(
155
- "message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
156
- { cause: error }
157
- );
158
- }
159
- return new Error(safeJsonStringify(error) ?? "unknown", {
160
- cause: error
161
- });
162
- }
163
- var typedResult = {
164
- ok,
165
- err,
166
- get _type() {
167
- throw new Error("usage as value is not allowed");
168
- }
169
- };
170
- function getOkErr() {
171
- return typedResult;
172
- }
173
-
174
- export {
175
- ok,
176
- err,
177
- Result,
178
- resultify,
179
- unknownToError
180
- };