@ls-stack/utils 2.0.0 → 2.0.1

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.
@@ -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
  }
@@ -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,
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.0.1",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"