@ls-stack/utils 1.13.2 → 2.0.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 (140) hide show
  1. package/dist/arrayUtils.cjs +91 -0
  2. package/dist/arrayUtils.d.cts +51 -0
  3. package/dist/arrayUtils.js +1 -2
  4. package/dist/assertions.cjs +99 -0
  5. package/dist/assertions.d.cts +17 -0
  6. package/dist/assertions.js +1 -2
  7. package/dist/castValues.cjs +41 -0
  8. package/dist/castValues.d.cts +4 -0
  9. package/dist/castValues.js +0 -1
  10. package/dist/{chunk-FYMTZQ42.js → chunk-4UGSP3L3.js} +0 -1
  11. package/dist/{chunk-HLFWWIDK.js → chunk-5DZT3Z5Z.js} +0 -1
  12. package/dist/{chunk-W7X6NSGR.js → chunk-AZBBTE33.js} +0 -1
  13. package/dist/{chunk-MMKMF4GG.js → chunk-IATIXMCE.js} +0 -1
  14. package/dist/chunk-JOBPQAI3.js +147 -0
  15. package/dist/{chunk-M74KKYE4.js → chunk-KCOXGSRA.js} +0 -1
  16. package/dist/{chunk-NKCC3375.js → chunk-NWXBMMHO.js} +0 -1
  17. package/dist/{chunk-SNAAKCTH.js → chunk-RVVH3MDA.js} +0 -1
  18. package/dist/{chunk-4QK76IRX.js → chunk-T5WDDPFI.js} +1 -6
  19. package/dist/{chunk-GLZ5MZC5.js → chunk-V2L472LS.js} +0 -30
  20. package/dist/consoleFmt.cjs +84 -0
  21. package/dist/consoleFmt.d.cts +48 -0
  22. package/dist/consoleFmt.js +0 -1
  23. package/dist/conversions.cjs +44 -0
  24. package/dist/conversions.d.cts +3 -0
  25. package/dist/conversions.js +1 -2
  26. package/dist/createThrottleController.cjs +183 -0
  27. package/dist/createThrottleController.d.cts +16 -0
  28. package/dist/createThrottleController.js +2 -3
  29. package/dist/debounce.cjs +125 -0
  30. package/dist/debounce.d.cts +45 -0
  31. package/dist/debounce.js +0 -1
  32. package/dist/dedent.cjs +57 -0
  33. package/dist/dedent.d.cts +4 -0
  34. package/dist/dedent.js +0 -1
  35. package/dist/deepEqual.cjs +88 -0
  36. package/dist/deepEqual.d.cts +4 -0
  37. package/dist/deepEqual.js +1 -2
  38. package/dist/enhancedMap.cjs +128 -0
  39. package/dist/enhancedMap.d.cts +20 -0
  40. package/dist/enhancedMap.d.ts +0 -2
  41. package/dist/enhancedMap.js +2 -3
  42. package/dist/exhaustiveMatch.cjs +55 -0
  43. package/dist/exhaustiveMatch.d.cts +6 -0
  44. package/dist/exhaustiveMatch.js +0 -1
  45. package/dist/interpolate.cjs +88 -0
  46. package/dist/interpolate.d.cts +11 -0
  47. package/dist/interpolate.js +2 -3
  48. package/dist/levenshtein.cjs +180 -0
  49. package/dist/levenshtein.d.cts +5 -0
  50. package/dist/levenshtein.js +0 -1
  51. package/dist/main.cjs +4 -0
  52. package/dist/main.d.cts +2 -0
  53. package/dist/main.d.ts +1 -1
  54. package/dist/main.js +0 -1
  55. package/dist/mathUtils.cjs +50 -0
  56. package/dist/mathUtils.d.cts +6 -0
  57. package/dist/mathUtils.js +1 -2
  58. package/dist/objUtils.cjs +67 -0
  59. package/dist/objUtils.d.cts +7 -0
  60. package/dist/objUtils.js +1 -2
  61. package/dist/parallelAsyncCalls.cjs +265 -0
  62. package/dist/{parallelAsyncResultCalls.d.ts → parallelAsyncCalls.d.cts} +7 -7
  63. package/dist/parallelAsyncCalls.d.ts +76 -0
  64. package/dist/{parallelAsyncResultCalls.js → parallelAsyncCalls.js} +7 -9
  65. package/dist/promiseUtils.cjs +38 -0
  66. package/dist/promiseUtils.d.cts +8 -0
  67. package/dist/promiseUtils.js +0 -1
  68. package/dist/rsResult.cjs +179 -0
  69. package/dist/rsResult.d.cts +88 -0
  70. package/dist/rsResult.d.ts +33 -63
  71. package/dist/rsResult.js +6 -10
  72. package/dist/runShellCmd.cjs +127 -0
  73. package/dist/runShellCmd.d.cts +23 -0
  74. package/dist/runShellCmd.d.ts +2 -1
  75. package/dist/runShellCmd.js +9 -4
  76. package/dist/shallowEqual.cjs +88 -0
  77. package/dist/shallowEqual.d.cts +4 -0
  78. package/dist/shallowEqual.js +0 -1
  79. package/dist/sleep.cjs +32 -0
  80. package/dist/sleep.d.cts +3 -0
  81. package/dist/sleep.js +1 -2
  82. package/dist/stringUtils.cjs +66 -0
  83. package/dist/stringUtils.d.cts +16 -0
  84. package/dist/stringUtils.d.ts +1 -3
  85. package/dist/stringUtils.js +1 -4
  86. package/dist/testUtils.cjs +357 -0
  87. package/dist/testUtils.d.cts +38 -0
  88. package/dist/testUtils.js +5 -6
  89. package/dist/typingFnUtils.cjs +55 -0
  90. package/dist/typingFnUtils.d.cts +15 -0
  91. package/dist/typingFnUtils.js +0 -1
  92. package/dist/typingTestUtils.cjs +43 -0
  93. package/dist/typingTestUtils.d.cts +12 -0
  94. package/dist/typingTestUtils.js +0 -1
  95. package/dist/typingUtils.cjs +18 -0
  96. package/dist/typingUtils.d.cts +11 -0
  97. package/dist/typingUtils.js +0 -1
  98. package/dist/yamlStringify.cjs +346 -0
  99. package/dist/yamlStringify.d.cts +8 -0
  100. package/dist/yamlStringify.js +3 -4
  101. package/package.json +64 -34
  102. package/dist/arrayUtils.js.map +0 -1
  103. package/dist/assertions.js.map +0 -1
  104. package/dist/castValues.js.map +0 -1
  105. package/dist/chunk-4QK76IRX.js.map +0 -1
  106. package/dist/chunk-FYMTZQ42.js.map +0 -1
  107. package/dist/chunk-GLZ5MZC5.js.map +0 -1
  108. package/dist/chunk-HLFWWIDK.js.map +0 -1
  109. package/dist/chunk-I7ZDKDUM.js +0 -217
  110. package/dist/chunk-I7ZDKDUM.js.map +0 -1
  111. package/dist/chunk-M74KKYE4.js.map +0 -1
  112. package/dist/chunk-MMKMF4GG.js.map +0 -1
  113. package/dist/chunk-NKCC3375.js.map +0 -1
  114. package/dist/chunk-SNAAKCTH.js.map +0 -1
  115. package/dist/chunk-W7X6NSGR.js.map +0 -1
  116. package/dist/consoleFmt.js.map +0 -1
  117. package/dist/conversions.js.map +0 -1
  118. package/dist/createThrottleController.js.map +0 -1
  119. package/dist/debounce.js.map +0 -1
  120. package/dist/dedent.js.map +0 -1
  121. package/dist/deepEqual.js.map +0 -1
  122. package/dist/enhancedMap.js.map +0 -1
  123. package/dist/exhaustiveMatch.js.map +0 -1
  124. package/dist/interpolate.js.map +0 -1
  125. package/dist/levenshtein.js.map +0 -1
  126. package/dist/main.js.map +0 -1
  127. package/dist/mathUtils.js.map +0 -1
  128. package/dist/objUtils.js.map +0 -1
  129. package/dist/parallelAsyncResultCalls.js.map +0 -1
  130. package/dist/promiseUtils.js.map +0 -1
  131. package/dist/rsResult.js.map +0 -1
  132. package/dist/runShellCmd.js.map +0 -1
  133. package/dist/shallowEqual.js.map +0 -1
  134. package/dist/sleep.js.map +0 -1
  135. package/dist/stringUtils.js.map +0 -1
  136. package/dist/testUtils.js.map +0 -1
  137. package/dist/typingFnUtils.js.map +0 -1
  138. package/dist/typingTestUtils.js.map +0 -1
  139. package/dist/typingUtils.js.map +0 -1
  140. package/dist/yamlStringify.js.map +0 -1
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/rsResult.ts
21
+ var rsResult_exports = {};
22
+ __export(rsResult_exports, {
23
+ Result: () => Result,
24
+ asyncResultify: () => asyncResultify,
25
+ createTypedResult: () => createTypedResult,
26
+ normalizeError: () => normalizeError,
27
+ resultify: () => resultify,
28
+ safeJsonStringify: () => safeJsonStringify
29
+ });
30
+ module.exports = __toCommonJS(rsResult_exports);
31
+
32
+ // src/assertions.ts
33
+ function isObject(value) {
34
+ return typeof value === "object" && value !== null && !Array.isArray(value);
35
+ }
36
+
37
+ // src/rsResult.ts
38
+ function okUnwrapOr() {
39
+ return this.value;
40
+ }
41
+ function ok(value = void 0) {
42
+ return {
43
+ ok: true,
44
+ error: false,
45
+ value,
46
+ unwrapOrNull: okUnwrapOr,
47
+ unwrapOr: okUnwrapOr,
48
+ unwrap: okUnwrapOr
49
+ };
50
+ }
51
+ function err(error) {
52
+ return {
53
+ ok: false,
54
+ error,
55
+ unwrapOrNull: () => null,
56
+ unwrapOr: (defaultValue) => defaultValue,
57
+ errorResult() {
58
+ return err(error);
59
+ },
60
+ unwrap: () => {
61
+ if (error instanceof Error) {
62
+ throw error;
63
+ }
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
+ }
72
+ };
73
+ }
74
+ function unknownToError(error) {
75
+ return err(normalizeError(error));
76
+ }
77
+ async function unwrap(result) {
78
+ const unwrapped = await result;
79
+ if (unwrapped.ok) {
80
+ return unwrapped.value;
81
+ }
82
+ throw unwrapped.error;
83
+ }
84
+ function asyncMap(resultPromise) {
85
+ return {
86
+ err: async (mapFn) => {
87
+ const result = await resultPromise;
88
+ return result.ok ? ok(result.value) : err(mapFn(result.error));
89
+ },
90
+ ok: async (mapFn) => {
91
+ const result = await resultPromise;
92
+ return result.ok ? ok(mapFn(result.value)) : err(result.error);
93
+ },
94
+ okAndErr: async ({
95
+ ok: mapFn,
96
+ err: mapErrFn
97
+ }) => {
98
+ const result = await resultPromise;
99
+ return result.ok ? ok(mapFn(result.value)) : err(mapErrFn(result.error));
100
+ }
101
+ };
102
+ }
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
+ var Result = {
120
+ ok,
121
+ err,
122
+ unknownToError,
123
+ unwrap,
124
+ asyncMap,
125
+ map: syncMap
126
+ };
127
+ function resultify(fn, errorNormalizer) {
128
+ try {
129
+ return ok(fn());
130
+ } catch (error) {
131
+ return err(
132
+ errorNormalizer ? errorNormalizer(error) : normalizeError(error)
133
+ );
134
+ }
135
+ }
136
+ async function asyncResultify(fn, errorNormalizer) {
137
+ try {
138
+ return ok(await fn());
139
+ } catch (error) {
140
+ return err(
141
+ errorNormalizer ? errorNormalizer(error) : normalizeError(error)
142
+ );
143
+ }
144
+ }
145
+ function normalizeError(error) {
146
+ if (error instanceof Error) return error;
147
+ if (typeof error === "string") {
148
+ return new Error(error);
149
+ }
150
+ if (isObject(error)) {
151
+ return new Error(
152
+ "message" in error && error.message ? String(error.message) : safeJsonStringify(error) ?? "unknown",
153
+ { cause: error }
154
+ );
155
+ }
156
+ return new Error(safeJsonStringify(error) ?? "unknown", { cause: error });
157
+ }
158
+ function safeJsonStringify(value) {
159
+ try {
160
+ return JSON.stringify(value);
161
+ } catch (_) {
162
+ return null;
163
+ }
164
+ }
165
+ function createTypedResult() {
166
+ return {
167
+ ok,
168
+ err
169
+ };
170
+ }
171
+ // Annotate the CommonJS export names for ESM import in node:
172
+ 0 && (module.exports = {
173
+ Result,
174
+ asyncResultify,
175
+ createTypedResult,
176
+ normalizeError,
177
+ resultify,
178
+ safeJsonStringify
179
+ });
@@ -0,0 +1,88 @@
1
+ type Ok<T> = {
2
+ ok: true;
3
+ error: false;
4
+ value: T;
5
+ };
6
+ type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
7
+ type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
8
+ type Err<E extends ResultValidErrors> = {
9
+ ok: false;
10
+ error: E;
11
+ errorResult: () => Result<any, E>;
12
+ };
13
+ type Unwrap<E, T> = IsExactlyAny<E> extends true ? any : E extends Error ? () => T : unknown;
14
+ type ResultMethods<T, E> = {
15
+ /** Returns the value if the result is Ok, otherwise returns null */
16
+ unwrapOrNull: () => T | null;
17
+ /** Returns the value if the result is Ok, otherwise returns the provided default value */
18
+ unwrapOr: <R extends T>(defaultValue: R) => T | R;
19
+ unwrap: Unwrap<E, T>;
20
+ };
21
+ type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
22
+ declare function ok(): OkResult<void, any>;
23
+ declare function ok<T>(value: T): OkResult<T, any>;
24
+ type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
25
+ declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
26
+ declare function unknownToError(error: unknown): ErrResult<Error, any>;
27
+ /** Unwraps a promise result */
28
+ declare function unwrap<T>(result: Promise<Result<T, Error>>): Promise<T>;
29
+ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
30
+ err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
31
+ ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
32
+ okAndErr: <NewValue, NewError extends ResultValidErrors>({ ok: mapFn, err: mapErrFn, }: {
33
+ ok: (value: T) => NewValue;
34
+ err: (error: E) => NewError;
35
+ }) => Promise<Result<NewValue, NewError>>;
36
+ };
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
+ /**
46
+ * Util for implementing something similar to Result<T, E> in Rust, for better error handling.
47
+ *
48
+ * Usage:
49
+ *
50
+ * @example
51
+ * function doSomething(): Result<string, Error> {
52
+ * if (something) {
53
+ * return ok('success');
54
+ * }
55
+ *
56
+ * return err(new Error('something went wrong'));
57
+ * }
58
+ *
59
+ * const result = doSomething();
60
+ *
61
+ * if (result.ok) {
62
+ * // result.value is a string
63
+ * } else {
64
+ * // result.error is an Error
65
+ * }
66
+ */
67
+ type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrResult<E, T>;
68
+ declare const Result: {
69
+ ok: typeof ok;
70
+ err: typeof err;
71
+ unknownToError: typeof unknownToError;
72
+ unwrap: typeof unwrap;
73
+ asyncMap: typeof asyncMap;
74
+ map: typeof syncMap;
75
+ };
76
+ /** transform a function in a result function */
77
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
78
+ /** transform a async function in a result function */
79
+ declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
80
+ declare function normalizeError(error: unknown): Error;
81
+ declare function safeJsonStringify(value: unknown): string | null;
82
+ type TypedResult<T, E extends ResultValidErrors = Error> = {
83
+ ok: (value: T) => OkResult<T, E, T>;
84
+ err: (error: E) => ErrResult<E, T>;
85
+ };
86
+ declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
87
+
88
+ export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify };
@@ -3,20 +3,12 @@ type Ok<T> = {
3
3
  error: false;
4
4
  value: T;
5
5
  };
6
- type NormalizedErrorResultProps = {
7
- id: string;
8
- message: string;
9
- code?: number;
10
- metadata?: ValidErrorMetadata;
11
- };
12
6
  type IsExactlyAny<T> = boolean extends (T extends never ? true : false) ? true : false;
7
+ type ResultValidErrors = Error | Record<string, unknown> | unknown[] | true;
13
8
  type Err<E extends ResultValidErrors> = {
14
9
  ok: false;
15
10
  error: E;
16
11
  errorResult: () => Result<any, E>;
17
- normalizedErrorResult: (normalizedErr: E extends NormalizedError ? {
18
- withMetadata: ValidErrorMetadata;
19
- } | NormalizedErrorResultProps : NormalizedErrorResultProps) => Result<any, NormalizedError>;
20
12
  };
21
13
  type Unwrap<E, T> = IsExactlyAny<E> extends true ? any : E extends Error ? () => T : unknown;
22
14
  type ResultMethods<T, E> = {
@@ -27,23 +19,29 @@ type ResultMethods<T, E> = {
27
19
  unwrap: Unwrap<E, T>;
28
20
  };
29
21
  type OkResult<T, E extends ResultValidErrors, M = any> = Ok<T> & ResultMethods<M, E>;
30
- type ResultValidErrors = Error | Record<string, unknown> | string[];
31
22
  declare function ok(): OkResult<void, any>;
32
23
  declare function ok<T>(value: T): OkResult<T, any>;
33
24
  type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
34
25
  declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
35
- declare function normalizedUnknownErr(error: unknown): ErrResult<NormalizedError<string>, any>;
36
- declare function normalizedErr(message: string): ErrResult<NormalizedError>;
37
- declare function normalizedErr<T extends string>(id: T, message: string): ErrResult<NormalizedError<T>>;
38
- declare function normalizedErr<T extends string>(errorProps: {
39
- id: T;
40
- message: string;
41
- code?: number;
42
- cause?: Error;
43
- metadata?: ValidErrorMetadata;
44
- }): ErrResult<NormalizedError<T>>;
26
+ declare function unknownToError(error: unknown): ErrResult<Error, any>;
45
27
  /** Unwraps a promise result */
46
28
  declare function unwrap<T>(result: Promise<Result<T, Error>>): Promise<T>;
29
+ declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
30
+ err: <NewError extends ResultValidErrors>(mapFn: (error: E) => NewError) => Promise<Result<T, NewError>>;
31
+ ok: <NewValue>(mapFn: (value: T) => NewValue) => Promise<Result<NewValue, E>>;
32
+ okAndErr: <NewValue, NewError extends ResultValidErrors>({ ok: mapFn, err: mapErrFn, }: {
33
+ ok: (value: T) => NewValue;
34
+ err: (error: E) => NewError;
35
+ }) => Promise<Result<NewValue, NewError>>;
36
+ };
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
+ };
47
45
  /**
48
46
  * Util for implementing something similar to Result<T, E> in Rust, for better error handling.
49
47
  *
@@ -66,53 +64,25 @@ declare function unwrap<T>(result: Promise<Result<T, Error>>): Promise<T>;
66
64
  * // result.error is an Error
67
65
  * }
68
66
  */
69
- type Result<T, E extends ResultValidErrors = NormalizedError> = OkResult<T, E, T> | ErrResult<E, T>;
67
+ type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrResult<E, T>;
70
68
  declare const Result: {
71
69
  ok: typeof ok;
72
70
  err: typeof err;
73
- normalizedErr: typeof normalizedErr;
74
- normalizedUnknownErr: typeof normalizedUnknownErr;
71
+ unknownToError: typeof unknownToError;
75
72
  unwrap: typeof unwrap;
73
+ asyncMap: typeof asyncMap;
74
+ map: typeof syncMap;
76
75
  };
77
- /** transfor a function in a result function */
78
- declare function resultify<T, E extends ResultValidErrors = NormalizedError>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
76
+ /** transform a function in a result function */
77
+ declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
79
78
  /** transform a async function in a result function */
80
- declare function asyncResultify<T, E extends Error = NormalizedError>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
81
- type ValidErrorMetadata = Record<string, unknown> | undefined;
82
- declare function setNormalizedErrorSideEffects(callback: (normalizedError: NormalizedError) => void): void;
83
- declare class NormalizedError<T = string> extends Error {
84
- id: T;
85
- metadata?: ValidErrorMetadata;
86
- cause?: Error;
87
- code: number;
88
- constructor({ id, message, metadata, cause, code, }: {
89
- id: T;
90
- message: string;
91
- metadata?: ValidErrorMetadata;
92
- cause?: Error;
93
- code?: number;
94
- });
95
- toString(): string;
96
- toJSON(): {
97
- id: T;
98
- message: string;
99
- metadata: ValidErrorMetadata;
100
- code: number;
101
- cause: string | (Error & Record<"toJSON", unknown>) | undefined;
102
- };
103
- }
104
- declare class NormalizedErrorWithMetadata<M extends ValidErrorMetadata, T = string> extends NormalizedError<T> {
105
- metadata: M;
106
- constructor(props: {
107
- id: T;
108
- message: string;
109
- metadata: M;
110
- cause?: Error;
111
- } | {
112
- error: NormalizedError<T>;
113
- metadata: M;
114
- });
115
- }
116
- declare function normalizeError(error: unknown): NormalizedError;
79
+ declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
80
+ declare function normalizeError(error: unknown): Error;
81
+ declare function safeJsonStringify(value: unknown): string | null;
82
+ type TypedResult<T, E extends ResultValidErrors = Error> = {
83
+ ok: (value: T) => OkResult<T, E, T>;
84
+ err: (error: E) => ErrResult<E, T>;
85
+ };
86
+ declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
117
87
 
118
- export { NormalizedError, NormalizedErrorWithMetadata, Result, type ValidErrorMetadata, asyncResultify, normalizeError, resultify, setNormalizedErrorSideEffects };
88
+ export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify };
package/dist/rsResult.js CHANGED
@@ -1,21 +1,17 @@
1
1
  import {
2
- NormalizedError,
3
- NormalizedErrorWithMetadata,
4
2
  Result,
5
3
  asyncResultify,
4
+ createTypedResult,
6
5
  normalizeError,
7
6
  resultify,
8
- setNormalizedErrorSideEffects
9
- } from "./chunk-I7ZDKDUM.js";
10
- import "./chunk-GLZ5MZC5.js";
11
- import "./chunk-FYMTZQ42.js";
7
+ safeJsonStringify
8
+ } from "./chunk-JOBPQAI3.js";
9
+ import "./chunk-4UGSP3L3.js";
12
10
  export {
13
- NormalizedError,
14
- NormalizedErrorWithMetadata,
15
11
  Result,
16
12
  asyncResultify,
13
+ createTypedResult,
17
14
  normalizeError,
18
15
  resultify,
19
- setNormalizedErrorSideEffects
16
+ safeJsonStringify
20
17
  };
21
- //# sourceMappingURL=rsResult.js.map
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/runShellCmd.ts
21
+ var runShellCmd_exports = {};
22
+ __export(runShellCmd_exports, {
23
+ concurrentCmd: () => concurrentCmd,
24
+ runCmd: () => runCmd,
25
+ runCmdSilent: () => runCmdSilent,
26
+ runCmdSilentUnwrap: () => runCmdSilentUnwrap,
27
+ runCmdUnwrap: () => runCmdUnwrap
28
+ });
29
+ module.exports = __toCommonJS(runShellCmd_exports);
30
+ var import_child_process = require("child_process");
31
+ function runCmd(label, command, {
32
+ mock,
33
+ silent,
34
+ throwOnError,
35
+ cwd = process.cwd(),
36
+ noCiColorForce
37
+ } = {}) {
38
+ if (mock) return Promise.resolve(mock);
39
+ if (label && (!silent || silent === "timeOnly")) {
40
+ console.log(`----${label}----`);
41
+ console.time("\u2705");
42
+ }
43
+ return new Promise((resolve) => {
44
+ const [cmd = "", ...args] = Array.isArray(command) ? command.flatMap(
45
+ (item) => item.startsWith("$") ? item.replace("$", "").split(" ") : item
46
+ ) : command.split(" ");
47
+ const child = (0, import_child_process.spawn)(cmd, args, {
48
+ cwd,
49
+ env: noCiColorForce ? void 0 : { ...process.env, CLICOLOR_FORCE: "1" }
50
+ });
51
+ let stdout = "";
52
+ let stderr = "";
53
+ let out = "";
54
+ child.stdout.on("data", (data) => {
55
+ stdout += String(data);
56
+ out += String(data);
57
+ if (!silent) {
58
+ console.log(String(data));
59
+ }
60
+ });
61
+ child.stderr.on("data", (data) => {
62
+ stderr += String(data);
63
+ out += String(data);
64
+ if (!silent) {
65
+ console.log(String(data));
66
+ }
67
+ });
68
+ child.on("close", (code) => {
69
+ const hasError = code !== 0;
70
+ if (!silent || silent === "timeOnly") {
71
+ if (!hasError) {
72
+ console.timeEnd("\u2705");
73
+ }
74
+ console.log("\n");
75
+ }
76
+ if (throwOnError && hasError) {
77
+ if (silent) {
78
+ if (label) {
79
+ console.log(`----${label}----`);
80
+ } else {
81
+ console.trace();
82
+ }
83
+ console.error(stderr);
84
+ }
85
+ process.exit(1);
86
+ }
87
+ resolve({ label, out, stderr, stdout, error: hasError });
88
+ });
89
+ });
90
+ }
91
+ async function concurrentCmd(label, cmd, onResult) {
92
+ const start = Date.now();
93
+ const result = await runCmd(label, cmd, { silent: true });
94
+ onResult(result);
95
+ const elapsedSeconds = (Date.now() - start) / 1e3;
96
+ console.log(
97
+ `${result.error ? "\u{1F534}" : "\u2705"} ${result.label} (${elapsedSeconds.toFixed(
98
+ 1
99
+ )}s)`
100
+ );
101
+ return () => {
102
+ if (result.error) {
103
+ console.log(`\u274C ${result.label} errors:`);
104
+ console.log(result.out);
105
+ console.log("\n");
106
+ }
107
+ };
108
+ }
109
+ async function runCmdUnwrap(label, command, {
110
+ silent
111
+ } = {}) {
112
+ return (await runCmd(label, command, { silent, throwOnError: true })).stdout;
113
+ }
114
+ function runCmdSilent(command) {
115
+ return runCmd(null, command, { silent: true });
116
+ }
117
+ function runCmdSilentUnwrap(command) {
118
+ return runCmdUnwrap(null, command, { silent: true });
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ concurrentCmd,
123
+ runCmd,
124
+ runCmdSilent,
125
+ runCmdSilentUnwrap,
126
+ runCmdUnwrap
127
+ });
@@ -0,0 +1,23 @@
1
+ type CmdResult = {
2
+ label: string | null;
3
+ out: string;
4
+ error: boolean;
5
+ stdout: string;
6
+ stderr: string;
7
+ };
8
+ type RunCmdOptions = {
9
+ mock?: CmdResult;
10
+ silent?: boolean | 'timeOnly';
11
+ cwd?: string;
12
+ throwOnError?: boolean;
13
+ noCiColorForce?: boolean;
14
+ };
15
+ declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd, noCiColorForce, }?: RunCmdOptions): Promise<CmdResult>;
16
+ declare function concurrentCmd(label: string, cmd: string | string[], onResult: (result: CmdResult) => void): Promise<() => void>;
17
+ declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
18
+ silent?: boolean | 'timeOnly';
19
+ }): Promise<string>;
20
+ declare function runCmdSilent(command: string | string[]): Promise<CmdResult>;
21
+ declare function runCmdSilentUnwrap(command: string | string[]): Promise<string>;
22
+
23
+ export { concurrentCmd, runCmd, runCmdSilent, runCmdSilentUnwrap, runCmdUnwrap };
@@ -10,8 +10,9 @@ type RunCmdOptions = {
10
10
  silent?: boolean | 'timeOnly';
11
11
  cwd?: string;
12
12
  throwOnError?: boolean;
13
+ noCiColorForce?: boolean;
13
14
  };
14
- declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd }?: RunCmdOptions): Promise<CmdResult>;
15
+ declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd, noCiColorForce, }?: RunCmdOptions): Promise<CmdResult>;
15
16
  declare function concurrentCmd(label: string, cmd: string | string[], onResult: (result: CmdResult) => void): Promise<() => void>;
16
17
  declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
17
18
  silent?: boolean | 'timeOnly';
@@ -1,7 +1,12 @@
1
1
  // src/runShellCmd.ts
2
2
  import { spawn } from "child_process";
3
- process.env.FORCE_COLOR = true;
4
- function runCmd(label, command, { mock, silent, throwOnError, cwd = process.cwd() } = {}) {
3
+ function runCmd(label, command, {
4
+ mock,
5
+ silent,
6
+ throwOnError,
7
+ cwd = process.cwd(),
8
+ noCiColorForce
9
+ } = {}) {
5
10
  if (mock) return Promise.resolve(mock);
6
11
  if (label && (!silent || silent === "timeOnly")) {
7
12
  console.log(`----${label}----`);
@@ -12,7 +17,8 @@ function runCmd(label, command, { mock, silent, throwOnError, cwd = process.cwd(
12
17
  (item) => item.startsWith("$") ? item.replace("$", "").split(" ") : item
13
18
  ) : command.split(" ");
14
19
  const child = spawn(cmd, args, {
15
- cwd
20
+ cwd,
21
+ env: noCiColorForce ? void 0 : { ...process.env, CLICOLOR_FORCE: "1" }
16
22
  });
17
23
  let stdout = "";
18
24
  let stderr = "";
@@ -90,4 +96,3 @@ export {
90
96
  runCmdSilentUnwrap,
91
97
  runCmdUnwrap
92
98
  };
93
- //# sourceMappingURL=runShellCmd.js.map