@filen/utils 0.0.4 → 0.0.6
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/dist/misc.js +56 -0
- package/dist/run.js +17 -21
- package/dist/types/misc.d.ts +5 -0
- package/dist/types/run.d.ts +9 -9
- package/package.json +1 -1
package/dist/misc.js
CHANGED
|
@@ -309,3 +309,59 @@ export function fastLocaleCompare(a, b) {
|
|
|
309
309
|
}
|
|
310
310
|
return caseDiff;
|
|
311
311
|
}
|
|
312
|
+
export const BPS_TO_READABLE_UNITS = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
|
313
|
+
export function bpsToReadable(bps) {
|
|
314
|
+
if (!(bps > 0 && bps < 1099511627776)) {
|
|
315
|
+
return "0.1 B/s";
|
|
316
|
+
}
|
|
317
|
+
let i = 0;
|
|
318
|
+
let value = bps;
|
|
319
|
+
if (value >= 1024) {
|
|
320
|
+
value /= 1024;
|
|
321
|
+
i = 1;
|
|
322
|
+
if (value >= 1024) {
|
|
323
|
+
value /= 1024;
|
|
324
|
+
i = 2;
|
|
325
|
+
if (value >= 1024) {
|
|
326
|
+
value /= 1024;
|
|
327
|
+
i = 3;
|
|
328
|
+
if (value >= 1024) {
|
|
329
|
+
value /= 1024;
|
|
330
|
+
i = 4;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (value < 0.1) {
|
|
336
|
+
value = 0.1;
|
|
337
|
+
}
|
|
338
|
+
return value.toFixed(1) + " " + BPS_TO_READABLE_UNITS[i];
|
|
339
|
+
}
|
|
340
|
+
export const FORMAT_BYTES_SIZES = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
|
341
|
+
export const POWERS_1024 = [1, 1024, 1048576, 1073741824, 1099511627776, 1125899906842624];
|
|
342
|
+
export function formatBytes(bytes, decimals = 2) {
|
|
343
|
+
if (bytes === 0) {
|
|
344
|
+
return "0 B";
|
|
345
|
+
}
|
|
346
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
347
|
+
let i = 0;
|
|
348
|
+
if (bytes >= POWERS_1024[5]) {
|
|
349
|
+
i = 5;
|
|
350
|
+
}
|
|
351
|
+
else if (bytes >= POWERS_1024[4]) {
|
|
352
|
+
i = 4;
|
|
353
|
+
}
|
|
354
|
+
else if (bytes >= POWERS_1024[3]) {
|
|
355
|
+
i = 3;
|
|
356
|
+
}
|
|
357
|
+
else if (bytes >= POWERS_1024[2]) {
|
|
358
|
+
i = 2;
|
|
359
|
+
}
|
|
360
|
+
else if (bytes >= POWERS_1024[1]) {
|
|
361
|
+
i = 1;
|
|
362
|
+
}
|
|
363
|
+
const value = bytes / POWERS_1024[i];
|
|
364
|
+
const multiplier = Math.pow(10, dm);
|
|
365
|
+
const rounded = Math.round(value * multiplier) / multiplier;
|
|
366
|
+
return rounded + " " + FORMAT_BYTES_SIZES[i];
|
|
367
|
+
}
|
package/dist/run.js
CHANGED
|
@@ -23,15 +23,14 @@ export function run(fn, options) {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
catch (e) {
|
|
26
|
-
|
|
27
|
-
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error);
|
|
26
|
+
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
|
|
28
27
|
if (options === null || options === void 0 ? void 0 : options.throw) {
|
|
29
|
-
throw
|
|
28
|
+
throw e;
|
|
30
29
|
}
|
|
31
30
|
return {
|
|
32
31
|
success: false,
|
|
33
32
|
data: null,
|
|
34
|
-
error:
|
|
33
|
+
error: e
|
|
35
34
|
};
|
|
36
35
|
}
|
|
37
36
|
finally {
|
|
@@ -40,7 +39,7 @@ export function run(fn, options) {
|
|
|
40
39
|
yield ((_b = deferredFunctions[i]) === null || _b === void 0 ? void 0 : _b.call(deferredFunctions));
|
|
41
40
|
}
|
|
42
41
|
catch (e) {
|
|
43
|
-
(_c = options === null || options === void 0 ? void 0 : options.onError) === null || _c === void 0 ? void 0 : _c.call(options, e
|
|
42
|
+
(_c = options === null || options === void 0 ? void 0 : options.onError) === null || _c === void 0 ? void 0 : _c.call(options, e);
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
}
|
|
@@ -103,8 +102,8 @@ export function runAbortable(fn, options) {
|
|
|
103
102
|
}
|
|
104
103
|
resolve(result);
|
|
105
104
|
}
|
|
106
|
-
catch (
|
|
107
|
-
reject(
|
|
105
|
+
catch (e) {
|
|
106
|
+
reject(e);
|
|
108
107
|
}
|
|
109
108
|
finally {
|
|
110
109
|
signal.removeEventListener("abort", abortHandler);
|
|
@@ -132,15 +131,14 @@ export function runAbortable(fn, options) {
|
|
|
132
131
|
};
|
|
133
132
|
}
|
|
134
133
|
catch (e) {
|
|
135
|
-
|
|
136
|
-
(_e = options === null || options === void 0 ? void 0 : options.onError) === null || _e === void 0 ? void 0 : _e.call(options, error);
|
|
134
|
+
(_e = options === null || options === void 0 ? void 0 : options.onError) === null || _e === void 0 ? void 0 : _e.call(options, e);
|
|
137
135
|
if (options === null || options === void 0 ? void 0 : options.throw) {
|
|
138
|
-
throw
|
|
136
|
+
throw e;
|
|
139
137
|
}
|
|
140
138
|
return {
|
|
141
139
|
success: false,
|
|
142
140
|
data: null,
|
|
143
|
-
error:
|
|
141
|
+
error: e
|
|
144
142
|
};
|
|
145
143
|
}
|
|
146
144
|
finally {
|
|
@@ -149,7 +147,7 @@ export function runAbortable(fn, options) {
|
|
|
149
147
|
yield ((_f = deferredFunctions[i]) === null || _f === void 0 ? void 0 : _f.call(deferredFunctions));
|
|
150
148
|
}
|
|
151
149
|
catch (e) {
|
|
152
|
-
(_g = options === null || options === void 0 ? void 0 : options.onError) === null || _g === void 0 ? void 0 : _g.call(options, e
|
|
150
|
+
(_g = options === null || options === void 0 ? void 0 : options.onError) === null || _g === void 0 ? void 0 : _g.call(options, e);
|
|
153
151
|
}
|
|
154
152
|
}
|
|
155
153
|
}
|
|
@@ -168,7 +166,7 @@ export function runEffect(fn, options) {
|
|
|
168
166
|
(_a = deferredFunctions[i]) === null || _a === void 0 ? void 0 : _a.call(deferredFunctions);
|
|
169
167
|
}
|
|
170
168
|
catch (e) {
|
|
171
|
-
(_b = options === null || options === void 0 ? void 0 : options.onError) === null || _b === void 0 ? void 0 : _b.call(options, e
|
|
169
|
+
(_b = options === null || options === void 0 ? void 0 : options.onError) === null || _b === void 0 ? void 0 : _b.call(options, e);
|
|
172
170
|
}
|
|
173
171
|
}
|
|
174
172
|
};
|
|
@@ -182,15 +180,14 @@ export function runEffect(fn, options) {
|
|
|
182
180
|
};
|
|
183
181
|
}
|
|
184
182
|
catch (e) {
|
|
185
|
-
|
|
186
|
-
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error);
|
|
183
|
+
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
|
|
187
184
|
if (options === null || options === void 0 ? void 0 : options.throw) {
|
|
188
|
-
throw
|
|
185
|
+
throw e;
|
|
189
186
|
}
|
|
190
187
|
return {
|
|
191
188
|
success: false,
|
|
192
189
|
data: null,
|
|
193
|
-
error:
|
|
190
|
+
error: e,
|
|
194
191
|
cleanup
|
|
195
192
|
};
|
|
196
193
|
}
|
|
@@ -258,15 +255,14 @@ export function runTimeout(fn, timeoutMs, options) {
|
|
|
258
255
|
return result;
|
|
259
256
|
}
|
|
260
257
|
catch (e) {
|
|
261
|
-
|
|
262
|
-
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error);
|
|
258
|
+
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
|
|
263
259
|
if (options === null || options === void 0 ? void 0 : options.throw) {
|
|
264
|
-
throw
|
|
260
|
+
throw e;
|
|
265
261
|
}
|
|
266
262
|
return {
|
|
267
263
|
success: false,
|
|
268
264
|
data: null,
|
|
269
|
-
error:
|
|
265
|
+
error: e
|
|
270
266
|
};
|
|
271
267
|
}
|
|
272
268
|
});
|
package/dist/types/misc.d.ts
CHANGED
|
@@ -29,3 +29,8 @@ export declare function createExecutableTimeout(callback: () => void, delay?: nu
|
|
|
29
29
|
cancel: () => void;
|
|
30
30
|
};
|
|
31
31
|
export declare function fastLocaleCompare(a: string, b: string): number;
|
|
32
|
+
export declare const BPS_TO_READABLE_UNITS: string[];
|
|
33
|
+
export declare function bpsToReadable(bps: number): string;
|
|
34
|
+
export declare const FORMAT_BYTES_SIZES: string[];
|
|
35
|
+
export declare const POWERS_1024: readonly [1, 1024, 1048576, 1073741824, 1099511627776, 1125899906842624];
|
|
36
|
+
export declare function formatBytes(bytes: number, decimals?: number): string;
|
package/dist/types/run.d.ts
CHANGED
|
@@ -3,21 +3,21 @@ export type Success<T> = {
|
|
|
3
3
|
data: T;
|
|
4
4
|
error: null;
|
|
5
5
|
};
|
|
6
|
-
export type Failure<E =
|
|
6
|
+
export type Failure<E = unknown> = {
|
|
7
7
|
success: false;
|
|
8
8
|
data: null;
|
|
9
9
|
error: E;
|
|
10
10
|
};
|
|
11
|
-
export type Result<T, E =
|
|
11
|
+
export type Result<T, E = unknown> = Success<T> | Failure<E>;
|
|
12
12
|
export type GenericFnResult = number | boolean | string | object | null | undefined | symbol | bigint | void | Promise<number | boolean | string | object | null | undefined | symbol | bigint | void> | Array<number | boolean | string | object | null | undefined | symbol | bigint | void>;
|
|
13
13
|
export type DeferFn = (fn: () => GenericFnResult) => void;
|
|
14
14
|
export type DeferredFunction = () => GenericFnResult;
|
|
15
15
|
export type DeferredFunctions = Array<DeferredFunction>;
|
|
16
16
|
export type Options = {
|
|
17
17
|
throw?: boolean;
|
|
18
|
-
onError?: (err:
|
|
18
|
+
onError?: (err: unknown) => void;
|
|
19
19
|
};
|
|
20
|
-
export declare function run<TResult, E =
|
|
20
|
+
export declare function run<TResult, E = unknown>(fn: (deferFn: DeferFn) => Promise<TResult> | TResult, options?: Options): Promise<Result<TResult, E>>;
|
|
21
21
|
export type AbortableFn = (abortableFn: () => GenericFnResult, opts?: {
|
|
22
22
|
signal?: AbortSignal;
|
|
23
23
|
}) => GenericFnResult;
|
|
@@ -25,7 +25,7 @@ export declare class AbortError extends Error {
|
|
|
25
25
|
constructor(message?: string);
|
|
26
26
|
}
|
|
27
27
|
export declare function abortSignalReason(signal: AbortSignal): string | undefined;
|
|
28
|
-
export declare function runAbortable<TResult, E =
|
|
28
|
+
export declare function runAbortable<TResult, E = unknown>(fn: ({ abortable, defer, signal, controller }: {
|
|
29
29
|
abortable: AbortableFn;
|
|
30
30
|
defer: DeferFn;
|
|
31
31
|
signal: AbortSignal;
|
|
@@ -34,12 +34,12 @@ export declare function runAbortable<TResult, E = Error>(fn: ({ abortable, defer
|
|
|
34
34
|
controller?: AbortController;
|
|
35
35
|
signal?: AbortSignal;
|
|
36
36
|
}): Promise<Result<TResult, E>>;
|
|
37
|
-
export declare function runEffect<TResult, E =
|
|
37
|
+
export declare function runEffect<TResult, E = unknown>(fn: (deferFn: DeferFn) => TResult, options?: Options & {
|
|
38
38
|
automaticCleanup?: boolean;
|
|
39
39
|
}): Result<TResult, E> & {
|
|
40
40
|
cleanup: () => void;
|
|
41
41
|
};
|
|
42
|
-
export declare function runRetry<TResult, E =
|
|
42
|
+
export declare function runRetry<TResult, E = unknown>(fn: (deferFn: DeferFn, attempt: number) => Promise<TResult> | TResult, options?: Options & {
|
|
43
43
|
maxAttempts?: number;
|
|
44
44
|
delayMs?: number;
|
|
45
45
|
backoff?: "linear" | "exponential";
|
|
@@ -49,6 +49,6 @@ export declare function runRetry<TResult, E = Error>(fn: (deferFn: DeferFn, atte
|
|
|
49
49
|
export declare class TimeoutError extends Error {
|
|
50
50
|
constructor(message?: string);
|
|
51
51
|
}
|
|
52
|
-
export declare function runTimeout<TResult, E =
|
|
53
|
-
export declare function runDebounced<TResult, TArgs extends unknown[]>(fn: (defer: DeferFn, ...args: TArgs) => Promise<TResult> | TResult, delayMs: number, options?: Options): (...args: TArgs) => Promise<Result<TResult,
|
|
52
|
+
export declare function runTimeout<TResult, E = unknown>(fn: (deferFn: DeferFn) => Promise<TResult> | TResult, timeoutMs: number, options?: Options): Promise<Result<TResult, E>>;
|
|
53
|
+
export declare function runDebounced<TResult, TArgs extends unknown[]>(fn: (defer: DeferFn, ...args: TArgs) => Promise<TResult> | TResult, delayMs: number, options?: Options): (...args: TArgs) => Promise<Result<TResult, unknown>>;
|
|
54
54
|
export default run;
|