xjs-common 8.1.0 → 8.3.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 +9 -10
- package/dist/func/u-array.d.ts +1 -1
- package/dist/func/u-array.js +1 -1
- package/dist/func/u-file.d.ts +3 -1
- package/dist/func/u-file.js +27 -15
- package/dist/func/u.d.ts +20 -0
- package/dist/func/u.js +41 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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.
|
|
@@ -215,6 +213,7 @@ XJS throws error with `code` property which has one of the following numbers.
|
|
|
215
213
|
|10|`func/u`|
|
|
216
214
|
|20|`func/u-string`|
|
|
217
215
|
|30|`func/u-type` (include `func/decorator/d-type`) |
|
|
216
|
+
|40|`func/u-file` |
|
|
218
217
|
|100|`func/decorator/transaction`|
|
|
219
218
|
|200|`prcs/http/http-resolver`|
|
|
220
219
|
|
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-array.js
CHANGED
|
@@ -21,7 +21,7 @@ var UArray;
|
|
|
21
21
|
return [];
|
|
22
22
|
if (op?.k)
|
|
23
23
|
return Array.from((0, u_1.array2map)(array, e => e[op.k]).values()).map(a => op?.takeLast ? a.pop() : a.shift());
|
|
24
|
-
const a = op?.takeLast ? [...array].reverse() : array;
|
|
24
|
+
const a = op?.takeLast ? [...array].reverse() : [...array];
|
|
25
25
|
const p = op?.predicate ?? ((v1, v2) => v1 == v2);
|
|
26
26
|
const result = [a.shift()];
|
|
27
27
|
a.forEach(v => result.some(v2 => p(v, v2)) ? {} : result.push(v));
|
package/dist/func/u-file.d.ts
CHANGED
|
@@ -3,9 +3,11 @@ import { MaybeArray } from "../const/types";
|
|
|
3
3
|
export declare namespace UFile {
|
|
4
4
|
function mkdir(p: MaybeArray<string>): boolean;
|
|
5
5
|
function write(p: MaybeArray<string>, c: string): void;
|
|
6
|
-
function
|
|
6
|
+
function rm(p: MaybeArray<string>): void;
|
|
7
|
+
function exists(p: MaybeArray<string>): boolean;
|
|
7
8
|
function read(p: MaybeArray<string>): Buffer;
|
|
8
9
|
function read(p: MaybeArray<string>, encoding: BufferEncoding): string;
|
|
9
10
|
function cp(from: MaybeArray<string>, to: MaybeArray<string>): void;
|
|
10
11
|
function mv(from: MaybeArray<string>, to: MaybeArray<string>): void;
|
|
12
|
+
function joinPath(p: MaybeArray<string>): string;
|
|
11
13
|
}
|
package/dist/func/u-file.js
CHANGED
|
@@ -27,43 +27,55 @@ exports.UFile = void 0;
|
|
|
27
27
|
const fs = __importStar(require("fs"));
|
|
28
28
|
const path = __importStar(require("path"));
|
|
29
29
|
const u_type_1 = require("./u-type");
|
|
30
|
+
const xjs_err_1 = require("../obj/xjs-err");
|
|
31
|
+
const s_errCode = 40;
|
|
30
32
|
var UFile;
|
|
31
33
|
(function (UFile) {
|
|
32
34
|
function mkdir(p) {
|
|
33
|
-
const dirPath =
|
|
35
|
+
const dirPath = joinPath(p);
|
|
34
36
|
const e = fs.existsSync(dirPath);
|
|
35
|
-
if (!e
|
|
37
|
+
if (!e)
|
|
36
38
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
39
|
+
else if (!fs.statSync(dirPath).isDirectory())
|
|
40
|
+
throw new xjs_err_1.XjsErr(s_errCode, "Already exists a file (not directory) on the path.");
|
|
37
41
|
return !e;
|
|
38
42
|
}
|
|
39
43
|
UFile.mkdir = mkdir;
|
|
40
44
|
function write(p, c) {
|
|
41
|
-
fs.writeFileSync(
|
|
45
|
+
fs.writeFileSync(joinPath(p), c);
|
|
42
46
|
}
|
|
43
47
|
UFile.write = write;
|
|
44
|
-
function
|
|
45
|
-
|
|
48
|
+
function rm(p) {
|
|
49
|
+
fs.rmSync(joinPath(p), { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
UFile.rm = rm;
|
|
52
|
+
function exists(p) {
|
|
53
|
+
return fs.existsSync(joinPath(p));
|
|
46
54
|
}
|
|
47
55
|
UFile.exists = exists;
|
|
48
56
|
function read(p, encoding) {
|
|
49
|
-
|
|
57
|
+
const f = joinPath(p);
|
|
58
|
+
if (!fs.existsSync(f))
|
|
59
|
+
throw new xjs_err_1.XjsErr(s_errCode, `No file found => ${f}`);
|
|
60
|
+
return fs.readFileSync(f, encoding);
|
|
50
61
|
}
|
|
51
62
|
UFile.read = read;
|
|
52
63
|
function cp(from, to) {
|
|
53
|
-
const f =
|
|
54
|
-
if (fs.existsSync(f))
|
|
55
|
-
|
|
64
|
+
const f = joinPath(from), t = joinPath(to);
|
|
65
|
+
if (!fs.existsSync(f))
|
|
66
|
+
throw new xjs_err_1.XjsErr(s_errCode, `No file found => ${f}`);
|
|
67
|
+
fs.copyFileSync(f, t);
|
|
56
68
|
}
|
|
57
69
|
UFile.cp = cp;
|
|
58
70
|
function mv(from, to) {
|
|
59
|
-
const f =
|
|
60
|
-
if (fs.existsSync(f)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
71
|
+
const f = joinPath(from), t = joinPath(to);
|
|
72
|
+
if (!fs.existsSync(f))
|
|
73
|
+
throw new xjs_err_1.XjsErr(s_errCode, `No file found => ${f}`);
|
|
74
|
+
fs.renameSync(f, t);
|
|
64
75
|
}
|
|
65
76
|
UFile.mv = mv;
|
|
66
|
-
function
|
|
77
|
+
function joinPath(p) {
|
|
67
78
|
return u_type_1.UType.isString(p) ? p : path.join(...p);
|
|
68
79
|
}
|
|
80
|
+
UFile.joinPath = joinPath;
|
|
69
81
|
})(UFile = exports.UFile || (exports.UFile = {}));
|
package/dist/func/u.d.ts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
|
+
import { Loggable } from "../const/types";
|
|
1
2
|
export declare function getJSTDate(d?: Date): Date;
|
|
2
3
|
export declare function delay(sec: number): Promise<void>;
|
|
3
4
|
export declare function int2array(size: number): number[];
|
|
4
5
|
export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
|
|
5
6
|
export declare function bitor(...bit: number[]): number;
|
|
6
7
|
export declare function checkPortAvailability(port: number): Promise<boolean>;
|
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.checkPortAvailability = exports.bitor = exports.array2map = exports.int2array = exports.delay = exports.getJSTDate = void 0;
|
|
3
|
+
exports.retry = exports.checkPortAvailability = exports.bitor = exports.array2map = exports.int2array = exports.delay = exports.getJSTDate = void 0;
|
|
4
4
|
const xjs_err_1 = require("../obj/xjs-err");
|
|
5
5
|
const s_errCode = 10;
|
|
6
6
|
function getJSTDate(d) {
|
|
@@ -43,3 +43,43 @@ function checkPortAvailability(port) {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
exports.checkPortAvailability = checkPortAvailability;
|
|
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
|
+
};
|
|
56
|
+
const prcs = (c) => {
|
|
57
|
+
if (c < 0)
|
|
58
|
+
throw new xjs_err_1.XjsErr(s_errCode, "failure exceeds retryable count.");
|
|
59
|
+
let ret = null;
|
|
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();
|
|
82
|
+
};
|
|
83
|
+
return prcs(initialCount);
|
|
84
|
+
}
|
|
85
|
+
exports.retry = retry;
|