xjs-node 1.0.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.
Files changed (47) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +79 -0
  3. package/dist/const/ccy.d.ts +18 -0
  4. package/dist/const/ccy.js +20 -0
  5. package/dist/const/gender.d.ts +4 -0
  6. package/dist/const/gender.js +8 -0
  7. package/dist/const/http-method.d.ts +8 -0
  8. package/dist/const/http-method.js +12 -0
  9. package/dist/const/time-unit.d.ts +9 -0
  10. package/dist/const/time-unit.js +13 -0
  11. package/dist/const/types.d.ts +24 -0
  12. package/dist/const/types.js +14 -0
  13. package/dist/func/decorator/d-type.d.ts +24 -0
  14. package/dist/func/decorator/d-type.js +71 -0
  15. package/dist/func/decorator/d-validate.d.ts +20 -0
  16. package/dist/func/decorator/d-validate.js +64 -0
  17. package/dist/func/decorator/transaction.d.ts +7 -0
  18. package/dist/func/decorator/transaction.js +36 -0
  19. package/dist/func/u-array.d.ts +40 -0
  20. package/dist/func/u-array.js +70 -0
  21. package/dist/func/u-file.d.ts +45 -0
  22. package/dist/func/u-file.js +161 -0
  23. package/dist/func/u-http.d.ts +7 -0
  24. package/dist/func/u-http.js +25 -0
  25. package/dist/func/u-obj.d.ts +18 -0
  26. package/dist/func/u-obj.js +44 -0
  27. package/dist/func/u-string.d.ts +24 -0
  28. package/dist/func/u-string.js +86 -0
  29. package/dist/func/u-type.d.ts +25 -0
  30. package/dist/func/u-type.js +58 -0
  31. package/dist/func/u.d.ts +3 -0
  32. package/dist/func/u.js +42 -0
  33. package/dist/index.d.ts +5 -0
  34. package/dist/index.js +23 -0
  35. package/dist/obj/type-exp.d.ts +8 -0
  36. package/dist/obj/type-exp.js +23 -0
  37. package/dist/obj/xjs-err.d.ts +5 -0
  38. package/dist/obj/xjs-err.js +13 -0
  39. package/dist/prcs/http/http-resolver-context.d.ts +64 -0
  40. package/dist/prcs/http/http-resolver-context.js +338 -0
  41. package/dist/prcs/http/http-resolver.d.ts +36 -0
  42. package/dist/prcs/http/http-resolver.js +53 -0
  43. package/dist/prcs/http/i-http-client.d.ts +63 -0
  44. package/dist/prcs/http/i-http-client.js +2 -0
  45. package/dist/prcs/http-resolver.d.ts +53 -0
  46. package/dist/prcs/http-resolver.js +255 -0
  47. package/package.json +34 -0
@@ -0,0 +1,40 @@
1
+ export declare namespace UArray {
2
+ /**
3
+ * compares two arrays to valuate equality.
4
+ * if one side is null or undefined, it returns true when other side is the same.
5
+ * @param v1 it uses equal operator for comparing elements, so applying object element is not recommended.
6
+ * @param v2 same as v1.
7
+ * @param sort it uses {@link Array#sort} on v1 and v2 if true. default is true.
8
+ * @param useStrictEqual it uses `===` operator for compareing elements if true, otherwise using `==` operator. default is true.
9
+ */
10
+ function eq(v1: any[], v2: any[], op: {
11
+ sort?: boolean;
12
+ useStrictEqual: false;
13
+ }): boolean;
14
+ function eq<T>(v1: T[], v2: T[], op: {
15
+ sort?: boolean;
16
+ useStrictEqual: true;
17
+ }): boolean;
18
+ function eq<T>(v1: T[], v2: T[], op: {
19
+ sort?: boolean;
20
+ }): boolean;
21
+ function eq<T>(v1: T[], v2: T[]): boolean;
22
+ /**
23
+ * returns array which is removed duplicate of elements.
24
+ * this doesn't mutate the param.
25
+ */
26
+ function distinct<T>(array: T[]): T[];
27
+ function distinct<T>(array: T[], op: {
28
+ k: keyof T;
29
+ takeLast?: boolean;
30
+ }): T[];
31
+ function distinct<T>(array: T[], op: {
32
+ predicate: (v1: T, v2: T) => boolean;
33
+ takeLast?: boolean;
34
+ }): T[];
35
+ function chop<T>(array: T[], len: number): T[][];
36
+ function remove<T>(array: T[], v: T): void;
37
+ function randomPick<T>(array: T[], takeout?: boolean): T;
38
+ function shuffle<T>(array: T[]): T[];
39
+ function takeOut<T>(array: T[], filter: (v: T, i?: number) => boolean): T[];
40
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UArray = void 0;
4
+ const u_1 = require("./u");
5
+ var UArray;
6
+ (function (UArray) {
7
+ function eq(v1, v2, op = {}) {
8
+ const { sort, useStrictEqual } = Object.assign({ sort: true, useStrictEqual: true }, op);
9
+ if (v1 && !v2 || !v1 && v2)
10
+ return false;
11
+ if (!v1)
12
+ return true;
13
+ if (v1.length !== v2.length)
14
+ return false;
15
+ const a = sort ? [...v1].sort() : v1, b = sort ? [...v2].sort() : v2;
16
+ return a.every((v, i) => useStrictEqual ? v === b[i] : v == b[i]);
17
+ }
18
+ UArray.eq = eq;
19
+ function distinct(array, op) {
20
+ if (!array || array.length === 0)
21
+ return [];
22
+ if (op?.k)
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];
25
+ const p = op?.predicate ?? ((v1, v2) => v1 == v2);
26
+ const result = [a.shift()];
27
+ a.forEach(v => result.some(v2 => p(v, v2)) ? {} : result.push(v));
28
+ return result;
29
+ }
30
+ UArray.distinct = distinct;
31
+ function chop(array, len) {
32
+ return [...Array(Math.ceil(array.length / len)).keys()]
33
+ .map(i => {
34
+ let endIdx = (i + 1) * len;
35
+ if (endIdx > array.length)
36
+ endIdx = array.length;
37
+ return array.slice(i * len, endIdx);
38
+ });
39
+ }
40
+ UArray.chop = chop;
41
+ function remove(array, v) {
42
+ const idx = array.indexOf(v);
43
+ if (idx !== -1)
44
+ array.splice(idx, 1);
45
+ }
46
+ UArray.remove = remove;
47
+ function randomPick(array, takeout = true) {
48
+ const i = Math.floor(array.length * Math.random());
49
+ const r = array[i];
50
+ if (takeout)
51
+ array.splice(i, 1);
52
+ return r;
53
+ }
54
+ UArray.randomPick = randomPick;
55
+ function shuffle(array) {
56
+ const cp = [...array];
57
+ return (0, u_1.int2array)(array.length).map(_ => randomPick(cp));
58
+ }
59
+ UArray.shuffle = shuffle;
60
+ function takeOut(array, filter) {
61
+ const result = [];
62
+ for (let i = array.length - 1; i >= 0; i--)
63
+ if (filter(array[i], i)) {
64
+ result.unshift(array[i]);
65
+ array.splice(i, 1);
66
+ }
67
+ return result;
68
+ }
69
+ UArray.takeOut = takeOut;
70
+ })(UArray = exports.UArray || (exports.UArray = {}));
@@ -0,0 +1,45 @@
1
+ /// <reference types="node" />
2
+ import { MaybeArray } from "xjs-common";
3
+ interface FileStatus {
4
+ isFile(): boolean;
5
+ isDirectory(): boolean;
6
+ isBlockDevice(): boolean;
7
+ isCharacterDevice(): boolean;
8
+ isSymbolicLink(): boolean;
9
+ isFIFO(): boolean;
10
+ isSocket(): boolean;
11
+ }
12
+ export declare namespace UFile {
13
+ function mkdir(p: MaybeArray<string>): boolean;
14
+ function write(p: MaybeArray<string>, c: string): void;
15
+ /**
16
+ * remove a file. no error if the file to be removed doesn't exist.
17
+ */
18
+ function rm(p: MaybeArray<string>): void;
19
+ function exists(p: MaybeArray<string>): boolean;
20
+ /**
21
+ * return a file status. if the file of the status doesn't exist, this returns `null`.
22
+ */
23
+ function status(p: MaybeArray<string>): FileStatus;
24
+ function read(p: MaybeArray<string>): Buffer;
25
+ function read(p: MaybeArray<string>, encoding: BufferEncoding): string;
26
+ function cp(from: MaybeArray<string>, to: MaybeArray<string>): void;
27
+ function mv(from: MaybeArray<string>, to: MaybeArray<string>): void;
28
+ function ls(p: MaybeArray<string>): string[];
29
+ /**
30
+ * check availability to export a file with specified directory and file name.
31
+ * if it doesn't, retry to check after appending incremental number (e.g. `.1`) to the filename.
32
+ * @param dir destination directory path.
33
+ * @param fname file name wanna export to.
34
+ * @returns exportable file path.
35
+ */
36
+ function reserveFilePath(dir: MaybeArray<string>, fname: string): string;
37
+ /**
38
+ * decompress zip file. this depends on os enviroment due to using the os command.
39
+ * currently this supports only windows (installed `tar` or `unzip` in gnu compiler) and linux systems (installed `unzip`).
40
+ * @param zipPath zip file to be unzipped.
41
+ * @param destDir directory that the decompress files export to.
42
+ */
43
+ function unzip(zipPath: MaybeArray<string>, destDir?: MaybeArray<string>): void;
44
+ }
45
+ export {};
@@ -0,0 +1,161 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.UFile = void 0;
27
+ const child_process_1 = require("child_process");
28
+ const fs = __importStar(require("fs"));
29
+ const xjs_common_1 = require("xjs-common");
30
+ const u_1 = require("./u");
31
+ const s_errCode = 1040;
32
+ var UFile;
33
+ (function (UFile) {
34
+ function mkdir(p) {
35
+ const dirPath = (0, u_1.joinPath)(p);
36
+ const e = fs.existsSync(dirPath);
37
+ if (!e)
38
+ fs.mkdirSync(dirPath, { recursive: true });
39
+ else if (!fs.statSync(dirPath).isDirectory())
40
+ throw new xjs_common_1.XjsErr(s_errCode, "Already exists a file (not directory) on the path.");
41
+ return !e;
42
+ }
43
+ UFile.mkdir = mkdir;
44
+ function write(p, c) {
45
+ fs.writeFileSync((0, u_1.joinPath)(p), c);
46
+ }
47
+ UFile.write = write;
48
+ /**
49
+ * remove a file. no error if the file to be removed doesn't exist.
50
+ */
51
+ function rm(p) {
52
+ const pt = (0, u_1.joinPath)(p);
53
+ if (fs.existsSync(pt))
54
+ fs.rmSync(pt, { recursive: true });
55
+ }
56
+ UFile.rm = rm;
57
+ function exists(p) {
58
+ return !!p && fs.existsSync((0, u_1.joinPath)(p));
59
+ }
60
+ UFile.exists = exists;
61
+ /**
62
+ * return a file status. if the file of the status doesn't exist, this returns `null`.
63
+ */
64
+ function status(p) {
65
+ const pt = (0, u_1.joinPath)(p);
66
+ return fs.existsSync(pt) ? fs.statSync(pt) : null;
67
+ }
68
+ UFile.status = status;
69
+ function read(p, encoding) {
70
+ const f = (0, u_1.joinPath)(p);
71
+ if (!fs.existsSync(f))
72
+ throw new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`);
73
+ return fs.readFileSync(f, encoding);
74
+ }
75
+ UFile.read = read;
76
+ function cp(from, to) {
77
+ const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
78
+ if (!fs.existsSync(f))
79
+ throw new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`);
80
+ fs.copyFileSync(f, t);
81
+ }
82
+ UFile.cp = cp;
83
+ function mv(from, to) {
84
+ const f = (0, u_1.joinPath)(from), t = (0, u_1.joinPath)(to);
85
+ if (!fs.existsSync(f))
86
+ throw new xjs_common_1.XjsErr(s_errCode, `No file found => ${f}`);
87
+ fs.renameSync(f, t);
88
+ }
89
+ UFile.mv = mv;
90
+ function ls(p) {
91
+ const pt = (0, u_1.joinPath)(p);
92
+ if (!pt || !fs.statSync(pt).isDirectory())
93
+ throw new xjs_common_1.XjsErr(s_errCode, "Specified path for ls is not directory.");
94
+ return fs.readdirSync(pt);
95
+ }
96
+ UFile.ls = ls;
97
+ /**
98
+ * check availability to export a file with specified directory and file name.
99
+ * if it doesn't, retry to check after appending incremental number (e.g. `.1`) to the filename.
100
+ * @param dir destination directory path.
101
+ * @param fname file name wanna export to.
102
+ * @returns exportable file path.
103
+ */
104
+ function reserveFilePath(dir, fname) {
105
+ const pt = (0, u_1.joinPath)(dir);
106
+ if (!pt || !fs.statSync(pt).isDirectory())
107
+ throw new xjs_common_1.XjsErr(s_errCode, "Specified directory path is not directory.");
108
+ if (!fname || fname.match(/[\\/:*?"<>|]/))
109
+ throw new xjs_common_1.XjsErr(s_errCode, "Specified filename is invalid due to empty or including disallowed characters.");
110
+ let dest = (0, u_1.joinPath)(pt, fname), i = 1;
111
+ while (fs.existsSync(dest))
112
+ dest = (0, u_1.joinPath)(pt, `${fname}.${i++}`);
113
+ return dest;
114
+ }
115
+ UFile.reserveFilePath = reserveFilePath;
116
+ /**
117
+ * decompress zip file. this depends on os enviroment due to using the os command.
118
+ * currently this supports only windows (installed `tar` or `unzip` in gnu compiler) and linux systems (installed `unzip`).
119
+ * @param zipPath zip file to be unzipped.
120
+ * @param destDir directory that the decompress files export to.
121
+ */
122
+ function unzip(zipPath, destDir) {
123
+ if (!exists(zipPath))
124
+ throw new xjs_common_1.XjsErr(s_errCode, "There is no file on the zip path.");
125
+ if (!!destDir && !exists(destDir))
126
+ throw new xjs_common_1.XjsErr(s_errCode, "The destination directory is not found.");
127
+ let cmd = "unzip", options = null, availableCmd = true;
128
+ if (destDir)
129
+ options = `-d "${destDir}"`;
130
+ const check = () => { try {
131
+ (0, child_process_1.execSync)(`${cmd} --help`, { stdio: "ignore" });
132
+ }
133
+ catch {
134
+ availableCmd = false;
135
+ } };
136
+ check();
137
+ if (process.platform === "win32") {
138
+ if (!availableCmd) {
139
+ cmd = "tar";
140
+ options = "-xf";
141
+ availableCmd = true;
142
+ if (destDir)
143
+ options = `-C "${destDir}" ${options}`;
144
+ check();
145
+ }
146
+ }
147
+ else if (process.platform === "linux") {
148
+ }
149
+ else
150
+ throw new xjs_common_1.XjsErr(s_errCode, "The os running on is not supported for xjs unzip.");
151
+ if (!availableCmd)
152
+ throw new xjs_common_1.XjsErr(s_errCode, `"${cmd}" command is not installed.`);
153
+ try {
154
+ (0, child_process_1.execSync)([cmd, options, `"${zipPath}"`].filter(e => e).join(" "), { stdio: "ignore" });
155
+ }
156
+ catch (e) {
157
+ throw new xjs_common_1.XjsErr(s_errCode, "Something went wrong at unzip.", e);
158
+ }
159
+ }
160
+ UFile.unzip = unzip;
161
+ })(UFile = exports.UFile || (exports.UFile = {}));
@@ -0,0 +1,7 @@
1
+ export declare namespace UHttp {
2
+ function isHttpSuccess(statusCode: number): boolean;
3
+ function statusCategoryOf(statusCode: number): number;
4
+ function concatParamsWithEncoding(end: string, params: {
5
+ [k: string]: string | string[];
6
+ }): string;
7
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UHttp = void 0;
4
+ var UHttp;
5
+ (function (UHttp) {
6
+ function isHttpSuccess(statusCode) {
7
+ return statusCategoryOf(statusCode) === 2;
8
+ }
9
+ UHttp.isHttpSuccess = isHttpSuccess;
10
+ function statusCategoryOf(statusCode) {
11
+ return Math.floor(statusCode / 100);
12
+ }
13
+ UHttp.statusCategoryOf = statusCategoryOf;
14
+ function concatParamsWithEncoding(end, params) {
15
+ if (!params || Object.keys(params).length === 0)
16
+ return end;
17
+ const paramsFlatten = Object.entries(params)
18
+ .flatMap(kv => Array.isArray(kv[1]) ? kv[1].map(v => [kv[0], v]) : [kv]);
19
+ let result = end ? end + "?" : "";
20
+ for (var kv of paramsFlatten)
21
+ result += kv[0] + "=" + encodeURIComponent(kv[1]) + "&";
22
+ return result.substring(0, result.length - 1);
23
+ }
24
+ UHttp.concatParamsWithEncoding = concatParamsWithEncoding;
25
+ })(UHttp = exports.UHttp || (exports.UHttp = {}));
@@ -0,0 +1,18 @@
1
+ import { NormalRecord } from "../const/types";
2
+ export declare namespace UObj {
3
+ /**
4
+ * assign properties to the object with specified property keys.
5
+ * @param t target object.
6
+ * @param s source object.
7
+ * @param keys property keys which are copied from source object. if omit this, all keys in source object is applied.
8
+ * @param keepDtypeClass if true, class which has properties decorated with {@link DType} in target object is kept and that is assigned properties recursively.
9
+ */
10
+ function assignProperties<T extends NormalRecord, S extends NormalRecord>(t: T, s: S, keys?: (keyof S)[], keepDtypeClass?: boolean): T & Partial<S>;
11
+ /**
12
+ * crop properties of the object. the properties is removed with `delete` operator.
13
+ * @param o object which properties is removed.
14
+ * @param keys property names to be remained. if omit this, it removes the properties other than properties decorated {@link DType}.
15
+ * @param exclusive if true, it removes `keys` instead of remaining it.
16
+ */
17
+ function crop<T extends NormalRecord>(o: T, keys?: (keyof T)[], exclusive?: boolean): Partial<T>;
18
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UObj = void 0;
4
+ const d_type_1 = require("./decorator/d-type");
5
+ const u_type_1 = require("./u-type");
6
+ var UObj;
7
+ (function (UObj) {
8
+ /**
9
+ * assign properties to the object with specified property keys.
10
+ * @param t target object.
11
+ * @param s source object.
12
+ * @param keys property keys which are copied from source object. if omit this, all keys in source object is applied.
13
+ * @param keepDtypeClass if true, class which has properties decorated with {@link DType} in target object is kept and that is assigned properties recursively.
14
+ */
15
+ function assignProperties(t, s, keys, keepDtypeClass) {
16
+ for (const k of keys ?? Object.keys(s))
17
+ if (u_type_1.UType.isDefined(s[k]))
18
+ if (keepDtypeClass && u_type_1.UType.isObject(t[k]) && u_type_1.UType.isObject(s[k]) && t[k]?.[d_type_1.smbl_tm]) {
19
+ assignProperties(t[k], s[k], null, true);
20
+ }
21
+ else
22
+ t[k] = s[k];
23
+ return t;
24
+ }
25
+ UObj.assignProperties = assignProperties;
26
+ /**
27
+ * crop properties of the object. the properties is removed with `delete` operator.
28
+ * @param o object which properties is removed.
29
+ * @param keys property names to be remained. if omit this, it removes the properties other than properties decorated {@link DType}.
30
+ * @param exclusive if true, it removes `keys` instead of remaining it.
31
+ */
32
+ function crop(o, keys, exclusive) {
33
+ if (!keys && !o[d_type_1.smbl_tm])
34
+ return {};
35
+ const _keys = keys ?? Object.keys(o[d_type_1.smbl_tm]);
36
+ Object.keys(o).filter(k => {
37
+ if (!keys && o[d_type_1.smbl_tm]?.[k]?.rec && o[k])
38
+ crop(o[k]);
39
+ return !!exclusive === _keys.includes(k);
40
+ }).forEach(k => delete o[k]);
41
+ return o;
42
+ }
43
+ UObj.crop = crop;
44
+ })(UObj = exports.UObj || (exports.UObj = {}));
@@ -0,0 +1,24 @@
1
+ import { TimeUnit } from "../const/time-unit";
2
+ export declare namespace UString {
3
+ function eq(s1: string, s2: string): boolean;
4
+ /**
5
+ * generate date time number as fixed length (depends on `unit`) string without separator charactor.
6
+ * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
7
+ * @param op.date Date object refered by this. default is `new Date()`.
8
+ * @param op.unit time unit. default is secound.
9
+ */
10
+ function simpleTime(op?: {
11
+ date?: Date;
12
+ unit?: TimeUnit;
13
+ }): string;
14
+ function trimProps(obj: any): void;
15
+ function generateRandomString(len: number): string;
16
+ function idx2az(idx: number): string;
17
+ function az2idx(az: string): number;
18
+ function is_yyyy(v: string): boolean;
19
+ function is_yyyyMM(v: string): boolean;
20
+ function is_yyyyMMdd(v: string): boolean;
21
+ function is_yyyyMMddhh(v: string): boolean;
22
+ function is_yyyyMMddhhmm(v: string): boolean;
23
+ function is_yyyyMMddhhmmss(v: string): boolean;
24
+ }
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UString = void 0;
4
+ const time_unit_1 = require("../const/time-unit");
5
+ const xjs_err_1 = require("../obj/xjs-err");
6
+ const u_1 = require("./u");
7
+ const u_type_1 = require("./u-type");
8
+ const s_errCode = 20;
9
+ var UString;
10
+ (function (UString) {
11
+ function eq(s1, s2) {
12
+ return !u_type_1.UType.isString(s1) || !u_type_1.UType.isString(s2) ? s1 === s2 : s1.trim() === s2.trim();
13
+ }
14
+ UString.eq = eq;
15
+ /**
16
+ * generate date time number as fixed length (depends on `unit`) string without separator charactor.
17
+ * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
18
+ * @param op.date Date object refered by this. default is `new Date()`.
19
+ * @param op.unit time unit. default is secound.
20
+ */
21
+ function simpleTime(op) {
22
+ const t = (op?.date ?? new Date()).toISOString().split(".")[0].replace(/[-T:]/g, "");
23
+ if (op?.unit === time_unit_1.TimeUnit.Msec)
24
+ return t;
25
+ return t.substring(0, 14 - (6 - (op?.unit ?? time_unit_1.TimeUnit.Sec)) * 2);
26
+ }
27
+ UString.simpleTime = simpleTime;
28
+ function trimProps(obj) {
29
+ Object.keys(obj)
30
+ .filter(k => typeof obj[k] === "string")
31
+ .forEach(k => obj[k] = obj[k]?.trim());
32
+ }
33
+ UString.trimProps = trimProps;
34
+ function generateRandomString(len) {
35
+ return (0, u_1.int2array)(len).map(_ => {
36
+ let rnd = Math.floor(62 * Math.random());
37
+ const remain = rnd - 52;
38
+ if (remain >= 0)
39
+ return remain.toString();
40
+ if (rnd > 26)
41
+ rnd += 6;
42
+ return String.fromCharCode(rnd + 65);
43
+ }).join("");
44
+ }
45
+ UString.generateRandomString = generateRandomString;
46
+ function idx2az(idx) {
47
+ let az = "", num = idx;
48
+ while (num >= 0) {
49
+ az = String.fromCharCode(num % 26 + 97) + az;
50
+ num = Math.floor(num / 26) - 1;
51
+ }
52
+ return az.toUpperCase();
53
+ }
54
+ UString.idx2az = idx2az;
55
+ function az2idx(az) {
56
+ if (!az?.match(/^[a-zA-Z]+$/))
57
+ throw new xjs_err_1.XjsErr(s_errCode, "the parameter isn't az(AZ) format.");
58
+ return az.toLowerCase().split("").map(c => c.charCodeAt(0) - 97).reverse()
59
+ .map((idx, i) => (idx + 1) * (26 ** i)).reduce((v1, v2) => v1 + v2) - 1;
60
+ }
61
+ UString.az2idx = az2idx;
62
+ function is_yyyy(v) {
63
+ return !!v?.match(/^[1-9]\d{3}$/);
64
+ }
65
+ UString.is_yyyy = is_yyyy;
66
+ function is_yyyyMM(v) {
67
+ return !!v?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])$/);
68
+ }
69
+ UString.is_yyyyMM = is_yyyyMM;
70
+ function is_yyyyMMdd(v) {
71
+ return !!v?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|[3][0-1])$/);
72
+ }
73
+ UString.is_yyyyMMdd = is_yyyyMMdd;
74
+ function is_yyyyMMddhh(v) {
75
+ return !!v?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|[3][0-1])([01]\d|2[0-3])$/);
76
+ }
77
+ UString.is_yyyyMMddhh = is_yyyyMMddhh;
78
+ function is_yyyyMMddhhmm(v) {
79
+ return !!v?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|[3][0-1])([01]\d|2[0-3])[0-5]\d$/);
80
+ }
81
+ UString.is_yyyyMMddhhmm = is_yyyyMMddhhmm;
82
+ function is_yyyyMMddhhmmss(v) {
83
+ return !!v?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|[3][0-1])([01]\d|2[0-3])[0-5]\d[0-5]\d$/);
84
+ }
85
+ UString.is_yyyyMMddhhmmss = is_yyyyMMddhhmmss;
86
+ })(UString = exports.UString || (exports.UString = {}));
@@ -0,0 +1,25 @@
1
+ import { MaybeArray, Type } from "../const/types";
2
+ export declare namespace UType {
3
+ function isDefined(v: any): boolean;
4
+ function isEmpty(v: any): boolean;
5
+ function isString(v: any): v is string;
6
+ function isNumber(v: any): v is number;
7
+ function isBigint(v: any): v is bigint;
8
+ function isBoolean(v: any): v is boolean;
9
+ function isSymbol(v: any): v is symbol;
10
+ function isObject(v: any): v is object;
11
+ function isArray(v: any, t: Type.string): v is string[];
12
+ function isArray(v: any, t: Type.number): v is number[];
13
+ function isArray(v: any, t: Type.bigint): v is bigint[];
14
+ function isArray(v: any, t: Type.boolean): v is boolean[];
15
+ function isArray(v: any, t: Type.symbol): v is symbol[];
16
+ function isArray(v: any, t: Type.object): v is object[];
17
+ function isArray(v: any): v is any[];
18
+ /**
19
+ * validate properties which attached decorators in {@link DType}.
20
+ * @param o object to be validated.
21
+ * @returns invalid property keys. returns an empty array if `o` is valid.
22
+ */
23
+ function validate(o: any): string[];
24
+ function takeAsArray<T>(v: MaybeArray<T>): T[];
25
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UType = void 0;
4
+ const types_1 = require("../const/types");
5
+ const d_type_1 = require("./decorator/d-type");
6
+ var UType;
7
+ (function (UType) {
8
+ function isDefined(v) {
9
+ return typeof v !== types_1.Type.undefined;
10
+ }
11
+ UType.isDefined = isDefined;
12
+ function isEmpty(v) {
13
+ return v === null || typeof v === types_1.Type.undefined;
14
+ }
15
+ UType.isEmpty = isEmpty;
16
+ function isString(v) { return typeof v === types_1.Type.string; }
17
+ UType.isString = isString;
18
+ function isNumber(v) { return typeof v === types_1.Type.number; }
19
+ UType.isNumber = isNumber;
20
+ function isBigint(v) { return typeof v === types_1.Type.bigint; }
21
+ UType.isBigint = isBigint;
22
+ function isBoolean(v) { return typeof v === types_1.Type.boolean; }
23
+ UType.isBoolean = isBoolean;
24
+ function isSymbol(v) { return typeof v === types_1.Type.symbol; }
25
+ UType.isSymbol = isSymbol;
26
+ function isObject(v) { return typeof v === types_1.Type.object; }
27
+ UType.isObject = isObject;
28
+ function isArray(v, t) {
29
+ return Array.isArray(v) && (!t || v.every(e => typeof e === t));
30
+ }
31
+ UType.isArray = isArray;
32
+ /**
33
+ * validate properties which attached decorators in {@link DType}.
34
+ * @param o object to be validated.
35
+ * @returns invalid property keys. returns an empty array if `o` is valid.
36
+ */
37
+ function validate(o) {
38
+ if (!o[d_type_1.smbl_tm])
39
+ return [];
40
+ return Object.entries(o[d_type_1.smbl_tm]).flatMap(e => validateProp(e[0], o[e[0]], e[1]));
41
+ }
42
+ UType.validate = validate;
43
+ function validateProp(k, prop, td) {
44
+ if (isEmpty(prop))
45
+ return td.req ? [k] : [];
46
+ if (td.t && typeof prop !== td.t)
47
+ return [k];
48
+ if (td.ary)
49
+ return Array.isArray(prop) ? prop.flatMap(e => validateProp(k, e, td.ary)) : [k];
50
+ if (td.rec)
51
+ return validate(prop).flatMap(k2 => `${k}.${k2}`);
52
+ return [];
53
+ }
54
+ function takeAsArray(v) {
55
+ return Array.isArray(v) ? v : [v];
56
+ }
57
+ UType.takeAsArray = takeAsArray;
58
+ })(UType = exports.UType || (exports.UType = {}));
@@ -0,0 +1,3 @@
1
+ import { MaybeArray } from "xjs-common";
2
+ export declare function checkPortAvailability(port: number): Promise<boolean>;
3
+ export declare function joinPath(...p: MaybeArray<string>[]): string;
package/dist/func/u.js ADDED
@@ -0,0 +1,42 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.joinPath = exports.checkPortAvailability = void 0;
27
+ const path = __importStar(require("path"));
28
+ const xjs_common_1 = require("xjs-common");
29
+ const s_errCode = 1010;
30
+ function checkPortAvailability(port) {
31
+ return new Promise(resolve => {
32
+ const server = require('net').createServer();
33
+ server.once('error', () => resolve(false))
34
+ .once('listening', () => { server.close(); resolve(true); })
35
+ .listen(port);
36
+ });
37
+ }
38
+ exports.checkPortAvailability = checkPortAvailability;
39
+ function joinPath(...p) {
40
+ return path.join(...p.flatMap(xjs_common_1.UType.takeAsArray));
41
+ }
42
+ exports.joinPath = joinPath;
@@ -0,0 +1,5 @@
1
+ export * from "./func/u";
2
+ export * from "./func/u-file";
3
+ export { HttpResolver } from "./prcs/http/http-resolver";
4
+ export { s_clientMode } from "./prcs/http/http-resolver-context";
5
+ export { IHttpClient } from "./prcs/http/i-http-client";