xjs-common 9.1.7 → 10.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 +10 -45
- package/dist/func/u-string.d.ts +3 -0
- package/dist/func/u-string.js +36 -0
- package/dist/func/u.d.ts +1 -3
- package/dist/func/u.js +1 -39
- package/dist/index.d.ts +0 -4
- package/dist/index.js +1 -6
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
[![npm][npm-badge]][npm-url] [![CI][ci-badge]][ci-url]
|
|
2
2
|
|
|
3
3
|
# Overview
|
|
4
|
-
Library modules for
|
|
4
|
+
Library modules for typescript that bundled general-purpose implementations.
|
|
5
5
|
This module is very simple, therefore it has no dependencies.
|
|
6
6
|
|
|
7
7
|
# Install
|
|
8
8
|
```
|
|
9
9
|
npm i xjs-common
|
|
10
10
|
```
|
|
11
|
-
**NOTE**:
|
|
11
|
+
**NOTE**: features related nodejs was moved to [xjs-node](https://github.com/begyyal/xjs_node) at v10.0.0.
|
|
12
|
+
if you has been used the features (e.g. `HttpResolver`, `UFile`), please use the new package instead of this.
|
|
12
13
|
|
|
13
14
|
# Code example (only part)
|
|
14
15
|
### Miscellaneous utilities.
|
|
15
16
|
```ts
|
|
16
|
-
import {
|
|
17
|
+
import { delay, int2array, UHttp, retry, MaybeArray, Loggable } from "xjs-common";
|
|
17
18
|
|
|
18
19
|
(async () => {
|
|
19
20
|
// await 3 seconds.
|
|
@@ -22,8 +23,12 @@ import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xj
|
|
|
22
23
|
// [ 0, 1, 2, 3, 4 ]
|
|
23
24
|
console.log(int2array(5));
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
// runs callback with customizable retry.
|
|
27
|
+
retry(async () => { }, { count: 2 });
|
|
28
|
+
|
|
29
|
+
// utility types
|
|
30
|
+
let maybeArray: MaybeArray<number> = 0; // also number array is applicable.
|
|
31
|
+
let logger: Loggable = console; // object implements log/warn/error is applicable.
|
|
27
32
|
|
|
28
33
|
// true
|
|
29
34
|
console.log(UHttp.isHttpSuccess(204));
|
|
@@ -32,13 +37,6 @@ import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xj
|
|
|
32
37
|
console.log(UHttp.concatParamsWithEncoding("https://aaa.com", { p1: "a", p2: ["1", "2"] }));
|
|
33
38
|
// p1=a&p2=1&p2=2
|
|
34
39
|
console.log(UHttp.concatParamsWithEncoding(null, { p1: "a", p2: ["1", "2"] }));
|
|
35
|
-
|
|
36
|
-
// runs callback with customizable retry.
|
|
37
|
-
retry(async () => { }, { count: 2 });
|
|
38
|
-
|
|
39
|
-
// concatenate file path and make directory if doesn't already exist.
|
|
40
|
-
UFile.mkdir("path/to/dir");
|
|
41
|
-
UFile.mkdir(["path", "to", "dir"]);
|
|
42
40
|
})();
|
|
43
41
|
```
|
|
44
42
|
### Array utilities.
|
|
@@ -94,37 +92,6 @@ import { UString } from "xjs-common";
|
|
|
94
92
|
console.log(UString.simpleTime({ date: getJSTDate(), unit: TimeUnit.Day }));
|
|
95
93
|
})();
|
|
96
94
|
```
|
|
97
|
-
### Enhanced http client.
|
|
98
|
-
```ts
|
|
99
|
-
import { HttpResolver, s_clientMode } from "xjs-common";
|
|
100
|
-
|
|
101
|
-
(async () => {
|
|
102
|
-
// can customize logging. (default is console.)
|
|
103
|
-
// const http = new HttpResolver(chromeMajorVersion, logger);
|
|
104
|
-
const http = new HttpResolver();
|
|
105
|
-
|
|
106
|
-
// implicitly corresponds to cookies and redirect, and do randomization.
|
|
107
|
-
let res = await http.get("https://begyyal.net");
|
|
108
|
-
const { payload, headers } = res;
|
|
109
|
-
|
|
110
|
-
// use proxy by passing the configuration.
|
|
111
|
-
const proxy = { server: "proxy.sample.com", port: 8080, auth: { name: "prx", pass: "****" } }
|
|
112
|
-
res = await http.post("https://begyyal.net", { proxy });
|
|
113
|
-
|
|
114
|
-
// switch tls ciphers order pattern by passing clientMode. (default is random between chrome or firefox.)
|
|
115
|
-
res = await http.get("https://begyyal.net", { mode: s_clientMode.chrome });
|
|
116
|
-
|
|
117
|
-
// download a file when [Content-Disposition: attachment] exists in the response.
|
|
118
|
-
await http.get("https://begyyal.net/a.txt", { downloadPath: "/path/to/store" });
|
|
119
|
-
|
|
120
|
-
// if you want to keep some states of requests (and suppress to randomize), it can create new context to do.
|
|
121
|
-
const context = http.newContext();
|
|
122
|
-
res = await context.get("https://begyyal.net/1");
|
|
123
|
-
// this request sends with cookies that is set by precedent requests.
|
|
124
|
-
// in POST, payload is treated as json if it is an object.
|
|
125
|
-
res = await context.post("https://begyyal.net/2", { a: "b" });
|
|
126
|
-
})();
|
|
127
|
-
```
|
|
128
95
|
### Mark method as transaction.
|
|
129
96
|
**NOTE**: this feature uses decorator, so requires `"experimentalDecorators": true` in tsconfig.
|
|
130
97
|
```ts
|
|
@@ -216,9 +183,7 @@ XJS throws error with `code` property which has one of the following numbers.
|
|
|
216
183
|
|10|`func/u`|
|
|
217
184
|
|20|`func/u-string`|
|
|
218
185
|
|30|`func/u-type` (include `func/decorator/d-type`) |
|
|
219
|
-
|40|`func/u-file` |
|
|
220
186
|
|100|`func/decorator/transaction`|
|
|
221
|
-
|200|`prcs/http/http-resolver`|
|
|
222
187
|
|
|
223
188
|
# License
|
|
224
189
|
[Apache-License](./LICENSE)
|
package/dist/func/u-string.d.ts
CHANGED
|
@@ -15,6 +15,9 @@ export declare namespace UString {
|
|
|
15
15
|
function generateRandomString(len: number): string;
|
|
16
16
|
function idx2az(idx: number): string;
|
|
17
17
|
function az2idx(az: string): number;
|
|
18
|
+
function asJpy(amount: number): string;
|
|
19
|
+
function asUsd(amount: number): string;
|
|
20
|
+
function asPercentage(amount: number): string;
|
|
18
21
|
function is_yyyy(v: string): boolean;
|
|
19
22
|
function is_yyyyMM(v: string): boolean;
|
|
20
23
|
function is_yyyyMMdd(v: string): boolean;
|
package/dist/func/u-string.js
CHANGED
|
@@ -59,6 +59,42 @@ var UString;
|
|
|
59
59
|
.map((idx, i) => (idx + 1) * (26 ** i)).reduce((v1, v2) => v1 + v2) - 1;
|
|
60
60
|
}
|
|
61
61
|
UString.az2idx = az2idx;
|
|
62
|
+
function asAmount(amount, unit) {
|
|
63
|
+
const int2dec = amount.toString().split(".");
|
|
64
|
+
const etni = int2dec[0].split("").reverse().join("");
|
|
65
|
+
let fetni = "";
|
|
66
|
+
const max = Math.ceil(etni.length / 3);
|
|
67
|
+
for (let i = 0; i < max; i++) {
|
|
68
|
+
if (i === max - 1)
|
|
69
|
+
fetni += etni.substring(i * 3);
|
|
70
|
+
else
|
|
71
|
+
fetni += (etni.substring(i * 3, (i + 1) * 3) + ",");
|
|
72
|
+
}
|
|
73
|
+
const finte = unit + fetni.split("").reverse().join("");
|
|
74
|
+
if (int2dec.length === 1)
|
|
75
|
+
return finte;
|
|
76
|
+
else
|
|
77
|
+
return finte + "." + int2dec[1];
|
|
78
|
+
}
|
|
79
|
+
function asJpy(amount) {
|
|
80
|
+
return u_type_1.UType.isEmpty(amount) ? "" : asAmount(Math.floor(amount), "¥");
|
|
81
|
+
}
|
|
82
|
+
UString.asJpy = asJpy;
|
|
83
|
+
function asUsd(amount) {
|
|
84
|
+
return u_type_1.UType.isEmpty(amount) ? "" : asAmount(Number(amount.toFixed(2)), "$");
|
|
85
|
+
}
|
|
86
|
+
UString.asUsd = asUsd;
|
|
87
|
+
function asPercentage(amount) {
|
|
88
|
+
if (u_type_1.UType.isEmpty(amount))
|
|
89
|
+
return "";
|
|
90
|
+
let percent = (amount * 100).toFixed(2);
|
|
91
|
+
while (percent.endsWith("0"))
|
|
92
|
+
percent = percent.substring(0, percent.length - 1);
|
|
93
|
+
if (percent.endsWith("."))
|
|
94
|
+
percent = percent.substring(0, percent.length - 1);
|
|
95
|
+
return percent + "%";
|
|
96
|
+
}
|
|
97
|
+
UString.asPercentage = asPercentage;
|
|
62
98
|
function is_yyyy(v) {
|
|
63
99
|
return !!v?.match(/^[1-9]\d{3}$/);
|
|
64
100
|
}
|
package/dist/func/u.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { Loggable
|
|
1
|
+
import { Loggable } from "../const/types";
|
|
2
2
|
export declare function getJSTDate(d?: Date): Date;
|
|
3
3
|
export declare function delay(sec: number): Promise<void>;
|
|
4
4
|
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
|
-
export declare function checkPortAvailability(port: number): Promise<boolean>;
|
|
8
7
|
export interface RetryOption<T = void | Promise<void>> {
|
|
9
8
|
count?: number;
|
|
10
9
|
logger?: Loggable;
|
|
@@ -23,4 +22,3 @@ export declare function retry<T>(cb: () => T, op?: RetryOption<void>): T;
|
|
|
23
22
|
export declare function retry<T>(cb: () => T, op?: RetryOption<Promise<void>>): Promise<T>;
|
|
24
23
|
export declare function retry<T>(cb: () => Promise<T>, op?: RetryOption<void>): Promise<T>;
|
|
25
24
|
export declare function retry<T>(cb: () => Promise<T>, op?: RetryOption<Promise<void>>): Promise<T>;
|
|
26
|
-
export declare function joinPath(...p: MaybeArray<string>[]): string;
|
package/dist/func/u.js
CHANGED
|
@@ -1,32 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
const path = __importStar(require("path"));
|
|
3
|
+
exports.retry = exports.bitor = exports.array2map = exports.int2array = exports.delay = exports.getJSTDate = void 0;
|
|
28
4
|
const xjs_err_1 = require("../obj/xjs-err");
|
|
29
|
-
const u_type_1 = require("./u-type");
|
|
30
5
|
const s_errCode = 10;
|
|
31
6
|
function getJSTDate(d) {
|
|
32
7
|
return new Date((d ? d.getTime() : Date.now()) + 9 * 60 * 60 * 1000);
|
|
@@ -59,15 +34,6 @@ function bitor(...bit) {
|
|
|
59
34
|
return bit.reduce((a, b) => a | b);
|
|
60
35
|
}
|
|
61
36
|
exports.bitor = bitor;
|
|
62
|
-
function checkPortAvailability(port) {
|
|
63
|
-
return new Promise(resolve => {
|
|
64
|
-
const server = require('net').createServer();
|
|
65
|
-
server.once('error', () => resolve(false))
|
|
66
|
-
.once('listening', () => { server.close(); resolve(true); })
|
|
67
|
-
.listen(port);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
exports.checkPortAvailability = checkPortAvailability;
|
|
71
37
|
;
|
|
72
38
|
function retry(cb, op) {
|
|
73
39
|
const l = op?.logger ?? console;
|
|
@@ -108,7 +74,3 @@ function retry(cb, op) {
|
|
|
108
74
|
return prcs(initialCount);
|
|
109
75
|
}
|
|
110
76
|
exports.retry = retry;
|
|
111
|
-
function joinPath(...p) {
|
|
112
|
-
return path.join(...p.flatMap(u_type_1.UType.takeAsArray));
|
|
113
|
-
}
|
|
114
|
-
exports.joinPath = joinPath;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,12 +7,8 @@ export * from "./func/u";
|
|
|
7
7
|
export * from "./func/u-obj";
|
|
8
8
|
export * from "./func/u-string";
|
|
9
9
|
export * from "./func/u-array";
|
|
10
|
-
export * from "./func/u-file";
|
|
11
10
|
export * from "./func/u-http";
|
|
12
11
|
export * from "./func/u-type";
|
|
13
12
|
export * from "./func/decorator/transaction";
|
|
14
13
|
export { DType } from "./func/decorator/d-type";
|
|
15
14
|
export * from "./obj/xjs-err";
|
|
16
|
-
export { HttpResolver } from "./prcs/http/http-resolver";
|
|
17
|
-
export { s_clientMode } from "./prcs/http/http-resolver-context";
|
|
18
|
-
export { IHttpClient } from "./prcs/http/i-http-client";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.DType = void 0;
|
|
18
18
|
__exportStar(require("./const/types"), exports);
|
|
19
19
|
__exportStar(require("./const/time-unit"), exports);
|
|
20
20
|
__exportStar(require("./const/gender"), exports);
|
|
@@ -24,14 +24,9 @@ __exportStar(require("./func/u"), exports);
|
|
|
24
24
|
__exportStar(require("./func/u-obj"), exports);
|
|
25
25
|
__exportStar(require("./func/u-string"), exports);
|
|
26
26
|
__exportStar(require("./func/u-array"), exports);
|
|
27
|
-
__exportStar(require("./func/u-file"), exports);
|
|
28
27
|
__exportStar(require("./func/u-http"), exports);
|
|
29
28
|
__exportStar(require("./func/u-type"), exports);
|
|
30
29
|
__exportStar(require("./func/decorator/transaction"), exports);
|
|
31
30
|
var d_type_1 = require("./func/decorator/d-type");
|
|
32
31
|
Object.defineProperty(exports, "DType", { enumerable: true, get: function () { return d_type_1.DType; } });
|
|
33
32
|
__exportStar(require("./obj/xjs-err"), exports);
|
|
34
|
-
var http_resolver_1 = require("./prcs/http/http-resolver");
|
|
35
|
-
Object.defineProperty(exports, "HttpResolver", { enumerable: true, get: function () { return http_resolver_1.HttpResolver; } });
|
|
36
|
-
var http_resolver_context_1 = require("./prcs/http/http-resolver-context");
|
|
37
|
-
Object.defineProperty(exports, "s_clientMode", { enumerable: true, get: function () { return http_resolver_context_1.s_clientMode; } });
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xjs-common",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "library modules for
|
|
3
|
+
"version": "10.1.0",
|
|
4
|
+
"description": "library modules for typescript that bundled general-purpose implementations.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/begyyal/xjs_commons"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [
|
|
10
|
-
"
|
|
10
|
+
"javascript",
|
|
11
11
|
"typescript",
|
|
12
12
|
"utility"
|
|
13
13
|
],
|
|
@@ -24,8 +24,7 @@
|
|
|
24
24
|
"main": "./dist/index",
|
|
25
25
|
"types": "./dist/index.d.ts",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/node": "^20.3.1",
|
|
28
27
|
"ts-node": "^10.9.1",
|
|
29
28
|
"typescript": "^4.9.5"
|
|
30
29
|
}
|
|
31
|
-
}
|
|
30
|
+
}
|