@ls-stack/utils 2.0.0 → 2.1.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.
@@ -23,6 +23,8 @@ __export(arrayUtils_exports, {
23
23
  arrayWithPrev: () => arrayWithPrev,
24
24
  arrayWithPrevAndIndex: () => arrayWithPrevAndIndex,
25
25
  filterAndMap: () => filterAndMap,
26
+ findAfterIndex: () => findAfterIndex,
27
+ findBeforeIndex: () => findBeforeIndex,
26
28
  isInArray: () => isInArray,
27
29
  sortBy: () => sortBy
28
30
  });
@@ -81,11 +83,33 @@ function isInArray(value, oneOf) {
81
83
  }
82
84
  return false;
83
85
  }
86
+ function findAfterIndex(array, index, predicate) {
87
+ for (let i = index + 1; i < array.length; i++) {
88
+ if (predicate(array[i])) {
89
+ return array[i];
90
+ }
91
+ }
92
+ return void 0;
93
+ }
94
+ function findBeforeIndex(array, index, predicate) {
95
+ let indexToUse = index;
96
+ if (indexToUse >= array.length) {
97
+ indexToUse = array.length;
98
+ }
99
+ for (let i = indexToUse - 1; i >= 0; i--) {
100
+ if (predicate(array[i])) {
101
+ return array[i];
102
+ }
103
+ }
104
+ return void 0;
105
+ }
84
106
  // Annotate the CommonJS export names for ESM import in node:
85
107
  0 && (module.exports = {
86
108
  arrayWithPrev,
87
109
  arrayWithPrevAndIndex,
88
110
  filterAndMap,
111
+ findAfterIndex,
112
+ findBeforeIndex,
89
113
  isInArray,
90
114
  sortBy
91
115
  });
@@ -47,5 +47,7 @@ declare function arrayWithPrevAndIndex<T>(array: T[]): {
47
47
  index: number;
48
48
  }[];
49
49
  declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]): value is U;
50
+ declare function findAfterIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
51
+ declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
50
52
 
51
- export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, isInArray, sortBy };
53
+ export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, sortBy };
@@ -47,5 +47,7 @@ declare function arrayWithPrevAndIndex<T>(array: T[]): {
47
47
  index: number;
48
48
  }[];
49
49
  declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]): value is U;
50
+ declare function findAfterIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
51
+ declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
50
52
 
51
- export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, isInArray, sortBy };
53
+ export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, sortBy };
@@ -2,13 +2,17 @@ import {
2
2
  arrayWithPrev,
3
3
  arrayWithPrevAndIndex,
4
4
  filterAndMap,
5
+ findAfterIndex,
6
+ findBeforeIndex,
5
7
  isInArray,
6
8
  sortBy
7
- } from "./chunk-AZBBTE33.js";
9
+ } from "./chunk-QMFZE2VO.js";
8
10
  export {
9
11
  arrayWithPrev,
10
12
  arrayWithPrevAndIndex,
11
13
  filterAndMap,
14
+ findAfterIndex,
15
+ findBeforeIndex,
12
16
  isInArray,
13
17
  sortBy
14
18
  };
@@ -6,6 +6,21 @@ import {
6
6
  function okUnwrapOr() {
7
7
  return this.value;
8
8
  }
9
+ function okMap(mapFn) {
10
+ return this.ok ? ok(mapFn(this.value)) : this;
11
+ }
12
+ function errMap(mapFn) {
13
+ return this.ok ? this : err(mapFn(this.error));
14
+ }
15
+ function mapOkAndErr({
16
+ ok: mapFn,
17
+ err: mapErrFn
18
+ }) {
19
+ return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
20
+ }
21
+ function returnResult() {
22
+ return this;
23
+ }
9
24
  function ok(value = void 0) {
10
25
  return {
11
26
  ok: true,
@@ -13,7 +28,10 @@ function ok(value = void 0) {
13
28
  value,
14
29
  unwrapOrNull: okUnwrapOr,
15
30
  unwrapOr: okUnwrapOr,
16
- unwrap: okUnwrapOr
31
+ unwrap: okUnwrapOr,
32
+ mapOk: okMap,
33
+ mapErr: returnResult,
34
+ mapOkAndErr
17
35
  };
18
36
  }
19
37
  function err(error) {
@@ -29,25 +47,25 @@ function err(error) {
29
47
  if (error instanceof Error) {
30
48
  throw error;
31
49
  }
32
- if (Array.isArray(error)) {
33
- throw new Error(error.join("\n"), { cause: error });
34
- }
35
- throw new Error(
36
- isObject(error) && "message" in error && error.message ? String(error.message) : "Unwrap failed",
37
- { cause: error }
38
- );
39
- }
50
+ throw normalizeError(error);
51
+ },
52
+ mapOk: returnResult,
53
+ mapErr: errMap,
54
+ mapOkAndErr
40
55
  };
41
56
  }
42
57
  function unknownToError(error) {
43
58
  return err(normalizeError(error));
44
59
  }
45
- async function unwrap(result) {
60
+ async function asyncUnwrap(result) {
46
61
  const unwrapped = await result;
47
62
  if (unwrapped.ok) {
48
63
  return unwrapped.value;
49
64
  }
50
- throw unwrapped.error;
65
+ if (unwrapped.error instanceof Error) {
66
+ throw unwrapped.error;
67
+ }
68
+ throw normalizeError(unwrapped.error);
51
69
  }
52
70
  function asyncMap(resultPromise) {
53
71
  return {
@@ -68,29 +86,12 @@ function asyncMap(resultPromise) {
68
86
  }
69
87
  };
70
88
  }
71
- function syncMap(result) {
72
- return {
73
- err: (mapFn) => {
74
- return result.ok ? ok(result.value) : err(mapFn(result.error));
75
- },
76
- ok: (mapFn) => {
77
- return result.ok ? ok(mapFn(result.value)) : err(result.error);
78
- },
79
- okAndErr: ({
80
- ok: mapFn,
81
- err: mapErrFn
82
- }) => {
83
- return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
84
- }
85
- };
86
- }
87
89
  var Result = {
88
90
  ok,
89
91
  err,
90
92
  unknownToError,
91
- unwrap,
92
- asyncMap,
93
- map: syncMap
93
+ asyncUnwrap,
94
+ asyncMap
94
95
  };
95
96
  function resultify(fn, errorNormalizer) {
96
97
  try {
@@ -117,7 +118,7 @@ function normalizeError(error) {
117
118
  }
118
119
  if (isObject(error)) {
119
120
  return new Error(
120
- "message" in error && error.message ? String(error.message) : safeJsonStringify(error) ?? "unknown",
121
+ "message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
121
122
  { cause: error }
122
123
  );
123
124
  }
@@ -1,19 +1,21 @@
1
1
  // src/deepEqual.ts
2
2
  var has = Object.prototype.hasOwnProperty;
3
- function find(iter, tar, key) {
4
- for (key of iter.keys()) {
5
- if (deepEqual(key, tar)) return key;
3
+ function find(iter, tar, maxDepth) {
4
+ for (const key of iter.keys()) {
5
+ if (deepEqual(key, tar, maxDepth)) return key;
6
6
  }
7
7
  }
8
- function deepEqual(foo, bar) {
8
+ function deepEqual(foo, bar, maxDepth = 20) {
9
9
  let ctor, len, tmp;
10
10
  if (foo === bar) return true;
11
+ if (maxDepth && maxDepth <= 0) return false;
11
12
  if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
12
- if (ctor === Date) return deepEqual(foo.getTime(), bar.getTime());
13
+ if (ctor === Date)
14
+ return deepEqual(foo.getTime(), bar.getTime(), maxDepth - 1);
13
15
  if (ctor === RegExp) return foo.toString() === bar.toString();
14
16
  if (ctor === Array) {
15
17
  if ((len = foo.length) === bar.length) {
16
- while (len-- && deepEqual(foo[len], bar[len])) ;
18
+ while (len-- && deepEqual(foo[len], bar[len], maxDepth - 1)) ;
17
19
  }
18
20
  return len === -1;
19
21
  }
@@ -24,7 +26,7 @@ function deepEqual(foo, bar) {
24
26
  for (len of foo) {
25
27
  tmp = len;
26
28
  if (tmp && typeof tmp === "object") {
27
- tmp = find(bar, tmp);
29
+ tmp = find(bar, tmp, maxDepth - 1);
28
30
  if (!tmp) return false;
29
31
  }
30
32
  if (!bar.has(tmp)) return false;
@@ -38,10 +40,10 @@ function deepEqual(foo, bar) {
38
40
  for (len of foo) {
39
41
  tmp = len[0];
40
42
  if (tmp && typeof tmp === "object") {
41
- tmp = find(bar, tmp);
43
+ tmp = find(bar, tmp, maxDepth - 1);
42
44
  if (!tmp) return false;
43
45
  }
44
- if (!deepEqual(len[1], bar.get(tmp))) {
46
+ if (!deepEqual(len[1], bar.get(tmp), maxDepth - 1)) {
45
47
  return false;
46
48
  }
47
49
  }
@@ -51,14 +53,19 @@ function deepEqual(foo, bar) {
51
53
  len = 0;
52
54
  for (ctor in foo) {
53
55
  if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
54
- if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor])) return false;
56
+ if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor], maxDepth - 1))
57
+ return false;
55
58
  }
56
59
  return Object.keys(bar).length === len;
57
60
  }
58
61
  }
59
62
  return foo !== foo && bar !== bar;
60
63
  }
64
+ function deepEqualWithMaxDepth(maxDepth) {
65
+ return (foo, bar) => deepEqual(foo, bar, maxDepth);
66
+ }
61
67
 
62
68
  export {
63
- deepEqual
69
+ deepEqual,
70
+ deepEqualWithMaxDepth
64
71
  };
@@ -53,11 +53,33 @@ function isInArray(value, oneOf) {
53
53
  }
54
54
  return false;
55
55
  }
56
+ function findAfterIndex(array, index, predicate) {
57
+ for (let i = index + 1; i < array.length; i++) {
58
+ if (predicate(array[i])) {
59
+ return array[i];
60
+ }
61
+ }
62
+ return void 0;
63
+ }
64
+ function findBeforeIndex(array, index, predicate) {
65
+ let indexToUse = index;
66
+ if (indexToUse >= array.length) {
67
+ indexToUse = array.length;
68
+ }
69
+ for (let i = indexToUse - 1; i >= 0; i--) {
70
+ if (predicate(array[i])) {
71
+ return array[i];
72
+ }
73
+ }
74
+ return void 0;
75
+ }
56
76
 
57
77
  export {
58
78
  filterAndMap,
59
79
  sortBy,
60
80
  arrayWithPrev,
61
81
  arrayWithPrevAndIndex,
62
- isInArray
82
+ isInArray,
83
+ findAfterIndex,
84
+ findBeforeIndex
63
85
  };
@@ -20,24 +20,27 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/deepEqual.ts
21
21
  var deepEqual_exports = {};
22
22
  __export(deepEqual_exports, {
23
- deepEqual: () => deepEqual
23
+ deepEqual: () => deepEqual,
24
+ deepEqualWithMaxDepth: () => deepEqualWithMaxDepth
24
25
  });
25
26
  module.exports = __toCommonJS(deepEqual_exports);
26
27
  var has = Object.prototype.hasOwnProperty;
27
- function find(iter, tar, key) {
28
- for (key of iter.keys()) {
29
- if (deepEqual(key, tar)) return key;
28
+ function find(iter, tar, maxDepth) {
29
+ for (const key of iter.keys()) {
30
+ if (deepEqual(key, tar, maxDepth)) return key;
30
31
  }
31
32
  }
32
- function deepEqual(foo, bar) {
33
+ function deepEqual(foo, bar, maxDepth = 20) {
33
34
  let ctor, len, tmp;
34
35
  if (foo === bar) return true;
36
+ if (maxDepth && maxDepth <= 0) return false;
35
37
  if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
36
- if (ctor === Date) return deepEqual(foo.getTime(), bar.getTime());
38
+ if (ctor === Date)
39
+ return deepEqual(foo.getTime(), bar.getTime(), maxDepth - 1);
37
40
  if (ctor === RegExp) return foo.toString() === bar.toString();
38
41
  if (ctor === Array) {
39
42
  if ((len = foo.length) === bar.length) {
40
- while (len-- && deepEqual(foo[len], bar[len])) ;
43
+ while (len-- && deepEqual(foo[len], bar[len], maxDepth - 1)) ;
41
44
  }
42
45
  return len === -1;
43
46
  }
@@ -48,7 +51,7 @@ function deepEqual(foo, bar) {
48
51
  for (len of foo) {
49
52
  tmp = len;
50
53
  if (tmp && typeof tmp === "object") {
51
- tmp = find(bar, tmp);
54
+ tmp = find(bar, tmp, maxDepth - 1);
52
55
  if (!tmp) return false;
53
56
  }
54
57
  if (!bar.has(tmp)) return false;
@@ -62,10 +65,10 @@ function deepEqual(foo, bar) {
62
65
  for (len of foo) {
63
66
  tmp = len[0];
64
67
  if (tmp && typeof tmp === "object") {
65
- tmp = find(bar, tmp);
68
+ tmp = find(bar, tmp, maxDepth - 1);
66
69
  if (!tmp) return false;
67
70
  }
68
- if (!deepEqual(len[1], bar.get(tmp))) {
71
+ if (!deepEqual(len[1], bar.get(tmp), maxDepth - 1)) {
69
72
  return false;
70
73
  }
71
74
  }
@@ -75,14 +78,19 @@ function deepEqual(foo, bar) {
75
78
  len = 0;
76
79
  for (ctor in foo) {
77
80
  if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
78
- if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor])) return false;
81
+ if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor], maxDepth - 1))
82
+ return false;
79
83
  }
80
84
  return Object.keys(bar).length === len;
81
85
  }
82
86
  }
83
87
  return foo !== foo && bar !== bar;
84
88
  }
89
+ function deepEqualWithMaxDepth(maxDepth) {
90
+ return (foo, bar) => deepEqual(foo, bar, maxDepth);
91
+ }
85
92
  // Annotate the CommonJS export names for ESM import in node:
86
93
  0 && (module.exports = {
87
- deepEqual
94
+ deepEqual,
95
+ deepEqualWithMaxDepth
88
96
  });
@@ -1,4 +1,20 @@
1
- /** forked from https://github.com/lukeed/dequal to consider invalid dates as equal */
2
- declare function deepEqual(foo: any, bar: any): boolean;
1
+ /**
2
+ * Deep equality comparison between two values
3
+ * @param foo First value to compare
4
+ * @param bar Second value to compare
5
+ * @param maxDepth Maximum comparison depth (default: 20)
6
+ * @returns True if values are deeply equal, false otherwise
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * deepEqual({a: 1}, {a: 1}) // true
11
+ * deepEqual({a: 1}, {a: 2}) // false
12
+ * deepEqual([1, {b: 2}], [1, {b: 2}]) // true
13
+ * deepEqual(new Map([['a', 1]]), new Map([['a', 1]])) // true
14
+ * deepEqual(new Set([1, 2]), new Set([1, 2])) // true
15
+ * ```
16
+ */
17
+ declare function deepEqual(foo: any, bar: any, maxDepth?: number): boolean;
18
+ declare function deepEqualWithMaxDepth(maxDepth: number): (foo: any, bar: any) => boolean;
3
19
 
4
- export { deepEqual };
20
+ export { deepEqual, deepEqualWithMaxDepth };
@@ -1,4 +1,20 @@
1
- /** forked from https://github.com/lukeed/dequal to consider invalid dates as equal */
2
- declare function deepEqual(foo: any, bar: any): boolean;
1
+ /**
2
+ * Deep equality comparison between two values
3
+ * @param foo First value to compare
4
+ * @param bar Second value to compare
5
+ * @param maxDepth Maximum comparison depth (default: 20)
6
+ * @returns True if values are deeply equal, false otherwise
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * deepEqual({a: 1}, {a: 1}) // true
11
+ * deepEqual({a: 1}, {a: 2}) // false
12
+ * deepEqual([1, {b: 2}], [1, {b: 2}]) // true
13
+ * deepEqual(new Map([['a', 1]]), new Map([['a', 1]])) // true
14
+ * deepEqual(new Set([1, 2]), new Set([1, 2])) // true
15
+ * ```
16
+ */
17
+ declare function deepEqual(foo: any, bar: any, maxDepth?: number): boolean;
18
+ declare function deepEqualWithMaxDepth(maxDepth: number): (foo: any, bar: any) => boolean;
3
19
 
4
- export { deepEqual };
20
+ export { deepEqual, deepEqualWithMaxDepth };
package/dist/deepEqual.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import {
2
- deepEqual
3
- } from "./chunk-KCOXGSRA.js";
2
+ deepEqual,
3
+ deepEqualWithMaxDepth
4
+ } from "./chunk-JQFUKJU5.js";
4
5
  export {
5
- deepEqual
6
+ deepEqual,
7
+ deepEqualWithMaxDepth
6
8
  };
@@ -38,6 +38,21 @@ function isObject(value) {
38
38
  function okUnwrapOr() {
39
39
  return this.value;
40
40
  }
41
+ function okMap(mapFn) {
42
+ return this.ok ? ok(mapFn(this.value)) : this;
43
+ }
44
+ function errMap(mapFn) {
45
+ return this.ok ? this : err(mapFn(this.error));
46
+ }
47
+ function mapOkAndErr({
48
+ ok: mapFn,
49
+ err: mapErrFn
50
+ }) {
51
+ return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
52
+ }
53
+ function returnResult() {
54
+ return this;
55
+ }
41
56
  function ok(value = void 0) {
42
57
  return {
43
58
  ok: true,
@@ -45,7 +60,10 @@ function ok(value = void 0) {
45
60
  value,
46
61
  unwrapOrNull: okUnwrapOr,
47
62
  unwrapOr: okUnwrapOr,
48
- unwrap: okUnwrapOr
63
+ unwrap: okUnwrapOr,
64
+ mapOk: okMap,
65
+ mapErr: returnResult,
66
+ mapOkAndErr
49
67
  };
50
68
  }
51
69
  function err(error) {
@@ -61,25 +79,25 @@ function err(error) {
61
79
  if (error instanceof Error) {
62
80
  throw error;
63
81
  }
64
- if (Array.isArray(error)) {
65
- throw new Error(error.join("\n"), { cause: error });
66
- }
67
- throw new Error(
68
- isObject(error) && "message" in error && error.message ? String(error.message) : "Unwrap failed",
69
- { cause: error }
70
- );
71
- }
82
+ throw normalizeError(error);
83
+ },
84
+ mapOk: returnResult,
85
+ mapErr: errMap,
86
+ mapOkAndErr
72
87
  };
73
88
  }
74
89
  function unknownToError(error) {
75
90
  return err(normalizeError(error));
76
91
  }
77
- async function unwrap(result) {
92
+ async function asyncUnwrap(result) {
78
93
  const unwrapped = await result;
79
94
  if (unwrapped.ok) {
80
95
  return unwrapped.value;
81
96
  }
82
- throw unwrapped.error;
97
+ if (unwrapped.error instanceof Error) {
98
+ throw unwrapped.error;
99
+ }
100
+ throw normalizeError(unwrapped.error);
83
101
  }
84
102
  function asyncMap(resultPromise) {
85
103
  return {
@@ -100,29 +118,12 @@ function asyncMap(resultPromise) {
100
118
  }
101
119
  };
102
120
  }
103
- function syncMap(result) {
104
- return {
105
- err: (mapFn) => {
106
- return result.ok ? ok(result.value) : err(mapFn(result.error));
107
- },
108
- ok: (mapFn) => {
109
- return result.ok ? ok(mapFn(result.value)) : err(result.error);
110
- },
111
- okAndErr: ({
112
- ok: mapFn,
113
- err: mapErrFn
114
- }) => {
115
- return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
116
- }
117
- };
118
- }
119
121
  var Result = {
120
122
  ok,
121
123
  err,
122
124
  unknownToError,
123
- unwrap,
124
- asyncMap,
125
- map: syncMap
125
+ asyncUnwrap,
126
+ asyncMap
126
127
  };
127
128
  function normalizeError(error) {
128
129
  if (error instanceof Error) return error;
@@ -131,7 +132,7 @@ function normalizeError(error) {
131
132
  }
132
133
  if (isObject(error)) {
133
134
  return new Error(
134
- "message" in error && error.message ? String(error.message) : safeJsonStringify(error) ?? "unknown",
135
+ "message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
135
136
  { cause: error }
136
137
  );
137
138
  }
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Result,
3
3
  normalizeError
4
- } from "./chunk-JOBPQAI3.js";
4
+ } from "./chunk-3PXKYWJF.js";
5
5
  import {
6
6
  sleep
7
7
  } from "./chunk-5DZT3Z5Z.js";
package/dist/rsResult.cjs CHANGED
@@ -38,6 +38,21 @@ function isObject(value) {
38
38
  function okUnwrapOr() {
39
39
  return this.value;
40
40
  }
41
+ function okMap(mapFn) {
42
+ return this.ok ? ok(mapFn(this.value)) : this;
43
+ }
44
+ function errMap(mapFn) {
45
+ return this.ok ? this : err(mapFn(this.error));
46
+ }
47
+ function mapOkAndErr({
48
+ ok: mapFn,
49
+ err: mapErrFn
50
+ }) {
51
+ return this.ok ? ok(mapFn(this.value)) : err(mapErrFn(this.error));
52
+ }
53
+ function returnResult() {
54
+ return this;
55
+ }
41
56
  function ok(value = void 0) {
42
57
  return {
43
58
  ok: true,
@@ -45,7 +60,10 @@ function ok(value = void 0) {
45
60
  value,
46
61
  unwrapOrNull: okUnwrapOr,
47
62
  unwrapOr: okUnwrapOr,
48
- unwrap: okUnwrapOr
63
+ unwrap: okUnwrapOr,
64
+ mapOk: okMap,
65
+ mapErr: returnResult,
66
+ mapOkAndErr
49
67
  };
50
68
  }
51
69
  function err(error) {
@@ -61,25 +79,25 @@ function err(error) {
61
79
  if (error instanceof Error) {
62
80
  throw error;
63
81
  }
64
- if (Array.isArray(error)) {
65
- throw new Error(error.join("\n"), { cause: error });
66
- }
67
- throw new Error(
68
- isObject(error) && "message" in error && error.message ? String(error.message) : "Unwrap failed",
69
- { cause: error }
70
- );
71
- }
82
+ throw normalizeError(error);
83
+ },
84
+ mapOk: returnResult,
85
+ mapErr: errMap,
86
+ mapOkAndErr
72
87
  };
73
88
  }
74
89
  function unknownToError(error) {
75
90
  return err(normalizeError(error));
76
91
  }
77
- async function unwrap(result) {
92
+ async function asyncUnwrap(result) {
78
93
  const unwrapped = await result;
79
94
  if (unwrapped.ok) {
80
95
  return unwrapped.value;
81
96
  }
82
- throw unwrapped.error;
97
+ if (unwrapped.error instanceof Error) {
98
+ throw unwrapped.error;
99
+ }
100
+ throw normalizeError(unwrapped.error);
83
101
  }
84
102
  function asyncMap(resultPromise) {
85
103
  return {
@@ -100,29 +118,12 @@ function asyncMap(resultPromise) {
100
118
  }
101
119
  };
102
120
  }
103
- function syncMap(result) {
104
- return {
105
- err: (mapFn) => {
106
- return result.ok ? ok(result.value) : err(mapFn(result.error));
107
- },
108
- ok: (mapFn) => {
109
- return result.ok ? ok(mapFn(result.value)) : err(result.error);
110
- },
111
- okAndErr: ({
112
- ok: mapFn,
113
- err: mapErrFn
114
- }) => {
115
- return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
116
- }
117
- };
118
- }
119
121
  var Result = {
120
122
  ok,
121
123
  err,
122
124
  unknownToError,
123
- unwrap,
124
- asyncMap,
125
- map: syncMap
125
+ asyncUnwrap,
126
+ asyncMap
126
127
  };
127
128
  function resultify(fn, errorNormalizer) {
128
129
  try {
@@ -149,7 +150,7 @@ function normalizeError(error) {
149
150
  }
150
151
  if (isObject(error)) {
151
152
  return new Error(
152
- "message" in error && error.message ? String(error.message) : safeJsonStringify(error) ?? "unknown",
153
+ "message" in error && error.message && typeof error.message === "string" ? error.message : safeJsonStringify(error) ?? "unknown",
153
154
  { cause: error }
154
155
  );
155
156
  }
@@ -3,20 +3,24 @@ type Ok<T> = {
3
3
  error: false;
4
4
  value: T;
5
5
  };
6
- type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
7
6
  type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
8
7
  type Err<E extends ResultValidErrors> = {
9
8
  ok: false;
10
9
  error: E;
11
10
  errorResult: () => Result<any, E>;
12
11
  };
13
- type Unwrap<E, T> = IsExactlyAny<E> extends true ? any : E extends Error ? () => T : unknown;
14
- type ResultMethods<T, E> = {
12
+ type ResultMethods<T, E extends ResultValidErrors> = {
15
13
  /** Returns the value if the result is Ok, otherwise returns null */
16
14
  unwrapOrNull: () => T | null;
17
15
  /** Returns the value if the result is Ok, otherwise returns the provided default value */
18
16
  unwrapOr: <R extends T>(defaultValue: R) => T | R;
19
- unwrap: Unwrap<E, T>;
17
+ unwrap: () => T;
18
+ mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
19
+ mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
20
+ mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
21
+ ok: (value: T) => NewValue;
22
+ err: (error: E) => NewError;
23
+ }) => Result<NewValue, NewError>;
20
24
  };
21
25
  type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
22
26
  declare function ok(): OkResult<void, any>;
@@ -25,7 +29,7 @@ type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T,
25
29
  declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
26
30
  declare function unknownToError(error: unknown): ErrResult<Error, any>;
27
31
  /** Unwraps a promise result */
28
- declare function unwrap<T>(result: Promise<Result<T, Error>>): Promise<T>;
32
+ declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
29
33
  declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
30
34
  err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
31
35
  ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
@@ -34,14 +38,6 @@ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise
34
38
  err: (error: E) => NewError;
35
39
  }) => Promise<Result<NewValue, NewError>>;
36
40
  };
37
- declare function syncMap<T, E extends ResultValidErrors>(result: Result<T, E>): {
38
- err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
39
- ok: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
40
- okAndErr: <NewValue, NewError extends ResultValidErrors>({ ok: mapFn, err: mapErrFn, }: {
41
- ok: (value: T) => NewValue;
42
- err: (error: E) => NewError;
43
- }) => Result<NewValue, NewError>;
44
- };
45
41
  /**
46
42
  * Util for implementing something similar to Result<T, E> in Rust, for better error handling.
47
43
  *
@@ -69,9 +65,8 @@ declare const Result: {
69
65
  ok: typeof ok;
70
66
  err: typeof err;
71
67
  unknownToError: typeof unknownToError;
72
- unwrap: typeof unwrap;
68
+ asyncUnwrap: typeof asyncUnwrap;
73
69
  asyncMap: typeof asyncMap;
74
- map: typeof syncMap;
75
70
  };
76
71
  /** transform a function in a result function */
77
72
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
@@ -3,20 +3,24 @@ type Ok<T> = {
3
3
  error: false;
4
4
  value: T;
5
5
  };
6
- type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
7
6
  type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
8
7
  type Err<E extends ResultValidErrors> = {
9
8
  ok: false;
10
9
  error: E;
11
10
  errorResult: () => Result<any, E>;
12
11
  };
13
- type Unwrap<E, T> = IsExactlyAny<E> extends true ? any : E extends Error ? () => T : unknown;
14
- type ResultMethods<T, E> = {
12
+ type ResultMethods<T, E extends ResultValidErrors> = {
15
13
  /** Returns the value if the result is Ok, otherwise returns null */
16
14
  unwrapOrNull: () => T | null;
17
15
  /** Returns the value if the result is Ok, otherwise returns the provided default value */
18
16
  unwrapOr: <R extends T>(defaultValue: R) => T | R;
19
- unwrap: Unwrap<E, T>;
17
+ unwrap: () => T;
18
+ mapOk: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
19
+ mapErr: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
20
+ mapOkAndErr: <NewValue, NewError extends ResultValidErrors>(mapFns: {
21
+ ok: (value: T) => NewValue;
22
+ err: (error: E) => NewError;
23
+ }) => Result<NewValue, NewError>;
20
24
  };
21
25
  type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
22
26
  declare function ok(): OkResult<void, any>;
@@ -25,7 +29,7 @@ type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T,
25
29
  declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
26
30
  declare function unknownToError(error: unknown): ErrResult<Error, any>;
27
31
  /** Unwraps a promise result */
28
- declare function unwrap<T>(result: Promise<Result<T, Error>>): Promise<T>;
32
+ declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
29
33
  declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
30
34
  err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
31
35
  ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
@@ -34,14 +38,6 @@ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise
34
38
  err: (error: E) => NewError;
35
39
  }) => Promise<Result<NewValue, NewError>>;
36
40
  };
37
- declare function syncMap<T, E extends ResultValidErrors>(result: Result<T, E>): {
38
- err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Result<T, NewError>;
39
- ok: <NewValue>(mapFn: (value: T) => NewValue) => Result<NewValue, E>;
40
- okAndErr: <NewValue, NewError extends ResultValidErrors>({ ok: mapFn, err: mapErrFn, }: {
41
- ok: (value: T) => NewValue;
42
- err: (error: E) => NewError;
43
- }) => Result<NewValue, NewError>;
44
- };
45
41
  /**
46
42
  * Util for implementing something similar to Result<T, E> in Rust, for better error handling.
47
43
  *
@@ -69,9 +65,8 @@ declare const Result: {
69
65
  ok: typeof ok;
70
66
  err: typeof err;
71
67
  unknownToError: typeof unknownToError;
72
- unwrap: typeof unwrap;
68
+ asyncUnwrap: typeof asyncUnwrap;
73
69
  asyncMap: typeof asyncMap;
74
- map: typeof syncMap;
75
70
  };
76
71
  /** transform a function in a result function */
77
72
  declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
package/dist/rsResult.js CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  normalizeError,
6
6
  resultify,
7
7
  safeJsonStringify
8
- } from "./chunk-JOBPQAI3.js";
8
+ } from "./chunk-3PXKYWJF.js";
9
9
  import "./chunk-4UGSP3L3.js";
10
10
  export {
11
11
  Result,
@@ -53,20 +53,22 @@ function isObject(value) {
53
53
 
54
54
  // src/deepEqual.ts
55
55
  var has = Object.prototype.hasOwnProperty;
56
- function find(iter, tar, key) {
57
- for (key of iter.keys()) {
58
- if (deepEqual(key, tar)) return key;
56
+ function find(iter, tar, maxDepth) {
57
+ for (const key of iter.keys()) {
58
+ if (deepEqual(key, tar, maxDepth)) return key;
59
59
  }
60
60
  }
61
- function deepEqual(foo, bar) {
61
+ function deepEqual(foo, bar, maxDepth = 20) {
62
62
  let ctor, len, tmp;
63
63
  if (foo === bar) return true;
64
+ if (maxDepth && maxDepth <= 0) return false;
64
65
  if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
65
- if (ctor === Date) return deepEqual(foo.getTime(), bar.getTime());
66
+ if (ctor === Date)
67
+ return deepEqual(foo.getTime(), bar.getTime(), maxDepth - 1);
66
68
  if (ctor === RegExp) return foo.toString() === bar.toString();
67
69
  if (ctor === Array) {
68
70
  if ((len = foo.length) === bar.length) {
69
- while (len-- && deepEqual(foo[len], bar[len])) ;
71
+ while (len-- && deepEqual(foo[len], bar[len], maxDepth - 1)) ;
70
72
  }
71
73
  return len === -1;
72
74
  }
@@ -77,7 +79,7 @@ function deepEqual(foo, bar) {
77
79
  for (len of foo) {
78
80
  tmp = len;
79
81
  if (tmp && typeof tmp === "object") {
80
- tmp = find(bar, tmp);
82
+ tmp = find(bar, tmp, maxDepth - 1);
81
83
  if (!tmp) return false;
82
84
  }
83
85
  if (!bar.has(tmp)) return false;
@@ -91,10 +93,10 @@ function deepEqual(foo, bar) {
91
93
  for (len of foo) {
92
94
  tmp = len[0];
93
95
  if (tmp && typeof tmp === "object") {
94
- tmp = find(bar, tmp);
96
+ tmp = find(bar, tmp, maxDepth - 1);
95
97
  if (!tmp) return false;
96
98
  }
97
- if (!deepEqual(len[1], bar.get(tmp))) {
99
+ if (!deepEqual(len[1], bar.get(tmp), maxDepth - 1)) {
98
100
  return false;
99
101
  }
100
102
  }
@@ -104,7 +106,8 @@ function deepEqual(foo, bar) {
104
106
  len = 0;
105
107
  for (ctor in foo) {
106
108
  if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
107
- if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor])) return false;
109
+ if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor], maxDepth - 1))
110
+ return false;
108
111
  }
109
112
  return Object.keys(bar).length === len;
110
113
  }
package/dist/testUtils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  deepEqual
3
- } from "./chunk-KCOXGSRA.js";
3
+ } from "./chunk-JQFUKJU5.js";
4
4
  import {
5
5
  clampMin
6
6
  } from "./chunk-NWXBMMHO.js";
@@ -11,7 +11,7 @@ import {
11
11
  import {
12
12
  arrayWithPrevAndIndex,
13
13
  filterAndMap
14
- } from "./chunk-AZBBTE33.js";
14
+ } from "./chunk-QMFZE2VO.js";
15
15
  import {
16
16
  isObject
17
17
  } from "./chunk-4UGSP3L3.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.0.0",
4
+ "version": "2.1.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -165,15 +165,17 @@
165
165
  },
166
166
  "devDependencies": {
167
167
  "@ls-stack/extended-lint": "^0.2.0",
168
+ "@types/eslint": "^9.6.1",
169
+ "@types/eslint__js": "^8.42.3",
168
170
  "@types/node": "^22.5.5",
169
171
  "@typescript-eslint/eslint-plugin": "^8.6.0",
170
172
  "@typescript-eslint/parser": "^8.6.0",
171
173
  "@vitest/ui": "^2.1.1",
174
+ "dequal": "^2.0.3",
172
175
  "eslint": "^9.10.0",
173
- "@types/eslint": "^9.6.1",
174
- "@types/eslint__js": "^8.42.3",
175
176
  "eslint-plugin-unicorn": "^55.0.0",
176
177
  "eslint-plugin-vitest": "^0.5.4",
178
+ "mitata": "^1.0.17",
177
179
  "prettier": "3.3.3",
178
180
  "prettier-plugin-organize-imports": "^4.0.0",
179
181
  "tsm": "^2.3.0",
@@ -283,6 +285,7 @@
283
285
  "build:update-exports": "tsm --no-warnings scripts/updatePackageExports.ts",
284
286
  "build-test": "tsup --config tsup.test.config.ts",
285
287
  "pre-publish": "./scripts/check-if-is-sync.sh && pnpm build",
286
- "test:console-fmt": "tsm --no-warnings scripts/testConsoleFmt.ts"
288
+ "test:console-fmt": "tsm --no-warnings scripts/testConsoleFmt.ts",
289
+ "bench:deepEqual": "tsm --no-warnings benchmarks/deepEqual.ts"
287
290
  }
288
291
  }