xjs-common 10.1.2 → 11.0.0-alpha.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.
Files changed (46) hide show
  1. package/dist/index.js +1 -32
  2. package/dist/types.d.ts +285 -0
  3. package/package.json +10 -7
  4. package/dist/const/ccy.d.ts +0 -18
  5. package/dist/const/ccy.js +0 -20
  6. package/dist/const/gender.d.ts +0 -4
  7. package/dist/const/gender.js +0 -8
  8. package/dist/const/http-method.d.ts +0 -8
  9. package/dist/const/http-method.js +0 -12
  10. package/dist/const/time-unit.d.ts +0 -9
  11. package/dist/const/time-unit.js +0 -13
  12. package/dist/const/types.d.ts +0 -24
  13. package/dist/const/types.js +0 -14
  14. package/dist/func/decorator/d-type.d.ts +0 -24
  15. package/dist/func/decorator/d-type.js +0 -71
  16. package/dist/func/decorator/d-validate.d.ts +0 -20
  17. package/dist/func/decorator/d-validate.js +0 -64
  18. package/dist/func/decorator/transaction.d.ts +0 -7
  19. package/dist/func/decorator/transaction.js +0 -36
  20. package/dist/func/u-array.d.ts +0 -40
  21. package/dist/func/u-array.js +0 -70
  22. package/dist/func/u-file.d.ts +0 -51
  23. package/dist/func/u-file.js +0 -171
  24. package/dist/func/u-http.d.ts +0 -9
  25. package/dist/func/u-http.js +0 -32
  26. package/dist/func/u-obj.d.ts +0 -18
  27. package/dist/func/u-obj.js +0 -44
  28. package/dist/func/u-string.d.ts +0 -27
  29. package/dist/func/u-string.js +0 -120
  30. package/dist/func/u-type.d.ts +0 -25
  31. package/dist/func/u-type.js +0 -58
  32. package/dist/func/u.d.ts +0 -24
  33. package/dist/func/u.js +0 -76
  34. package/dist/index.d.ts +0 -14
  35. package/dist/obj/type-exp.d.ts +0 -8
  36. package/dist/obj/type-exp.js +0 -23
  37. package/dist/obj/xjs-err.d.ts +0 -5
  38. package/dist/obj/xjs-err.js +0 -13
  39. package/dist/prcs/http/http-resolver-context.d.ts +0 -64
  40. package/dist/prcs/http/http-resolver-context.js +0 -343
  41. package/dist/prcs/http/http-resolver.d.ts +0 -36
  42. package/dist/prcs/http/http-resolver.js +0 -53
  43. package/dist/prcs/http/i-http-client.d.ts +0 -63
  44. package/dist/prcs/http/i-http-client.js +0 -2
  45. package/dist/prcs/http-resolver.d.ts +0 -53
  46. package/dist/prcs/http-resolver.js +0 -255
package/dist/index.js CHANGED
@@ -1,32 +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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.DType = void 0;
18
- __exportStar(require("./const/types"), exports);
19
- __exportStar(require("./const/time-unit"), exports);
20
- __exportStar(require("./const/gender"), exports);
21
- __exportStar(require("./const/http-method"), exports);
22
- __exportStar(require("./const/ccy"), exports);
23
- __exportStar(require("./func/u"), exports);
24
- __exportStar(require("./func/u-obj"), exports);
25
- __exportStar(require("./func/u-string"), exports);
26
- __exportStar(require("./func/u-array"), exports);
27
- __exportStar(require("./func/u-http"), exports);
28
- __exportStar(require("./func/u-type"), exports);
29
- __exportStar(require("./func/decorator/transaction"), exports);
30
- var d_type_1 = require("./func/decorator/d-type");
31
- Object.defineProperty(exports, "DType", { enumerable: true, get: function () { return d_type_1.DType; } });
32
- __exportStar(require("./obj/xjs-err"), exports);
1
+ (()=>{"use strict";var t,n,e,r;!function(t){t.string="string",t.number="number",t.bigint="bigint",t.boolean="boolean",t.symbol="symbol",t.object="object",t[void 0]="undefined",t.null="null"}(t||(t={})),function(t){t[t.Year=1]="Year",t[t.Month=2]="Month",t[t.Day=3]="Day",t[t.Hour=4]="Hour",t[t.Min=5]="Min",t[t.Sec=6]="Sec",t[t.Msec=7]="Msec"}(n||(n={})),function(t){t[t.Male=0]="Male",t[t.Female=1]="Female"}(e||(e={})),function(t){t.Get="GET",t.Put="PUT",t.Post="POST",t.Delete="DELETE",t.Patch="PATCH",t.Connect="CONNECT"}(r||(r={}));const o={jpy:{crossUsd:"usd/jpy"},usd:{crossUsd:null},gbp:{crossUsd:"gbp/usd"},eur:{crossUsd:"eur/usd"},cad:{crossUsd:"cad/usd"},aud:{crossUsd:"aud/usd"}};Object.entries(o).forEach(t=>{t[1].symbol=t[0],o[t[0]]=Object.freeze(t[1])}),Object.freeze(o);class i extends Error{code;origin;constructor(t,n,e){super(`[XJS] ${n}`),this.code=t,this.origin=e}}function s(t){const n=Number(t);if(Number.isNaN(n))throw new i(10,"size of the argument is not number.");return Array.from(Array(n).keys())}const u=Symbol.for("xjs:typeMap");var c,a,f,l,y,d;!function(n){function e(t,n,e){r(t,n,t=>{if(t.t)throw new i(30,"decorator to express type is duplicate.");t.t=e})}function r(t,n,e){const r=t[u]?Object.assign({},t[u]):{};r[n]??={t:null,req:!1,rec:!1,ary:null};const o=r[n];e(o);let s=null,c=null;if(o.t&&o.rec&&(s="type",c="recursive flag"),o.t&&o.ary&&(s="type",c="array"),o.ary&&o.rec&&(s="array",c="recursive flag"),s&&c)throw new i(30,`decorator to express ${s} and ${c} are exclusive.`);Object.defineProperty(t,u,{value:r,configurable:!0})}n.string=function(n,r){e(n,r,t.string)},n.number=function(n,r){e(n,r,t.number)},n.boolean=function(n,r){e(n,r,t.boolean)},n.required=function(t,n){r(t,n,t=>t.req=!0)},n.array=function(t={}){return(n,e)=>r(n,e,n=>n.ary=t)},n.recursive=function(t,n){r(t,n,t=>t.rec=!0)},n.keep=function(t,n){r(t,n,t=>{})}}(c||(c={})),function(n){function e(n){return null===n||typeof n===t.undefined}function r(t){return t[u]?Object.entries(t[u]).flatMap(n=>o(n[0],t[n[0]],n[1])):[]}function o(t,n,i){return e(n)?i.req?[t]:[]:i.t&&typeof n!==i.t?[t]:i.ary?Array.isArray(n)?n.flatMap(n=>o(t,n,i.ary)):[t]:i.rec?r(n).flatMap(n=>`${t}.${n}`):[]}n.isDefined=function(n){return typeof n!==t.undefined},n.isEmpty=e,n.isString=function(n){return typeof n===t.string},n.isNumber=function(n){return typeof n===t.number},n.isBigint=function(n){return typeof n===t.bigint},n.isBoolean=function(n){return typeof n===t.boolean},n.isSymbol=function(n){return typeof n===t.symbol},n.isObject=function(n){return typeof n===t.object},n.isArray=function(t,n){return Array.isArray(t)&&(!n||t.every(t=>typeof t===n))},n.validate=r,n.takeAsArray=function(t){return Array.isArray(t)?t:[t]}}(a||(a={})),function(t){t.assignProperties=function t(n,e,r,o){for(const i of r??Object.keys(e))a.isDefined(e[i])&&(o&&a.isObject(n[i])&&a.isObject(e[i])&&n[i]?.[u]?t(n[i],e[i],null,!0):n[i]=e[i]);return n},t.crop=function t(n,e,r){if(!e&&!n[u])return{};const o=e??Object.keys(n[u]);return Object.keys(n).filter(i=>(!e&&n[u]?.[i]?.rec&&n[i]&&t(n[i]),!!r===o.includes(i))).forEach(t=>delete n[t]),n}}(f||(f={})),function(t){function e(t,n){const e=t.toString().split("."),r=e[0].split("").reverse().join("");let o="";const i=Math.ceil(r.length/3);for(let t=0;t<i;t++)o+=t===i-1?r.substring(3*t):r.substring(3*t,3*(t+1))+",";const s=n+o.split("").reverse().join("");return 1===e.length?s:s+"."+e[1]}t.eq=function(t,n){return a.isString(t)&&a.isString(n)?t.trim()===n.trim():t===n},t.repeat=function(t,n){return s(n).map(n=>t).join("")},t.simpleTime=function(t){const e=(t?.date??new Date).toISOString().split(".")[0].replace(/[-T:]/g,"");return t?.unit===n.Msec?e:e.substring(0,14-2*(6-(t?.unit??n.Sec)))},t.generateRandomString=function(t){return s(t).map(t=>{let n=Math.floor(62*Math.random());const e=n-52;return e>=0?e.toString():(n>26&&(n+=6),String.fromCharCode(n+65))}).join("")},t.idx2az=function(t){let n="",e=t;for(;e>=0;)n=String.fromCharCode(e%26+97)+n,e=Math.floor(e/26)-1;return n.toUpperCase()},t.az2idx=function(t){if(!t?.match(/^[a-zA-Z]+$/))throw new i(20,"the parameter isn't az(AZ) format.");return t.toLowerCase().split("").map(t=>t.charCodeAt(0)-97).reverse().map((t,n)=>(t+1)*26**n).reduce((t,n)=>t+n)-1},t.asJpy=function(t){return a.isEmpty(t)?"":e(Math.floor(t),"¥")},t.asUsd=function(t){return a.isEmpty(t)?"":e(Number(t.toFixed(2)),"$")},t.asPercentage=function(t){if(a.isEmpty(t))return"";let n=(100*t).toFixed(2);for(;n.endsWith("0");)n=n.substring(0,n.length-1);return n.endsWith(".")&&(n=n.substring(0,n.length-1)),n+"%"},t.is_yyyy=function(t){return!!t?.match(/^[1-9]\d{3}$/)},t.is_yyyyMM=function(t){return!!t?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])$/)},t.is_yyyyMMdd=function(t){return!!t?.match(/^[1-9]\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|[3][0-1])$/)},t.is_yyyyMMddhh=function(t){return!!t?.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])$/)},t.is_yyyyMMddhhmm=function(t){return!!t?.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$/)},t.is_yyyyMMddhhmmss=function(t){return!!t?.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$/)}}(l||(l={})),function(t){function n(t,n=!0){const e=Math.floor(t.length*Math.random()),r=t[e];return n&&t.splice(e,1),r}t.eq=function(t,n,e={}){const{sort:r,useStrictEqual:o}=Object.assign({sort:!0,useStrictEqual:!0},e);if(t&&!n||!t&&n)return!1;if(!t)return!0;if(t.length!==n.length)return!1;const i=r?[...t].sort():t,s=r?[...n].sort():n;return i.every((t,n)=>o?t===s[n]:t==s[n])},t.distinct=function(t,n){if(!t||0===t.length)return[];if(n?.k)return Array.from(function(t,n){const e=new Map;for(const r of t){const t=n(r);e.has(t)?e.get(t).push(r):e.set(t,[r])}return e}(t,t=>t[n.k]).values()).map(t=>n?.takeLast?t.pop():t.shift());const e=n?.takeLast?[...t].reverse():[...t],r=n?.predicate??((t,n)=>t==n),o=[e.shift()];return e.forEach(t=>o.some(n=>r(t,n))?{}:o.push(t)),o},t.chop=function(t,n){return[...Array(Math.ceil(t.length/n)).keys()].map(e=>{let r=(e+1)*n;return r>t.length&&(r=t.length),t.slice(e*n,r)})},t.remove=function(t,n){const e=t.indexOf(n);-1!==e&&t.splice(e,1)},t.randomPick=n,t.shuffle=function(t){const e=[...t];return s(t.length).map(t=>n(e))},t.takeOut=function(t,n){const e=[];for(let r=t.length-1;r>=0;r--)n(t[r],r)&&(e.unshift(t[r]),t.splice(r,1));return e}}(y||(y={})),function(t){function n(t){return Math.floor(t/100)}t.isHttpSuccess=function(t){return 2===n(t)},t.statusCategoryOf=n,t.concatParamsWithEncoding=function(t,n){if(!n||0===Object.keys(n).length)return t;const e=Object.entries(n).flatMap(t=>Array.isArray(t[1])?t[1].map(n=>[t[0],n]):[t]);let r=t?t+"?":"";for(var o of e)r+=o[0]+"="+encodeURIComponent(o[1])+"&";return r.substring(0,r.length-1)},t.normalizeHeaders=function(t){return t?Object.entries(t).reduce((t,n)=>(t[n[0].toLowerCase()]=n[1],t),{}):{}}}(d||(d={}))})();
@@ -0,0 +1,285 @@
1
+ declare module "const/types" {
2
+ export type IndexSignature = string | number | symbol;
3
+ export type NormalRecord = Record<IndexSignature, any>;
4
+ export type MaybeArray<T> = T | T[];
5
+ export type Loggable = {
6
+ log: (msg: any) => void;
7
+ warn: (msg: any) => void;
8
+ error: (msg: any) => void;
9
+ };
10
+ export type Unique<T = number> = {
11
+ id: T;
12
+ };
13
+ export type IdName<T = number> = {
14
+ name: string;
15
+ } & Unique<T>;
16
+ export enum Type {
17
+ string = "string",
18
+ number = "number",
19
+ bigint = "bigint",
20
+ boolean = "boolean",
21
+ symbol = "symbol",
22
+ object = "object",
23
+ undefined = "undefined",
24
+ null = "null"
25
+ }
26
+ }
27
+ declare module "const/time-unit" {
28
+ export enum TimeUnit {
29
+ Year = 1,
30
+ Month = 2,
31
+ Day = 3,
32
+ Hour = 4,
33
+ Min = 5,
34
+ Sec = 6,
35
+ Msec = 7
36
+ }
37
+ }
38
+ declare module "const/gender" {
39
+ export enum Gender {
40
+ Male = 0,
41
+ Female = 1
42
+ }
43
+ }
44
+ declare module "const/http-method" {
45
+ export enum HttpMethod {
46
+ Get = "GET",
47
+ Put = "PUT",
48
+ Post = "POST",
49
+ Delete = "DELETE",
50
+ Patch = "PATCH",
51
+ Connect = "CONNECT"
52
+ }
53
+ }
54
+ declare module "const/ccy" {
55
+ export interface Ccy {
56
+ symbol: t_ccySymbol;
57
+ crossUsd: string;
58
+ }
59
+ export type t_ccySymbol = keyof (typeof currencies);
60
+ const currencies: {
61
+ jpy: Ccy;
62
+ usd: Ccy;
63
+ gbp: Ccy;
64
+ eur: Ccy;
65
+ cad: Ccy;
66
+ aud: Ccy;
67
+ };
68
+ export const s_ccy: {
69
+ [k in t_ccySymbol]: Readonly<Ccy>;
70
+ };
71
+ export function resolveCcy(str: string): Ccy;
72
+ }
73
+ declare module "obj/xjs-err" {
74
+ export class XjsErr extends Error {
75
+ code: number;
76
+ origin?: any;
77
+ constructor(code: number, msg: string, origin?: any);
78
+ }
79
+ }
80
+ declare module "func/u" {
81
+ import { Loggable } from "const/types";
82
+ export function getJSTDate(d?: Date): Date;
83
+ export function delay(sec: number): Promise<void>;
84
+ export function int2array(size: number): number[];
85
+ export function array2map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
86
+ export function bitor(...bit: number[]): number;
87
+ export interface RetryOption<T = void | Promise<void>> {
88
+ count?: number;
89
+ logger?: Loggable;
90
+ errorCriterion?: (e: any) => boolean;
91
+ intervalPredicate?: () => T;
92
+ }
93
+ /**
94
+ * runs callback with customizable retry.
95
+ * @param cb callback to be retried.
96
+ * @param op.count number of retries. default is 1.
97
+ * @param op.logger logger used for exceptions while retrying the process. default is `console` object.
98
+ * @param op.errorCriterion distinguish whether retry is required form exceptions. default is none. (i.e. allways required.)
99
+ * @param op.intervalPredicate predicate that runs between callbacks when retrying.
100
+ */
101
+ export function retry<T>(cb: () => T, op?: RetryOption<void>): T;
102
+ export function retry<T>(cb: () => T, op?: RetryOption<Promise<void>>): Promise<T>;
103
+ export function retry<T>(cb: () => Promise<T>, op?: RetryOption<void>): Promise<T>;
104
+ export function retry<T>(cb: () => Promise<T>, op?: RetryOption<Promise<void>>): Promise<T>;
105
+ }
106
+ declare module "func/u-type" {
107
+ import { MaybeArray, Type } from "const/types";
108
+ export namespace UType {
109
+ function isDefined(v: any): boolean;
110
+ function isEmpty(v: any): boolean;
111
+ function isString(v: any): v is string;
112
+ function isNumber(v: any): v is number;
113
+ function isBigint(v: any): v is bigint;
114
+ function isBoolean(v: any): v is boolean;
115
+ function isSymbol(v: any): v is symbol;
116
+ function isObject(v: any): v is object;
117
+ function isArray(v: any, t: Type.string): v is string[];
118
+ function isArray(v: any, t: Type.number): v is number[];
119
+ function isArray(v: any, t: Type.bigint): v is bigint[];
120
+ function isArray(v: any, t: Type.boolean): v is boolean[];
121
+ function isArray(v: any, t: Type.symbol): v is symbol[];
122
+ function isArray(v: any, t: Type.object): v is object[];
123
+ function isArray(v: any): v is any[];
124
+ /**
125
+ * validate properties which attached decorators in {@link DType}.
126
+ * @param o object to be validated.
127
+ * @returns invalid property keys. returns an empty array if `o` is valid.
128
+ */
129
+ function validate(o: any): string[];
130
+ function takeAsArray<T>(v: MaybeArray<T>): T[];
131
+ }
132
+ }
133
+ declare module "func/decorator/d-type" {
134
+ import { Type } from "const/types";
135
+ export const smbl_tm: unique symbol;
136
+ export interface TypeDesc {
137
+ t?: Type;
138
+ req?: boolean;
139
+ ary?: TypeDesc;
140
+ rec?: boolean;
141
+ }
142
+ export interface TypeMap {
143
+ [k: string]: TypeDesc;
144
+ }
145
+ /**
146
+ * decorators to be validated by {@link UType.validate},
147
+ * and to be cropped by {@link UObj.crop}.
148
+ */
149
+ export namespace DType {
150
+ function string(target: Object, propKey: string): void;
151
+ function number(target: Object, propKey: string): void;
152
+ function boolean(target: Object, propKey: string): void;
153
+ function required(target: Object, propKey: string): void;
154
+ function array(elmDesc?: TypeDesc): (target: Object, propKey: string) => void;
155
+ function recursive(target: Object, propKey: string): void;
156
+ function keep(target: Object, propKey: string): void;
157
+ }
158
+ }
159
+ declare module "func/u-obj" {
160
+ import { NormalRecord } from "const/types";
161
+ export namespace UObj {
162
+ /**
163
+ * assign properties to the object with specified property keys.
164
+ * @param t target object.
165
+ * @param s source object.
166
+ * @param keys property keys which are copied from source object. if omit this, all keys in source object is applied.
167
+ * @param keepDtypeClass if true, class which has properties decorated with {@link DType} in target object is kept and that is assigned properties recursively.
168
+ */
169
+ function assignProperties<T extends NormalRecord, S extends NormalRecord>(t: T, s: S, keys?: (keyof S)[], keepDtypeClass?: boolean): T & Partial<S>;
170
+ /**
171
+ * crop properties of the object. the properties is removed with `delete` operator.
172
+ * @param o object which properties is removed.
173
+ * @param keys property names to be remained. if omit this, it removes the properties other than properties decorated {@link DType}.
174
+ * @param exclusive if true, it removes `keys` instead of remaining it.
175
+ */
176
+ function crop<T extends NormalRecord>(o: T, keys?: (keyof T)[], exclusive?: boolean): Partial<T>;
177
+ }
178
+ }
179
+ declare module "func/u-string" {
180
+ import { TimeUnit } from "const/time-unit";
181
+ export namespace UString {
182
+ function eq(s1: string, s2: string): boolean;
183
+ function repeat(token: string, mlt: number): string;
184
+ /**
185
+ * generate date time number as fixed length (depends on `unit`) string without separator charactor.
186
+ * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
187
+ * @param op.date Date object refered by this. default is `new Date()`.
188
+ * @param op.unit time unit. default is secound.
189
+ */
190
+ function simpleTime(op?: {
191
+ date?: Date;
192
+ unit?: TimeUnit;
193
+ }): string;
194
+ function generateRandomString(len: number): string;
195
+ function idx2az(idx: number): string;
196
+ function az2idx(az: string): number;
197
+ function asJpy(amount: number): string;
198
+ function asUsd(amount: number): string;
199
+ function asPercentage(amount: number): string;
200
+ function is_yyyy(v: string): boolean;
201
+ function is_yyyyMM(v: string): boolean;
202
+ function is_yyyyMMdd(v: string): boolean;
203
+ function is_yyyyMMddhh(v: string): boolean;
204
+ function is_yyyyMMddhhmm(v: string): boolean;
205
+ function is_yyyyMMddhhmmss(v: string): boolean;
206
+ }
207
+ }
208
+ declare module "func/u-array" {
209
+ export namespace UArray {
210
+ /**
211
+ * compares two arrays to valuate equality.
212
+ * if one side is null or undefined, it returns true when other side is the same.
213
+ * @param v1 it uses equal operator for comparing elements, so applying object element is not recommended.
214
+ * @param v2 same as v1.
215
+ * @param sort it uses {@link Array#sort} on v1 and v2 if true. default is true.
216
+ * @param useStrictEqual it uses `===` operator for compareing elements if true, otherwise using `==` operator. default is true.
217
+ */
218
+ function eq(v1: any[], v2: any[], op: {
219
+ sort?: boolean;
220
+ useStrictEqual: false;
221
+ }): boolean;
222
+ function eq<T>(v1: T[], v2: T[], op: {
223
+ sort?: boolean;
224
+ useStrictEqual: true;
225
+ }): boolean;
226
+ function eq<T>(v1: T[], v2: T[], op: {
227
+ sort?: boolean;
228
+ }): boolean;
229
+ function eq<T>(v1: T[], v2: T[]): boolean;
230
+ /**
231
+ * returns array which is removed duplicate of elements.
232
+ * this doesn't mutate the param.
233
+ */
234
+ function distinct<T>(array: T[]): T[];
235
+ function distinct<T>(array: T[], op: {
236
+ k: keyof T;
237
+ takeLast?: boolean;
238
+ }): T[];
239
+ function distinct<T>(array: T[], op: {
240
+ predicate: (v1: T, v2: T) => boolean;
241
+ takeLast?: boolean;
242
+ }): T[];
243
+ function chop<T>(array: T[], len: number): T[][];
244
+ function remove<T>(array: T[], v: T): void;
245
+ function randomPick<T>(array: T[], takeout?: boolean): T;
246
+ function shuffle<T>(array: T[]): T[];
247
+ function takeOut<T>(array: T[], filter: (v: T, i?: number) => boolean): T[];
248
+ }
249
+ }
250
+ declare module "func/u-http" {
251
+ export namespace UHttp {
252
+ function isHttpSuccess(statusCode: number): boolean;
253
+ function statusCategoryOf(statusCode: number): number;
254
+ function concatParamsWithEncoding(end: string, params: {
255
+ [k: string]: string | string[];
256
+ }): string;
257
+ /** normalize object keys to lower case. */
258
+ function normalizeHeaders(headers: Record<string, any>): Record<string, any>;
259
+ }
260
+ }
261
+ declare module "func/decorator/transaction" {
262
+ /**
263
+ * applies transation to the method. note that the method must return a Promise.
264
+ * @param op.timeoutSec default is `30`.
265
+ */
266
+ export function transaction(op?: {
267
+ timeoutSec?: number;
268
+ }): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
269
+ }
270
+ declare module "xjs-common" {
271
+ export * from "const/types";
272
+ export * from "const/time-unit";
273
+ export * from "const/gender";
274
+ export * from "const/http-method";
275
+ export * from "const/ccy";
276
+ export * from "func/u";
277
+ export * from "func/u-obj";
278
+ export * from "func/u-string";
279
+ export * from "func/u-array";
280
+ export * from "func/u-http";
281
+ export * from "func/u-type";
282
+ export * from "func/decorator/transaction";
283
+ export { DType } from "func/decorator/d-type";
284
+ export * from "obj/xjs-err";
285
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "10.1.2",
3
+ "version": "11.0.0-alpha.1",
4
4
  "description": "library modules for typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,8 +12,9 @@
12
12
  "utility"
13
13
  ],
14
14
  "scripts": {
15
- "build": "tsc",
16
- "test": "node ./dist/test/test.js"
15
+ "build": "tsc && webpack && npm run build:types",
16
+ "build:types": "sh ./build-types.sh",
17
+ "test": "tsc -p ./tsconfig-test.json && node ./compiled/test/test.js"
17
18
  },
18
19
  "author": "begyyal",
19
20
  "license": "Apache-2.0",
@@ -22,9 +23,11 @@
22
23
  "!/dist/test*"
23
24
  ],
24
25
  "main": "./dist/index",
25
- "types": "./dist/index.d.ts",
26
+ "types": "./dist/types.d.ts",
26
27
  "devDependencies": {
27
- "ts-node": "^10.9.1",
28
- "typescript": "^4.9.5"
28
+ "tsx": "^4.20.3",
29
+ "typescript": "^5.9.2",
30
+ "webpack": "^5.101.0",
31
+ "webpack-cli": "^6.0.1"
29
32
  }
30
- }
33
+ }
@@ -1,18 +0,0 @@
1
- export interface Ccy {
2
- symbol: t_ccySymbol;
3
- crossUsd: string;
4
- }
5
- export type t_ccySymbol = keyof (typeof currencies);
6
- declare const currencies: {
7
- jpy: Ccy;
8
- usd: Ccy;
9
- gbp: Ccy;
10
- eur: Ccy;
11
- cad: Ccy;
12
- aud: Ccy;
13
- };
14
- export declare const s_ccy: {
15
- [k in t_ccySymbol]: Readonly<Ccy>;
16
- };
17
- export declare function resolveCcy(str: string): Ccy;
18
- export {};
package/dist/const/ccy.js DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveCcy = exports.s_ccy = void 0;
4
- const currencies = {
5
- jpy: { crossUsd: "usd/jpy" },
6
- usd: { crossUsd: null },
7
- gbp: { crossUsd: "gbp/usd" },
8
- eur: { crossUsd: "eur/usd" },
9
- cad: { crossUsd: "cad/usd" },
10
- aud: { crossUsd: "aud/usd" }
11
- };
12
- exports.s_ccy = (() => {
13
- Object.entries(currencies).forEach(kv => { kv[1].symbol = kv[0]; currencies[kv[0]] = Object.freeze(kv[1]); });
14
- return Object.freeze(currencies);
15
- })();
16
- function resolveCcy(str) {
17
- const ccyKey = Object.keys(exports.s_ccy).find(k => str?.toLowerCase() == k);
18
- return ccyKey && exports.s_ccy[ccyKey];
19
- }
20
- exports.resolveCcy = resolveCcy;
@@ -1,4 +0,0 @@
1
- export declare enum Gender {
2
- Male = 0,
3
- Female = 1
4
- }
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Gender = void 0;
4
- var Gender;
5
- (function (Gender) {
6
- Gender[Gender["Male"] = 0] = "Male";
7
- Gender[Gender["Female"] = 1] = "Female";
8
- })(Gender = exports.Gender || (exports.Gender = {}));
@@ -1,8 +0,0 @@
1
- export declare enum HttpMethod {
2
- Get = "GET",
3
- Put = "PUT",
4
- Post = "POST",
5
- Delete = "DELETE",
6
- Patch = "PATCH",
7
- Connect = "CONNECT"
8
- }
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpMethod = void 0;
4
- var HttpMethod;
5
- (function (HttpMethod) {
6
- HttpMethod["Get"] = "GET";
7
- HttpMethod["Put"] = "PUT";
8
- HttpMethod["Post"] = "POST";
9
- HttpMethod["Delete"] = "DELETE";
10
- HttpMethod["Patch"] = "PATCH";
11
- HttpMethod["Connect"] = "CONNECT";
12
- })(HttpMethod = exports.HttpMethod || (exports.HttpMethod = {}));
@@ -1,9 +0,0 @@
1
- export declare enum TimeUnit {
2
- Year = 1,
3
- Month = 2,
4
- Day = 3,
5
- Hour = 4,
6
- Min = 5,
7
- Sec = 6,
8
- Msec = 7
9
- }
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TimeUnit = void 0;
4
- var TimeUnit;
5
- (function (TimeUnit) {
6
- TimeUnit[TimeUnit["Year"] = 1] = "Year";
7
- TimeUnit[TimeUnit["Month"] = 2] = "Month";
8
- TimeUnit[TimeUnit["Day"] = 3] = "Day";
9
- TimeUnit[TimeUnit["Hour"] = 4] = "Hour";
10
- TimeUnit[TimeUnit["Min"] = 5] = "Min";
11
- TimeUnit[TimeUnit["Sec"] = 6] = "Sec";
12
- TimeUnit[TimeUnit["Msec"] = 7] = "Msec";
13
- })(TimeUnit = exports.TimeUnit || (exports.TimeUnit = {}));
@@ -1,24 +0,0 @@
1
- export type IndexSignature = string | number | symbol;
2
- export type NormalRecord = Record<IndexSignature, any>;
3
- export type MaybeArray<T> = T | T[];
4
- export type Loggable = {
5
- log: (msg: any) => void;
6
- warn: (msg: any) => void;
7
- error: (msg: any) => void;
8
- };
9
- export type Unique<T = number> = {
10
- id: T;
11
- };
12
- export type IdName<T = number> = {
13
- name: string;
14
- } & Unique<T>;
15
- export declare enum Type {
16
- string = "string",
17
- number = "number",
18
- bigint = "bigint",
19
- boolean = "boolean",
20
- symbol = "symbol",
21
- object = "object",
22
- undefined = "undefined",
23
- null = "null"
24
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Type = void 0;
4
- var Type;
5
- (function (Type) {
6
- Type["string"] = "string";
7
- Type["number"] = "number";
8
- Type["bigint"] = "bigint";
9
- Type["boolean"] = "boolean";
10
- Type["symbol"] = "symbol";
11
- Type["object"] = "object";
12
- Type["undefined"] = "undefined";
13
- Type["null"] = "null";
14
- })(Type = exports.Type || (exports.Type = {}));
@@ -1,24 +0,0 @@
1
- import { Type } from "../../const/types";
2
- export declare const smbl_tm: unique symbol;
3
- export interface TypeDesc {
4
- t?: Type;
5
- req?: boolean;
6
- ary?: TypeDesc;
7
- rec?: boolean;
8
- }
9
- export interface TypeMap {
10
- [k: string]: TypeDesc;
11
- }
12
- /**
13
- * decorators to be validated by {@link UType.validate},
14
- * and to be cropped by {@link UObj.crop}.
15
- */
16
- export declare namespace DType {
17
- function string(target: Object, propKey: string): void;
18
- function number(target: Object, propKey: string): void;
19
- function boolean(target: Object, propKey: string): void;
20
- function required(target: Object, propKey: string): void;
21
- function array(elmDesc?: TypeDesc): (target: Object, propKey: string) => void;
22
- function recursive(target: Object, propKey: string): void;
23
- function keep(target: Object, propKey: string): void;
24
- }
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DType = exports.smbl_tm = void 0;
4
- const types_1 = require("../../const/types");
5
- const xjs_err_1 = require("../../obj/xjs-err");
6
- const s_errCode = 30;
7
- exports.smbl_tm = Symbol.for("xjs:typeMap");
8
- /**
9
- * decorators to be validated by {@link UType.validate},
10
- * and to be cropped by {@link UObj.crop}.
11
- */
12
- var DType;
13
- (function (DType) {
14
- function string(target, propKey) {
15
- setTypeDesc(target, propKey, types_1.Type.string);
16
- }
17
- DType.string = string;
18
- function number(target, propKey) {
19
- setTypeDesc(target, propKey, types_1.Type.number);
20
- }
21
- DType.number = number;
22
- function boolean(target, propKey) {
23
- setTypeDesc(target, propKey, types_1.Type.boolean);
24
- }
25
- DType.boolean = boolean;
26
- function setTypeDesc(target, propKey, t) {
27
- setDesc(target, propKey, (td) => {
28
- if (td.t)
29
- throw new xjs_err_1.XjsErr(s_errCode, "decorator to express type is duplicate.");
30
- td.t = t;
31
- });
32
- }
33
- function required(target, propKey) {
34
- setDesc(target, propKey, (td) => td.req = true);
35
- }
36
- DType.required = required;
37
- function array(elmDesc = {}) {
38
- return (target, propKey) => setDesc(target, propKey, (td) => td.ary = elmDesc);
39
- }
40
- DType.array = array;
41
- function recursive(target, propKey) {
42
- setDesc(target, propKey, (td) => td.rec = true);
43
- }
44
- DType.recursive = recursive;
45
- function keep(target, propKey) {
46
- setDesc(target, propKey, (_) => { });
47
- }
48
- DType.keep = keep;
49
- function setDesc(target, propKey, setter) {
50
- const map = target[exports.smbl_tm] ? Object.assign({}, target[exports.smbl_tm]) : {};
51
- map[propKey] ??= { t: null, req: false, rec: false, ary: null };
52
- const td = map[propKey];
53
- setter(td);
54
- let ex1 = null, ex2 = null;
55
- if (td.t && td.rec) {
56
- ex1 = "type";
57
- ex2 = "recursive flag";
58
- }
59
- if (td.t && td.ary) {
60
- ex1 = "type";
61
- ex2 = "array";
62
- }
63
- if (td.ary && td.rec) {
64
- ex1 = "array";
65
- ex2 = "recursive flag";
66
- }
67
- if (ex1 && ex2)
68
- throw new xjs_err_1.XjsErr(s_errCode, `decorator to express ${ex1} and ${ex2} are exclusive.`);
69
- Object.defineProperty(target, exports.smbl_tm, { value: map, configurable: true });
70
- }
71
- })(DType = exports.DType || (exports.DType = {}));
@@ -1,20 +0,0 @@
1
- import { Type } from "../../const/types";
2
- export declare const smbl_tm: unique symbol;
3
- export interface TypeDesc {
4
- t?: Type;
5
- req?: boolean;
6
- ary?: TypeDesc;
7
- rec?: boolean;
8
- }
9
- export interface TypeMap {
10
- [k: string]: TypeDesc;
11
- }
12
- /** decorators to be validated by {@link UType.validate} */
13
- export declare namespace DValidate {
14
- function string(target: Object, propKey: string): void;
15
- function number(target: Object, propKey: string): void;
16
- function boolean(target: Object, propKey: string): void;
17
- function required(target: Object, propKey: string): void;
18
- function array(elmDesc?: TypeDesc): (target: Object, propKey: string) => void;
19
- function recursive(target: Object, propKey: string): void;
20
- }