@goodbyenjn/utils 1.3.1 → 26.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.
- package/README.md +625 -87
- package/dist/chunks/chunk-267b337b.js +2782 -0
- package/dist/{libs/result-c7c586dd.d.ts → chunks/chunk-4b76d1c4.d.ts} +69 -84
- package/dist/chunks/chunk-72aa7743.js +1082 -0
- package/dist/{libs/types-92e74e19.d.ts → chunks/chunk-a07ed28f.d.ts} +4113 -2427
- package/dist/{index.d.ts → common.d.ts} +50 -20
- package/dist/common.js +3 -0
- package/dist/fs.d.ts +187 -32
- package/dist/fs.js +2640 -174
- package/dist/global-types.d.ts +59 -84
- package/dist/remeda.d.ts +6305 -1228
- package/dist/remeda.js +2 -2
- package/dist/result.d.ts +2 -3
- package/dist/result.js +2 -3
- package/dist/types.d.ts +2 -2
- package/dist/types.js +1 -0
- package/package.json +24 -16
- package/dist/index.js +0 -5
- package/dist/libs/common-7c67f2df.js +0 -334
- package/dist/libs/remeda-16106be4.js +0 -2726
- package/dist/libs/result-a10bbd74.js +0 -243
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
import { isPromiseLike, o$5 as o, t$4 as t, t$6 as t$1 } from "./remeda-16106be4.js";
|
|
2
|
-
|
|
3
|
-
//#region src/result/result.ts
|
|
4
|
-
const nil = null;
|
|
5
|
-
var Result = class Result {
|
|
6
|
-
static ok(value) {
|
|
7
|
-
return new Ok(value);
|
|
8
|
-
}
|
|
9
|
-
static err(error) {
|
|
10
|
-
const err$1 = new Err(error);
|
|
11
|
-
if (error instanceof Error) err$1["stack"] = error.stack;
|
|
12
|
-
else if ("captureStackTrace" in Error) {
|
|
13
|
-
const dummy = {};
|
|
14
|
-
Error.captureStackTrace(dummy, Result.err);
|
|
15
|
-
err$1["stack"] = dummy.stack;
|
|
16
|
-
}
|
|
17
|
-
return err$1;
|
|
18
|
-
}
|
|
19
|
-
static try(fnOrData, onThrow) {
|
|
20
|
-
try {
|
|
21
|
-
let data = fnOrData;
|
|
22
|
-
if (t(fnOrData)) data = fnOrData();
|
|
23
|
-
if (!isPromiseLike(data)) return ok(data);
|
|
24
|
-
return data.then((value) => ok(value), (error) => err(onThrow ? onThrow(error) : error));
|
|
25
|
-
} catch (error) {
|
|
26
|
-
return err(onThrow ? onThrow(error) : error);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
static all(results) {
|
|
30
|
-
let acc = ok([]);
|
|
31
|
-
for (const result of results) {
|
|
32
|
-
if (!result.isOk()) {
|
|
33
|
-
acc = err(result.error);
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
acc = acc.map((values) => [...values, result.value]);
|
|
37
|
-
}
|
|
38
|
-
return acc;
|
|
39
|
-
}
|
|
40
|
-
static allSettled(results) {
|
|
41
|
-
let acc = ok([]);
|
|
42
|
-
for (const result of results) if (result.isErr() && acc.isErr()) acc = acc.mapErr((errors) => [...errors, result.error]);
|
|
43
|
-
else if (result.isOk() && acc.isOk()) acc = acc.map((values) => [...values, result.value]);
|
|
44
|
-
else if (result.isErr() && acc.isOk()) acc = err([result.error]);
|
|
45
|
-
return acc;
|
|
46
|
-
}
|
|
47
|
-
ctxs = [];
|
|
48
|
-
/**
|
|
49
|
-
* Check if `Result` is `OK`
|
|
50
|
-
*/
|
|
51
|
-
isOk() {
|
|
52
|
-
return this.ok;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Check if `Result` is `OK` and the value matches the predicate
|
|
56
|
-
*/
|
|
57
|
-
isOkAnd(predicate) {
|
|
58
|
-
return this.isOk() && predicate(this.value);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Check if `Result` is `Err`
|
|
62
|
-
*/
|
|
63
|
-
isErr() {
|
|
64
|
-
return !this.ok;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Check if `Result` is `Err` and the error matches the predicate
|
|
68
|
-
*/
|
|
69
|
-
isErrAnd(predicate) {
|
|
70
|
-
return this.isErr() && predicate(this.error);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Maps `Result<T, E>` to `Result<U, E>`
|
|
74
|
-
*/
|
|
75
|
-
map(fn) {
|
|
76
|
-
return this.isErr() ? this : ok(fn(this.value));
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Maps `Result<T, E>` to `Result<T, F>`
|
|
80
|
-
*/
|
|
81
|
-
mapErr(fn) {
|
|
82
|
-
return this.isOk() ? this : err(fn(this.error));
|
|
83
|
-
}
|
|
84
|
-
and(result) {
|
|
85
|
-
return this.isErr() ? this : result;
|
|
86
|
-
}
|
|
87
|
-
andThen(fn) {
|
|
88
|
-
return this.isErr() ? this : fn(this.value);
|
|
89
|
-
}
|
|
90
|
-
or(result) {
|
|
91
|
-
return this.isOk() ? this : result;
|
|
92
|
-
}
|
|
93
|
-
orElse(fn) {
|
|
94
|
-
return this.isOk() ? this : fn(this.error);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Calls the function with the value if `Result` is `Ok` and returns the result unchanged
|
|
98
|
-
*/
|
|
99
|
-
inspect(fn) {
|
|
100
|
-
try {
|
|
101
|
-
this.isOk() && fn(this.value);
|
|
102
|
-
} catch {}
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Calls the function with the error if `Result` is `Err` and returns the result unchanged
|
|
107
|
-
*/
|
|
108
|
-
inspectErr(fn) {
|
|
109
|
-
try {
|
|
110
|
-
this.isErr() && fn(this.error);
|
|
111
|
-
} catch {}
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Unwrap the `Ok` value, or return the provided value if `Result` is `Err`
|
|
116
|
-
*/
|
|
117
|
-
unwrapOr(defaultValue) {
|
|
118
|
-
return this.isOk() ? this.value : defaultValue;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Matches the `Result` variant and executes the corresponding function
|
|
122
|
-
*/
|
|
123
|
-
match(ok$1, err$1) {
|
|
124
|
-
return this.isOk() ? ok$1(this.value) : err$1(this.error);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Returns an iterable object that yields the `Ok` value and `Err` value
|
|
128
|
-
*/
|
|
129
|
-
iter() {
|
|
130
|
-
if (this.isOk()) return [
|
|
131
|
-
true,
|
|
132
|
-
nil,
|
|
133
|
-
this.value
|
|
134
|
-
];
|
|
135
|
-
else return [
|
|
136
|
-
false,
|
|
137
|
-
this.error,
|
|
138
|
-
nil
|
|
139
|
-
];
|
|
140
|
-
}
|
|
141
|
-
*[Symbol.iterator]() {
|
|
142
|
-
if (this.isOk()) return this.value;
|
|
143
|
-
const self = this;
|
|
144
|
-
yield self;
|
|
145
|
-
return self;
|
|
146
|
-
}
|
|
147
|
-
context(context) {
|
|
148
|
-
this.ctxs.push(context);
|
|
149
|
-
return this;
|
|
150
|
-
}
|
|
151
|
-
withContext(fn) {
|
|
152
|
-
this.ctxs.push(fn);
|
|
153
|
-
return this;
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
var Ok = class extends Result {
|
|
157
|
-
ok = true;
|
|
158
|
-
_value;
|
|
159
|
-
constructor(value) {
|
|
160
|
-
super();
|
|
161
|
-
this._value = value;
|
|
162
|
-
}
|
|
163
|
-
get value() {
|
|
164
|
-
return this._value;
|
|
165
|
-
}
|
|
166
|
-
get error() {
|
|
167
|
-
return nil;
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
var Err = class extends Result {
|
|
171
|
-
ok = false;
|
|
172
|
-
_error;
|
|
173
|
-
stack;
|
|
174
|
-
constructor(error) {
|
|
175
|
-
super();
|
|
176
|
-
this._error = error;
|
|
177
|
-
}
|
|
178
|
-
get value() {
|
|
179
|
-
return nil;
|
|
180
|
-
}
|
|
181
|
-
get error() {
|
|
182
|
-
return this._error;
|
|
183
|
-
}
|
|
184
|
-
print(presetOrOptions) {
|
|
185
|
-
const options = {
|
|
186
|
-
level: "error",
|
|
187
|
-
context: true,
|
|
188
|
-
stack: false
|
|
189
|
-
};
|
|
190
|
-
if (t$1(presetOrOptions)) {
|
|
191
|
-
options.context = presetOrOptions === "full" || presetOrOptions === "standard";
|
|
192
|
-
options.stack = presetOrOptions === "full";
|
|
193
|
-
} else if (o(presetOrOptions)) {
|
|
194
|
-
options.level = presetOrOptions.level ?? options.level;
|
|
195
|
-
options.context = presetOrOptions.context ?? options.context;
|
|
196
|
-
options.stack = presetOrOptions.stack ?? options.stack;
|
|
197
|
-
}
|
|
198
|
-
const output = this.format(options.context, options.stack);
|
|
199
|
-
switch (options.level) {
|
|
200
|
-
case "error":
|
|
201
|
-
console.error(output);
|
|
202
|
-
break;
|
|
203
|
-
case "warn":
|
|
204
|
-
console.warn(output);
|
|
205
|
-
break;
|
|
206
|
-
case "info":
|
|
207
|
-
console.info(output);
|
|
208
|
-
break;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
format(context, stack) {
|
|
212
|
-
const contexts = this.ctxs.slice().toReversed().map((ctx) => t(ctx) ? ctx() : ctx);
|
|
213
|
-
const stacks = this.stack?.split("\n").map((line) => line.trim()).filter(Boolean) || ["<no stack trace>"];
|
|
214
|
-
let message;
|
|
215
|
-
try {
|
|
216
|
-
message = this._error instanceof Error ? this._error.message : JSON.stringify(this._error);
|
|
217
|
-
} catch {
|
|
218
|
-
message = String(this._error);
|
|
219
|
-
}
|
|
220
|
-
const lines = [`Error: ${contexts.length > 0 ? contexts.at(0) : message}`];
|
|
221
|
-
if (context) lines.push("", "Caused by:", contexts.slice(1).concat(message).map((line, index) => ` ${index}: ${line}`));
|
|
222
|
-
if (stack) {
|
|
223
|
-
const top = stacks.at(0) || "";
|
|
224
|
-
const hasErrorMessage = (/* @__PURE__ */ new RegExp(`^\\w+:\\s+${message}$`)).test(top) || /^\w+$/.test(top);
|
|
225
|
-
lines.push("", "Stack trace:", stacks.slice(hasErrorMessage ? 1 : 0).map((line) => ` ${line}`));
|
|
226
|
-
}
|
|
227
|
-
const output = lines.flat().join("\n");
|
|
228
|
-
return output;
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
const ok = Result.ok;
|
|
232
|
-
const err = Result.err;
|
|
233
|
-
|
|
234
|
-
//#endregion
|
|
235
|
-
//#region src/result/utils.ts
|
|
236
|
-
function safeTry(body) {
|
|
237
|
-
const next = body().next();
|
|
238
|
-
if (isPromiseLike(next)) return next.then((res) => res.value);
|
|
239
|
-
return next.value;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
//#endregion
|
|
243
|
-
export { Err, Ok, Result, err, ok, safeTry };
|