xjs-common 8.2.0 → 8.3.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.
- package/README.md +14 -11
- package/dist/func/u-array.d.ts +1 -1
- package/dist/func/u-file.d.ts +1 -1
- package/dist/func/u-file.js +2 -2
- package/dist/func/u.d.ts +19 -2
- package/dist/func/u.js +33 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[![CI]
|
|
1
|
+
[![npm][npm-badge]][npm-url] [![CI][ci-badge]][ci-url]
|
|
2
2
|
|
|
3
3
|
# Overview
|
|
4
4
|
Library modules for nodejs + typescript that bundled general-purpose implementations.
|
|
@@ -13,7 +13,7 @@ npm i xjs-common
|
|
|
13
13
|
# Code example (only part)
|
|
14
14
|
- Miscellaneous utilities.
|
|
15
15
|
```ts
|
|
16
|
-
import { checkPortAvailability, delay, int2array, UFile, UHttp } from "xjs-common";
|
|
16
|
+
import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xjs-common";
|
|
17
17
|
|
|
18
18
|
(async () => {
|
|
19
19
|
// await 3 seconds.
|
|
@@ -33,13 +33,12 @@ import { checkPortAvailability, delay, int2array, UFile, UHttp } from "xjs-commo
|
|
|
33
33
|
// p1=a&p2=1&p2=2
|
|
34
34
|
console.log(UHttp.concatParamsWithEncoding(null, { p1: "a", p2: ["1", "2"] }));
|
|
35
35
|
|
|
36
|
+
// runs callback with customizable retry.
|
|
37
|
+
retry(async () => { }, { count: 2 });
|
|
38
|
+
|
|
36
39
|
// concatenate file path and make directory if doesn't already exist.
|
|
37
40
|
UFile.mkdir("path/to/dir");
|
|
38
41
|
UFile.mkdir(["path", "to", "dir"]);
|
|
39
|
-
|
|
40
|
-
// ignore if f1 is not file or doesn't exist.
|
|
41
|
-
UFile.mv("path/to/f1", "path/to/f2");
|
|
42
|
-
UFile.mv(["path", "to", "f1"], ["path", "to", "f2"]);
|
|
43
42
|
})();
|
|
44
43
|
```
|
|
45
44
|
- Array utilities.
|
|
@@ -76,8 +75,6 @@ import { UArray } from "xjs-common";
|
|
|
76
75
|
import { UString } from "xjs-common";
|
|
77
76
|
|
|
78
77
|
(() => {
|
|
79
|
-
// true
|
|
80
|
-
console.log(UString.eq("", " "));
|
|
81
78
|
// false
|
|
82
79
|
console.log(UString.eq("", null));
|
|
83
80
|
// true
|
|
@@ -91,9 +88,10 @@ import { UString } from "xjs-common";
|
|
|
91
88
|
// AA
|
|
92
89
|
console.log(UString.idx2az(26));
|
|
93
90
|
|
|
94
|
-
// e.g.
|
|
95
|
-
console.log(UString.
|
|
96
|
-
|
|
91
|
+
// e.g. 20250615053513
|
|
92
|
+
console.log(UString.simpleTime());
|
|
93
|
+
// e.g. 20250615
|
|
94
|
+
console.log(UString.simpleTime({ date: getJSTDate(), unit: TimeUnit.Day }));
|
|
97
95
|
})();
|
|
98
96
|
```
|
|
99
97
|
- Http client enhanced for web scraping.
|
|
@@ -221,3 +219,8 @@ XJS throws error with `code` property which has one of the following numbers.
|
|
|
221
219
|
|
|
222
220
|
# License
|
|
223
221
|
[Apache-License](./LICENSE)
|
|
222
|
+
|
|
223
|
+
[npm-url]: https://npmjs.org/package/xjs-common
|
|
224
|
+
[npm-badge]: https://badgen.net/npm/v/xjs-common
|
|
225
|
+
[ci-url]: https://github.com/begyyal/xjs_commons/actions/workflows/test.yml
|
|
226
|
+
[ci-badge]: https://github.com/begyyal/xjs_commons/actions/workflows/test.yml/badge.svg
|
package/dist/func/u-array.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare namespace UArray {
|
|
|
5
5
|
* @param v1 it uses equal operator for comparing elements, so applying object element is not recommended.
|
|
6
6
|
* @param v2 same as v1.
|
|
7
7
|
* @param sort it uses {@link Array#sort} on v1 and v2 if true. default is true.
|
|
8
|
-
* @param useStrictEqual it uses
|
|
8
|
+
* @param useStrictEqual it uses `===` operator for compareing elements if true, otherwise using `==` operator. default is true.
|
|
9
9
|
*/
|
|
10
10
|
function eq(v1: any[], v2: any[], op: {
|
|
11
11
|
sort?: boolean;
|
package/dist/func/u-file.d.ts
CHANGED
|
@@ -9,5 +9,5 @@ export declare namespace UFile {
|
|
|
9
9
|
function read(p: MaybeArray<string>, encoding: BufferEncoding): string;
|
|
10
10
|
function cp(from: MaybeArray<string>, to: MaybeArray<string>): void;
|
|
11
11
|
function mv(from: MaybeArray<string>, to: MaybeArray<string>): void;
|
|
12
|
-
function joinPath(p: MaybeArray<string>): string;
|
|
12
|
+
function joinPath(...p: MaybeArray<string>[]): string;
|
|
13
13
|
}
|
package/dist/func/u-file.js
CHANGED
|
@@ -74,8 +74,8 @@ var UFile;
|
|
|
74
74
|
fs.renameSync(f, t);
|
|
75
75
|
}
|
|
76
76
|
UFile.mv = mv;
|
|
77
|
-
function joinPath(p) {
|
|
78
|
-
return
|
|
77
|
+
function joinPath(...p) {
|
|
78
|
+
return path.join(...p.flatMap(u_type_1.UType.takeAsArray));
|
|
79
79
|
}
|
|
80
80
|
UFile.joinPath = joinPath;
|
|
81
81
|
})(UFile = exports.UFile || (exports.UFile = {}));
|
package/dist/func/u.d.ts
CHANGED
|
@@ -5,5 +5,22 @@ export declare function int2array(size: number): number[];
|
|
|
5
5
|
export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
|
|
6
6
|
export declare function bitor(...bit: number[]): number;
|
|
7
7
|
export declare function checkPortAvailability(port: number): Promise<boolean>;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
interface RetryOption<T = void | Promise<void>> {
|
|
9
|
+
count?: number;
|
|
10
|
+
logger?: Loggable;
|
|
11
|
+
errorCriterion?: (e: any) => boolean;
|
|
12
|
+
intervalPredicate?: () => T;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* runs callback with customizable retry.
|
|
16
|
+
* @param cb callback to be retried.
|
|
17
|
+
* @param op.count number of retries. default is 1.
|
|
18
|
+
* @param op.logger logger used for exceptions while retrying the process. default is `console` object.
|
|
19
|
+
* @param op.errorCriterion distinguish whether retry is required form exceptions. default is none. (i.e. allways required.)
|
|
20
|
+
* @param op.intervalPredicate predicate that runs between callbacks when retrying.
|
|
21
|
+
*/
|
|
22
|
+
export declare function retry<T>(cb: () => T, op?: RetryOption<void>): T;
|
|
23
|
+
export declare function retry<T>(cb: () => T, op?: RetryOption<Promise<void>>): Promise<T>;
|
|
24
|
+
export declare function retry<T>(cb: () => Promise<T>, op?: RetryOption<void>): Promise<T>;
|
|
25
|
+
export declare function retry<T>(cb: () => Promise<T>, op?: RetryOption<Promise<void>>): Promise<T>;
|
|
26
|
+
export {};
|
package/dist/func/u.js
CHANGED
|
@@ -43,24 +43,43 @@ function checkPortAvailability(port) {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
exports.checkPortAvailability = checkPortAvailability;
|
|
46
|
-
|
|
46
|
+
;
|
|
47
|
+
function retry(cb, op) {
|
|
48
|
+
const l = op?.logger ?? console;
|
|
49
|
+
const initialCount = op?.count ?? 1;
|
|
50
|
+
const handleError = (e) => {
|
|
51
|
+
if (op?.errorCriterion && !op.errorCriterion(e))
|
|
52
|
+
return false;
|
|
53
|
+
l.warn(e);
|
|
54
|
+
return true;
|
|
55
|
+
};
|
|
47
56
|
const prcs = (c) => {
|
|
48
57
|
if (c < 0)
|
|
49
58
|
throw new xjs_err_1.XjsErr(s_errCode, "failure exceeds retryable count.");
|
|
50
59
|
let ret = null;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const innerPrcs = () => {
|
|
61
|
+
try {
|
|
62
|
+
ret = cb();
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
if (handleError(e))
|
|
66
|
+
ret = prcs(c - 1);
|
|
67
|
+
else
|
|
68
|
+
throw e;
|
|
69
|
+
}
|
|
70
|
+
if (ret instanceof Promise) {
|
|
71
|
+
return new Promise((resolve, reject) => ret.then(resolve).catch((e) => { if (handleError(e))
|
|
72
|
+
resolve(prcs(c - 1));
|
|
73
|
+
else
|
|
74
|
+
reject(e); }));
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
return ret;
|
|
78
|
+
};
|
|
79
|
+
if (c < initialCount && op?.intervalPredicate)
|
|
80
|
+
ret = op?.intervalPredicate();
|
|
81
|
+
return ret instanceof Promise ? ret.then(() => innerPrcs()) : innerPrcs();
|
|
63
82
|
};
|
|
64
|
-
return prcs(
|
|
83
|
+
return prcs(initialCount);
|
|
65
84
|
}
|
|
66
85
|
exports.retry = retry;
|