@qwreey-js/ts-util 1.0.3

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.
@@ -0,0 +1,22 @@
1
+ export declare class CachedGetter<Res> {
2
+ private func;
3
+ private interval;
4
+ private value?;
5
+ private last?;
6
+ constructor(func: () => Promise<Res>, interval: number);
7
+ getValue(): Promise<Res>;
8
+ }
9
+ export declare class CachedGetterMap<Res, Key> {
10
+ private func;
11
+ private interval;
12
+ private values;
13
+ constructor(func: (key: Key) => Promise<Res>, interval: number);
14
+ getValue(key: Key): Promise<Res>;
15
+ }
16
+ export declare namespace CachedGetterMap {
17
+ type Record<Res> = {
18
+ value: Promise<Res>;
19
+ last: number;
20
+ };
21
+ }
22
+ //# sourceMappingURL=cached-getter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cached-getter.d.ts","sourceRoot":"","sources":["../src/cached-getter.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAY,CAAC,GAAG;IAC3B,OAAO,CAAC,IAAI,CAAqB;IACjC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,KAAK,CAAC,CAAe;IAC7B,OAAO,CAAC,IAAI,CAAC,CAAS;gBAEH,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,MAAM;IAKhD,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC;CAStC;AAED,qBAAa,eAAe,CAAC,GAAG,EAAE,GAAG;IACnC,OAAO,CAAC,IAAI,CAA6B;IACzC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,MAAM,CAAwC;gBAEnC,IAAI,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,MAAM;IAMxD,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;CAc9C;AACD,yBAAiB,eAAe,CAAC;IAC/B,KAAY,MAAM,CAAC,GAAG,IAAI;QACxB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;QACpB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;CACH"}
@@ -0,0 +1,43 @@
1
+ export class CachedGetter {
2
+ func;
3
+ interval;
4
+ value;
5
+ last;
6
+ constructor(func, interval) {
7
+ this.func = func;
8
+ this.interval = interval;
9
+ }
10
+ async getValue() {
11
+ const curr = +new Date();
12
+ if (!this.value || !this.last || this.last + this.interval < curr) {
13
+ this.last = curr;
14
+ this.value = this.func();
15
+ return await this.value;
16
+ }
17
+ return await this.value;
18
+ }
19
+ }
20
+ export class CachedGetterMap {
21
+ func;
22
+ interval;
23
+ values;
24
+ constructor(func, interval) {
25
+ this.func = func;
26
+ this.values = new Map();
27
+ this.interval = interval;
28
+ }
29
+ async getValue(key) {
30
+ const curr = +new Date();
31
+ const record = this.values.get(key);
32
+ if (!record || record.last + this.interval < curr) {
33
+ const value = this.func(key);
34
+ this.values.set(key, {
35
+ last: curr,
36
+ value,
37
+ });
38
+ return await value;
39
+ }
40
+ return await record.value;
41
+ }
42
+ }
43
+ //# sourceMappingURL=cached-getter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cached-getter.js","sourceRoot":"","sources":["../src/cached-getter.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAY;IACf,IAAI,CAAqB;IACzB,QAAQ,CAAS;IACjB,KAAK,CAAgB;IACrB,IAAI,CAAU;IAEtB,YAAmB,IAAwB,EAAE,QAAgB;QAC3D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEM,KAAK,CAAC,QAAQ;QACnB,MAAM,IAAI,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC;QACzB,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,EAAE,CAAC;YAClE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;YACzB,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC;QAC1B,CAAC;QACD,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC;IAC1B,CAAC;CACF;AAED,MAAM,OAAO,eAAe;IAClB,IAAI,CAA6B;IACjC,QAAQ,CAAS;IACjB,MAAM,CAAwC;IAEtD,YAAmB,IAAgC,EAAE,QAAgB;QACnE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAC,GAAQ;QAC5B,MAAM,IAAI,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEpC,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,EAAE,CAAC;YAClD,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE;gBACnB,IAAI,EAAE,IAAI;gBACV,KAAK;aACN,CAAC,CAAC;YACH,OAAO,MAAM,KAAK,CAAC;QACrB,CAAC;QACD,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC;IAC5B,CAAC;CACF"}
package/dist/date.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare namespace DateUtil {
2
+ function removeTime(dateTime: Date): Date;
3
+ function removeDay(dateTime: Date): Date;
4
+ function formatYYYYMMDD(date: Date): string;
5
+ function formatUTCYYYYMMDD(date: Date): string;
6
+ function fromUTCYYYMMDD(str: string): Date;
7
+ function formatUTCHHMMSS(date: Date): string;
8
+ }
9
+ //# sourceMappingURL=date.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../src/date.ts"],"names":[],"mappings":"AAKA,yBAAiB,QAAQ,CAAC;IACxB,SAAgB,UAAU,CAAC,QAAQ,EAAE,IAAI,GAAG,IAAI,CAM/C;IAED,SAAgB,SAAS,CAAC,QAAQ,EAAE,IAAI,GAAG,IAAI,CAE9C;IAED,SAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAEjD;IACD,SAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAEpD;IACD,SAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAIhD;IACD,SAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAElD;CACF"}
package/dist/date.js ADDED
@@ -0,0 +1,33 @@
1
+ function zeroPadding(value) {
2
+ if (value > 10)
3
+ return value.toString();
4
+ return "0" + value.toString();
5
+ }
6
+ export var DateUtil;
7
+ (function (DateUtil) {
8
+ function removeTime(dateTime) {
9
+ return new Date(dateTime.getFullYear(), dateTime.getMonth(), dateTime.getDate());
10
+ }
11
+ DateUtil.removeTime = removeTime;
12
+ function removeDay(dateTime) {
13
+ return new Date(dateTime.getFullYear(), dateTime.getMonth());
14
+ }
15
+ DateUtil.removeDay = removeDay;
16
+ function formatYYYYMMDD(date) {
17
+ return `${date.getFullYear()}-${zeroPadding(date.getMonth() + 1)}-${zeroPadding(date.getDate())}`;
18
+ }
19
+ DateUtil.formatYYYYMMDD = formatYYYYMMDD;
20
+ function formatUTCYYYYMMDD(date) {
21
+ return `${date.getUTCFullYear()}-${zeroPadding(date.getUTCMonth() + 1)}-${zeroPadding(date.getUTCDate())}`;
22
+ }
23
+ DateUtil.formatUTCYYYYMMDD = formatUTCYYYYMMDD;
24
+ function fromUTCYYYMMDD(str) {
25
+ return new Date(`${str.slice(0, 4)}-${str.slice(5, 7)}-${str.slice(8, 10)}T00:00:00.000Z`);
26
+ }
27
+ DateUtil.fromUTCYYYMMDD = fromUTCYYYMMDD;
28
+ function formatUTCHHMMSS(date) {
29
+ return `${zeroPadding(date.getUTCHours())}:${zeroPadding(date.getUTCMinutes())}:${zeroPadding(date.getUTCSeconds())}`;
30
+ }
31
+ DateUtil.formatUTCHHMMSS = formatUTCHHMMSS;
32
+ })(DateUtil || (DateUtil = {}));
33
+ //# sourceMappingURL=date.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"date.js","sourceRoot":"","sources":["../src/date.ts"],"names":[],"mappings":"AAAA,SAAS,WAAW,CAAC,KAAa;IAChC,IAAI,KAAK,GAAG,EAAE;QAAE,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IACxC,OAAO,GAAG,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;AAChC,CAAC;AAED,MAAM,KAAW,QAAQ,CA2BxB;AA3BD,WAAiB,QAAQ;IACvB,SAAgB,UAAU,CAAC,QAAc;QACvC,OAAO,IAAI,IAAI,CACb,QAAQ,CAAC,WAAW,EAAE,EACtB,QAAQ,CAAC,QAAQ,EAAE,EACnB,QAAQ,CAAC,OAAO,EAAE,CACnB,CAAC;IACJ,CAAC;IANe,mBAAU,aAMzB,CAAA;IAED,SAAgB,SAAS,CAAC,QAAc;QACtC,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC/D,CAAC;IAFe,kBAAS,YAExB,CAAA;IAED,SAAgB,cAAc,CAAC,IAAU;QACvC,OAAO,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,WAAW,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;IACpG,CAAC;IAFe,uBAAc,iBAE7B,CAAA;IACD,SAAgB,iBAAiB,CAAC,IAAU;QAC1C,OAAO,GAAG,IAAI,CAAC,cAAc,EAAE,IAAI,WAAW,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC;IAC7G,CAAC;IAFe,0BAAiB,oBAEhC,CAAA;IACD,SAAgB,cAAc,CAAC,GAAW;QACxC,OAAO,IAAI,IAAI,CACb,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,gBAAgB,CAC1E,CAAC;IACJ,CAAC;IAJe,uBAAc,iBAI7B,CAAA;IACD,SAAgB,eAAe,CAAC,IAAU;QACxC,OAAO,GAAG,WAAW,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC,EAAE,CAAC;IACxH,CAAC;IAFe,wBAAe,kBAE9B,CAAA;AACH,CAAC,EA3BgB,QAAQ,KAAR,QAAQ,QA2BxB"}
@@ -0,0 +1,14 @@
1
+ export * from "./cached-getter.js";
2
+ export * from "./date.js";
3
+ export * from "./interval.js";
4
+ export * from "./json.js";
5
+ export * from "./mixin.js";
6
+ export * from "./promise.js";
7
+ export * from "./result.js";
8
+ export * from "./utils.js";
9
+ import { setLogErr as _setLogErr } from "./libLog.js";
10
+ export declare namespace UtilLogging {
11
+ const setLogErr: typeof _setLogErr;
12
+ const logErr: import("./libLog.js").LogErr;
13
+ }
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,SAAS,IAAI,UAAU,EAAqB,MAAM,aAAa,CAAC;AACzE,yBAAiB,WAAW,CAAC;IACpB,MAAM,SAAS,mBAAa,CAAC;IAC7B,MAAM,MAAM,8BAAU,CAAC;CAC/B"}
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ export * from "./cached-getter.js";
2
+ export * from "./date.js";
3
+ export * from "./interval.js";
4
+ export * from "./json.js";
5
+ export * from "./mixin.js";
6
+ export * from "./promise.js";
7
+ export * from "./result.js";
8
+ export * from "./utils.js";
9
+ import { setLogErr as _setLogErr, logErr as _logErr } from "./libLog.js";
10
+ export var UtilLogging;
11
+ (function (UtilLogging) {
12
+ UtilLogging.setLogErr = _setLogErr;
13
+ UtilLogging.logErr = _logErr;
14
+ })(UtilLogging || (UtilLogging = {}));
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,SAAS,IAAI,UAAU,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,aAAa,CAAC;AACzE,MAAM,KAAW,WAAW,CAG3B;AAHD,WAAiB,WAAW;IACb,qBAAS,GAAG,UAAU,CAAC;IACvB,kBAAM,GAAG,OAAO,CAAC;AAChC,CAAC,EAHgB,WAAW,KAAX,WAAW,QAG3B"}
@@ -0,0 +1,16 @@
1
+ export declare class Interval {
2
+ private func;
3
+ private interval;
4
+ private id?;
5
+ private running;
6
+ private name;
7
+ constructor(func: () => any, interval: number, name?: string);
8
+ run(): void;
9
+ drop(): void;
10
+ executeAsync(): Promise<void>;
11
+ execute(options?: {
12
+ runTimerAfterExecute?: boolean;
13
+ runIn?: number;
14
+ }): void;
15
+ }
16
+ //# sourceMappingURL=interval.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interval.d.ts","sourceRoot":"","sources":["../src/interval.ts"],"names":[],"mappings":"AAMA,qBAAa,QAAQ;IACnB,OAAO,CAAC,IAAI,CAAY;IACxB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,EAAE,CAAC,CAAM;IACjB,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,IAAI,CAAS;gBAGnB,IAAI,EAAE,MAAM,GAAG,EACf,QAAQ,EAAE,MAAM,EAChB,IAAI,GAAE,MAAkB;IAQnB,GAAG;IAgBH,IAAI;IAQE,YAAY;IAWlB,OAAO,CACZ,OAAO,GAAE;QACP,oBAAoB,CAAC,EAAE,OAAO,CAAC;QAC/B,KAAK,CAAC,EAAE,MAAM,CAAC;KACX;CAST"}
@@ -0,0 +1,63 @@
1
+ import { logErr } from "./libLog.js";
2
+ // 입력받은 시간을 기준으로 함수를 반복 실행합니다.
3
+ // 처음 실행되지는 않습니다. drop 하거나 execute 할 수 있습니다
4
+ // execute 시 시간은 초기화됩니다 (0 초 부터 다시 카운팅)
5
+ // 내부 구현은 setInterval 로 구현됩니다
6
+ export class Interval {
7
+ func;
8
+ interval;
9
+ id;
10
+ running;
11
+ name;
12
+ constructor(func, interval, name = "default") {
13
+ this.func = func;
14
+ this.interval = interval;
15
+ this.running = false;
16
+ this.name = name;
17
+ }
18
+ run() {
19
+ if (this.running) {
20
+ throw Error("interval already started");
21
+ }
22
+ this.running = true;
23
+ this.id = setTimeout(async () => {
24
+ try {
25
+ await (this.func() ?? null);
26
+ }
27
+ catch (e) {
28
+ logErr(`interval failed(${this.name}): ${e}`);
29
+ }
30
+ this.running = false;
31
+ this.run();
32
+ }, this.interval);
33
+ }
34
+ drop() {
35
+ this.running = false;
36
+ if (this.id) {
37
+ clearTimeout(this.id);
38
+ delete this.id;
39
+ }
40
+ }
41
+ async executeAsync() {
42
+ const running = this.running;
43
+ if (running)
44
+ this.drop();
45
+ try {
46
+ await (this.func() ?? null);
47
+ }
48
+ catch (e) {
49
+ logErr(`interval failed(${this.name}): ${e}`);
50
+ }
51
+ if (running)
52
+ this.run();
53
+ }
54
+ execute(options = {}) {
55
+ setTimeout(async () => {
56
+ await this.executeAsync();
57
+ if (options.runTimerAfterExecute && !this.running) {
58
+ this.run();
59
+ }
60
+ }, options.runIn ?? 0);
61
+ }
62
+ }
63
+ //# sourceMappingURL=interval.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interval.js","sourceRoot":"","sources":["../src/interval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,8BAA8B;AAC9B,2CAA2C;AAC3C,uCAAuC;AACvC,6BAA6B;AAC7B,MAAM,OAAO,QAAQ;IACX,IAAI,CAAY;IAChB,QAAQ,CAAS;IACjB,EAAE,CAAO;IACT,OAAO,CAAU;IACjB,IAAI,CAAS;IAErB,YACE,IAAe,EACf,QAAgB,EAChB,OAAe,SAAS;QAExB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAEM,GAAG;QACR,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,MAAM,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAC1C,CAAC;QACD,IAAI,CAAC,OAAO,GA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AAI,CAAC,GAAG,EAAE,CAAC;YACb,CAAC;QACH,CAAC,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC;IACzB,CAAC;CACF"}
package/dist/json.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function parseJsonSafe<T = any>(content: string | undefined | null): T | null;
2
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,wBAAgB,aAAa,CAAC,CAAC,GAAG,GAAG,EACnC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GACjC,CAAC,GAAG,IAAI,CAOV"}
package/dist/json.js ADDED
@@ -0,0 +1,11 @@
1
+ export function parseJsonSafe(content) {
2
+ if (!content)
3
+ return null;
4
+ try {
5
+ return JSON.parse(content);
6
+ }
7
+ catch {
8
+ return null;
9
+ }
10
+ }
11
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,aAAa,CAC3B,OAAkC;IAElC,IAAI,CAAC,OAAO;QAAE,OAAO,IAAI,CAAC;IAC1B,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,4 @@
1
+ export type LogErr = (msg: string) => any;
2
+ export declare const logErr: LogErr;
3
+ export declare function setLogErr(newLogger: LogErr): void;
4
+ //# sourceMappingURL=libLog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libLog.d.ts","sourceRoot":"","sources":["../src/libLog.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,GAAG,CAAC;AAG1C,eAAO,MAAM,MAAM,EAAE,MAEpB,CAAC;AACF,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,QAE1C"}
package/dist/libLog.js ADDED
@@ -0,0 +1,8 @@
1
+ let logErrInner = console.log.bind(console);
2
+ export const logErr = (msg) => {
3
+ logErrInner(msg);
4
+ };
5
+ export function setLogErr(newLogger) {
6
+ logErrInner = newLogger;
7
+ }
8
+ //# sourceMappingURL=libLog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libLog.js","sourceRoot":"","sources":["../src/libLog.ts"],"names":[],"mappings":"AAEA,IAAI,WAAW,GAAW,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACpD,MAAM,CAAC,MAAM,MAAM,GAAW,CAAC,GAAW,EAAE,EAAE;IAC5C,WAAW,CAAC,GAAG,CAAC,CAAC;AACnB,CAAC,CAAC;AACF,MAAM,UAAU,SAAS,CAAC,SAAiB;IACzC,WAAW,GAAG,SAAS,CAAC;AAC1B,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { UnionToIntersection } from "./utils.js";
2
+ export type AbstractConstructor<T = any> = abstract new (...args: any[]) => T;
3
+ export type MixinBoth<A extends AbstractConstructor<InstanceType<A>>, B extends AbstractConstructor<InstanceType<B>>> = (A & B) & AbstractConstructor<InstanceType<A> & InstanceType<B>>;
4
+ export type Mixin<T extends AbstractConstructor[]> = T extends [
5
+ infer First extends AbstractConstructor
6
+ ] ? First : T extends [
7
+ ...infer Others extends AbstractConstructor[],
8
+ infer Last extends AbstractConstructor
9
+ ] ? MixinBoth<Mixin<Others>, Last> : never;
10
+ export declare function applyRuntimeMixins(targetClass: any, baseClassList: any[]): void;
11
+ export declare function mixin<T extends AbstractConstructor[]>(...baseClassList: T): Mixin<T>;
12
+ export type MixinApplyList = [
13
+ boolean | undefined | null,
14
+ AbstractConstructor
15
+ ][];
16
+ export type MixinInstance<Apply extends MixinApplyList> = UnionToIntersection<{
17
+ [K in keyof Apply]: Apply[K][0] extends true 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,aAAa,CAAC,KAAK,SAAS,cAAc,IAAI,mBAAmB,CAC3E;KACG,CAAC,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GACxC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GACzB,MAAM;CACX,CAAC,MAAM,CAAC,CACV,CAAC"}
package/dist/mixin.js ADDED
@@ -0,0 +1,27 @@
1
+ export function applyRuntimeMixins(targetClass, baseClassList) {
2
+ for (const baseClass of baseClassList) {
3
+ Object.getOwnPropertyNames(baseClass.prototype).forEach((name) => {
4
+ if (name !== "constructor") {
5
+ const descriptor = Object.getOwnPropertyDescriptor(baseClass.prototype, name);
6
+ if (descriptor) {
7
+ Object.defineProperty(targetClass.prototype, name, descriptor);
8
+ }
9
+ }
10
+ });
11
+ for (const name of Object.getOwnPropertyNames(baseClass)) {
12
+ if (!["length", "prototype", "name"].includes(name)) {
13
+ const descriptor = Object.getOwnPropertyDescriptor(baseClass, name);
14
+ if (descriptor) {
15
+ Object.defineProperty(targetClass, name, descriptor);
16
+ }
17
+ }
18
+ }
19
+ }
20
+ }
21
+ export function mixin(...baseClassList) {
22
+ const targetClass = class {
23
+ };
24
+ applyRuntimeMixins(targetClass, baseClassList);
25
+ return targetClass;
26
+ }
27
+ //# sourceMappingURL=mixin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mixin.js","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAoBA,MAAM,UAAU,kBAAkB,CAAC,WAAgB,EAAE,aAAoB;IACvE,KAAK,MAAM,SAAS,IAAI,aAAa,EAAE,CAAC;QACtC,MAAM,CAAC,mBAAmB,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC/D,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;gBAC3B,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAChD,SAAS,CAAC,SAAS,EACnB,IAAI,CACL,CAAC;gBACF,IAAI,UAAU,EAAE,CAAC;oBACf,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC;gBACjE,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,mBAAmB,CAAC,SAAS,CAAC,EAAE,CAAC;YACzD,IAAI,CAAC,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;gBACpD,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;gBAEpE,IAAI,UAAU,EAAE,CAAC;oBACf,MAAM,CAAC,cAAc,CAAC,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC;gBACvD,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,KAAK,CACnB,GAAG,aAAgB;IAEnB,MAAM,WAAW,GAAG;KAAQ,CAAC;IAC7B,kBAAkB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;IAC/C,OAAO,WAAkB,CAAC;AAC5B,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare function AsOk(promise: Promise<any>): Promise<boolean>;
2
+ export declare function NullCatch<T>(promise: Promise<T>): Promise<T | null>;
3
+ export declare function EmptyPromise(): Promise<undefined>;
4
+ export declare function ErrorPromise(err: Error): Promise<any>;
5
+ //# sourceMappingURL=promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAEA,wBAAgB,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAO5D;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAKnE;AAED,wBAAsB,YAAY,IAAI,OAAO,CAAC,SAAS,CAAC,CAAG;AAC3D,wBAAsB,YAAY,CAAC,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAE3D"}
@@ -0,0 +1,20 @@
1
+ import { logErr } from "./libLog.js";
2
+ export function AsOk(promise) {
3
+ return promise
4
+ .then(() => true)
5
+ .catch((i) => {
6
+ logErr("Promise throw(catch in AsOk): " + i);
7
+ return false;
8
+ });
9
+ }
10
+ export function NullCatch(promise) {
11
+ return promise.catch((i) => {
12
+ logErr("Promise throw(catch in NullCatch): " + i);
13
+ return null;
14
+ });
15
+ }
16
+ export async function EmptyPromise() { }
17
+ export async function ErrorPromise(err) {
18
+ throw err;
19
+ }
20
+ //# sourceMappingURL=promise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.js","sourceRoot":"","sources":["../src/promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,UAAU,IAAI,CAAC,OAAqB;IACxC,OAAO,OAAO;SACX,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;SAChB,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;QACX,MAAM,CAAC,gCAAgC,GAAG,CAAC,CAAC,CAAC;QAC7C,OAAO,KAAK,CAAC;IACf,CAAC,CAAC,CAAC;AACP,CAAC;AAED,MAAM,UAAU,SAAS,CAAI,OAAmB;IAC9C,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;QACzB,MAAM,CAAC,qCAAqC,GAAG,CAAC,CAAC,CAAC;QAClD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,KAAwB,CAAC;AAC3D,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,GAAU;IAC3C,MAAM,GAAG,CAAC;AACZ,CAAC"}
@@ -0,0 +1,18 @@
1
+ export type Result<T, U> = {
2
+ result: undefined;
3
+ error: U;
4
+ } | {
5
+ result: T;
6
+ error: undefined;
7
+ };
8
+ export declare namespace Result {
9
+ function ok<T>(result: T): {
10
+ result: T;
11
+ error: undefined;
12
+ };
13
+ function err<U>(error: U): {
14
+ result: undefined;
15
+ error: U;
16
+ };
17
+ }
18
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IACnB;IACE,MAAM,EAAE,SAAS,CAAC;IAClB,KAAK,EAAE,CAAC,CAAC;CACV,GACD;IACE,MAAM,EAAE,CAAC,CAAC;IACV,KAAK,EAAE,SAAS,CAAC;CAClB,CAAC;AAEN,yBAAiB,MAAM,CAAC;IACtB,SAAgB,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG;QAAE,MAAM,EAAE,CAAC,CAAC;QAAC,KAAK,EAAE,SAAS,CAAA;KAAE,CAEhE;IACD,SAAgB,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG;QAAE,MAAM,EAAE,SAAS,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,CAEhE;CACF"}
package/dist/result.js ADDED
@@ -0,0 +1,12 @@
1
+ export var Result;
2
+ (function (Result) {
3
+ function ok(result) {
4
+ return { error: undefined, result };
5
+ }
6
+ Result.ok = ok;
7
+ function err(error) {
8
+ return { error, result: undefined };
9
+ }
10
+ Result.err = err;
11
+ })(Result || (Result = {}));
12
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAUA,MAAM,KAAW,MAAM,CAOtB;AAPD,WAAiB,MAAM;IACrB,SAAgB,EAAE,CAAI,MAAS;QAC7B,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IACtC,CAAC;IAFe,SAAE,KAEjB,CAAA;IACD,SAAgB,GAAG,CAAI,KAAQ;QAC7B,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACtC,CAAC;IAFe,UAAG,MAElB,CAAA;AACH,CAAC,EAPgB,MAAM,KAAN,MAAM,QAOtB"}
@@ -0,0 +1,2 @@
1
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CACnC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,GAAG,KAAK,CACvC,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,IAAI,GAC1B,CAAC,GACD,KAAK,CAAC"}
package/dist/utils.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@qwreey-js/ts-util",
3
+ "version": "1.0.3",
4
+ "description": "Qwreey's typescript utilities",
5
+ "keywords": [
6
+ "typescript"
7
+ ],
8
+ "homepage": "https://github.com/qwreey/qwreey-js#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/qwreey/qwreey-js/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/qwreey/qwreey-js.git"
15
+ },
16
+ "license": "MIT",
17
+ "author": "me@qwreey.moe",
18
+ "type": "module",
19
+ "main": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "scripts": {
22
+ "build": "../../node_modules/typescript/bin/tsc",
23
+ "check": "npm run check:tsc && npm run check:eslint && npm run check:format",
24
+ "check:tsc": "../../node_modules/typescript/bin/tsc --noemit",
25
+ "check:eslint": "npm run --prefix ../../ check:eslint -- packages/ts-util/src",
26
+ "check:format": "npm run --prefix ../../ prettier -- --check packages/ts-util/src",
27
+ "fix:format": "npm run --prefix ../../ prettier -- --write packages/ts-util/src",
28
+ "init": "npm ci --include-workspace-root --include=dev",
29
+ "prepack": "npm run init && npm run check && npm run build"
30
+ },
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/index.d.ts",
34
+ "import": "./dist/index.js"
35
+ }
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "files": [
41
+ "dist", "!dist/test"
42
+ ]
43
+ }