@qwreey-js/ts-util 1.0.3 → 1.0.5
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/dist/base32.d.ts +16 -0
- package/dist/base32.d.ts.map +1 -0
- package/dist/base32.js +67 -0
- package/dist/base32.js.map +1 -0
- package/dist/base64.d.ts +34 -0
- package/dist/base64.d.ts.map +1 -0
- package/dist/base64.js +81 -0
- package/dist/base64.js.map +1 -0
- package/dist/cached-getter.d.ts +22 -0
- package/dist/cached-getter.d.ts.map +1 -1
- package/dist/cached-getter.js +22 -0
- package/dist/cached-getter.js.map +1 -1
- package/dist/color.d.ts +72 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +187 -0
- package/dist/color.js.map +1 -0
- package/dist/date.d.ts +37 -4
- package/dist/date.d.ts.map +1 -1
- package/dist/date.js +105 -14
- package/dist/date.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/interval.d.ts +66 -7
- package/dist/interval.d.ts.map +1 -1
- package/dist/interval.js +123 -16
- package/dist/interval.js.map +1 -1
- package/dist/mixin.d.ts +45 -0
- package/dist/mixin.d.ts.map +1 -1
- package/dist/mixin.js +77 -12
- package/dist/mixin.js.map +1 -1
- package/dist/params-builder.d.ts +31 -0
- package/dist/params-builder.d.ts.map +1 -0
- package/dist/params-builder.js +40 -0
- package/dist/params-builder.js.map +1 -0
- package/dist/result.d.ts +13 -0
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +10 -0
- package/dist/result.js.map +1 -1
- package/dist/timeid.d.ts +24 -0
- package/dist/timeid.d.ts.map +1 -0
- package/dist/timeid.js +62 -0
- package/dist/timeid.js.map +1 -0
- package/dist/utils.d.ts +81 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +141 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -2
package/dist/mixin.js
CHANGED
|
@@ -1,25 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copies prototype methods and static properties from a list of base classes directly onto a target class.
|
|
3
|
+
* @param targetClass The class to which the mixins will be applied.
|
|
4
|
+
* @param baseClassList An array of base classes to mix into the `targetClass`.
|
|
5
|
+
*/
|
|
1
6
|
export function applyRuntimeMixins(targetClass, baseClassList) {
|
|
2
7
|
for (const baseClass of baseClassList) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (
|
|
7
|
-
Object.
|
|
8
|
+
let currentProto = baseClass.prototype;
|
|
9
|
+
while (currentProto && currentProto !== Object.prototype) {
|
|
10
|
+
Object.getOwnPropertyNames(currentProto).forEach((name) => {
|
|
11
|
+
if (name !== "constructor") {
|
|
12
|
+
const descriptor = Object.getOwnPropertyDescriptor(currentProto, name);
|
|
13
|
+
if (descriptor) {
|
|
14
|
+
Object.defineProperty(targetClass.prototype, name, descriptor);
|
|
15
|
+
}
|
|
8
16
|
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
});
|
|
18
|
+
currentProto = Object.getPrototypeOf(currentProto);
|
|
19
|
+
}
|
|
20
|
+
let currentClass = baseClass;
|
|
21
|
+
while (currentClass &&
|
|
22
|
+
currentClass !== Function.prototype &&
|
|
23
|
+
currentClass !== Object) {
|
|
24
|
+
for (const name of Object.getOwnPropertyNames(currentClass)) {
|
|
25
|
+
if (!["length", "prototype", "name"].includes(name)) {
|
|
26
|
+
const descriptor = Object.getOwnPropertyDescriptor(currentClass, name);
|
|
27
|
+
if (descriptor) {
|
|
28
|
+
Object.defineProperty(targetClass, name, descriptor);
|
|
29
|
+
}
|
|
16
30
|
}
|
|
17
31
|
}
|
|
32
|
+
currentClass = Object.getPrototypeOf(currentClass);
|
|
18
33
|
}
|
|
19
34
|
}
|
|
20
35
|
}
|
|
36
|
+
export function isNonNullObject(instance) {
|
|
37
|
+
if (typeof instance !== "object")
|
|
38
|
+
return false;
|
|
39
|
+
if (instance === null)
|
|
40
|
+
return false;
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether the given instance incorporates a specific mixin or base class.
|
|
45
|
+
* This utility serves as an alternative to the native `instanceof` operator for custom
|
|
46
|
+
* mixin implementations where the prototype chain does not inherently preserve the relationship.
|
|
47
|
+
*
|
|
48
|
+
* @template T - The constructor type of the target mixin or base class.
|
|
49
|
+
* @param {T} baseClass - The mixin or base class constructor to verify against.
|
|
50
|
+
* @param {*} instance - The object instance to inspect.
|
|
51
|
+
* @returns {instance is T} `true` if the instance incorporates the specified mixin; otherwise, `false`.
|
|
52
|
+
*/
|
|
53
|
+
export function hasMixin(baseClass, instance) {
|
|
54
|
+
const mixins = getMixins(instance);
|
|
55
|
+
if (!mixins)
|
|
56
|
+
return false;
|
|
57
|
+
return mixins.includes(baseClass);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves the sequence of mixins associated with the specified instance.
|
|
61
|
+
*
|
|
62
|
+
* @param {*} instance - The object instance to inspect.
|
|
63
|
+
* @returns {ReadonlyArray<AbstractConstructor> | undefined} A read-only array of mixin constructors applied to the instance, or `undefined` if no mixins are registered.
|
|
64
|
+
*/
|
|
65
|
+
export function getMixins(instance) {
|
|
66
|
+
if (!isNonNullObject(instance))
|
|
67
|
+
return;
|
|
68
|
+
if (!("constructor" in instance))
|
|
69
|
+
return;
|
|
70
|
+
const instConstructor = instance.constructor;
|
|
71
|
+
if (typeof instConstructor !== "function")
|
|
72
|
+
return;
|
|
73
|
+
if (!("$baseClassList" in instConstructor))
|
|
74
|
+
return;
|
|
75
|
+
const baseClassList = instConstructor["$baseClassList"];
|
|
76
|
+
if (!Array.isArray(baseClassList))
|
|
77
|
+
return;
|
|
78
|
+
return baseClassList;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Creates an anonymous target class, applies the `baseClassList` to it, and returns the constructed mixin class.
|
|
82
|
+
* @param baseClassList A list of base classes you want to compose.
|
|
83
|
+
* @returns A new class constructor containing all instance and static properties of the base classes.
|
|
84
|
+
*/
|
|
21
85
|
export function mixin(...baseClassList) {
|
|
22
86
|
const targetClass = class {
|
|
87
|
+
static $baseClassList = baseClassList;
|
|
23
88
|
};
|
|
24
89
|
applyRuntimeMixins(targetClass, baseClassList);
|
|
25
90
|
return targetClass;
|
package/dist/mixin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mixin.js","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mixin.js","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AA8BA;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,WAAgB,EAAE,aAAoB;IACvE,KAAK,MAAM,SAAS,IAAI,aAAa,EAAE,CAAC;QACtC,IAAI,YAAY,GAAG,SAAS,CAAC,SAAS,CAAC;QACvC,OAAO,YAAY,IAAI,YAAY,KAAK,MAAM,CAAC,SAAS,EAAE,CAAC;YACzD,MAAM,CAAC,mBAAmB,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;gBACxD,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;oBAC3B,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAChD,YAAY,EACZ,IAAI,CACL,CAAC;oBACF,IAAI,UAAU,EAAE,CAAC;wBACf,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC;oBACjE,CAAC;gBACH,CAAC;YACH,CAAC,CAAC,CAAC;YACH,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;QACrD,CAAC;QAED,IAAI,YAAY,GAAG,SAAS,CAAC;QAC7B,OACE,YAAY;YACZ,YAAY,KAAK,QAAQ,CAAC,SAAS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IAClD,IAAI,CAAC,CAAC,gBAAgB,IAAI,eAAe,CAAC;QAAE,OAAO;IACnD,MAAM,aAAa,GAAG,eAAe,CAAC,gBAAgB,CAAQ,CAAC;IAC/D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC;QAAE,OAAO;IAE1C,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,KAAK,CACnB,GAAG,aAAgB;IAEnB,MAAM,WAAW,GAAG;QAClB,MAAM,CAAC,cAAc,GAAG,aAAa,CAAC;KACvC,CAAC;IACF,kBAAkB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;IAC/C,OAAO,WAAkB,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** URLSearchParams builder pattern wrapper */
|
|
2
|
+
export declare class ParamsBuilder {
|
|
3
|
+
private inner;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new ParamsBuilder instance.
|
|
6
|
+
* @param base - An optional existing {@link URLSearchParams} to wrap.
|
|
7
|
+
*/
|
|
8
|
+
constructor(base?: URLSearchParams);
|
|
9
|
+
/**
|
|
10
|
+
* Appends a new query parameter.
|
|
11
|
+
* @param name - The name of the parameter.
|
|
12
|
+
* @param value - The value of the parameter.
|
|
13
|
+
* @returns The current instance for method chaining.
|
|
14
|
+
*/
|
|
15
|
+
push(name: string, value: string): ParamsBuilder;
|
|
16
|
+
/**
|
|
17
|
+
* Finalizes the builder and returns the query string.
|
|
18
|
+
* @returns The serialized query parameters as a string.
|
|
19
|
+
*/
|
|
20
|
+
finalize(): string;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the underlying {@link URLSearchParams} instance.
|
|
23
|
+
*
|
|
24
|
+
* @note **Terminal Operation**: This method transfers ownership of the
|
|
25
|
+
* internal state. Do not use this builder instance after calling this method.
|
|
26
|
+
*
|
|
27
|
+
* @returns The raw URLSearchParams object.
|
|
28
|
+
*/
|
|
29
|
+
asInner(): URLSearchParams;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=params-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params-builder.d.ts","sourceRoot":"","sources":["../src/params-builder.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,qBAAa,aAAa;IACxB,OAAO,CAAC,KAAK,CAAkB;IAE/B;;;OAGG;gBACgB,IAAI,CAAC,EAAE,eAAe;IAIzC;;;;;OAKG;IACI,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,aAAa;IAKvD;;;OAGG;IACI,QAAQ,IAAI,MAAM;IAIzB;;;;;;;OAOG;IACI,OAAO,IAAI,eAAe;CAGlC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** URLSearchParams builder pattern wrapper */
|
|
2
|
+
export class ParamsBuilder {
|
|
3
|
+
inner;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new ParamsBuilder instance.
|
|
6
|
+
* @param base - An optional existing {@link URLSearchParams} to wrap.
|
|
7
|
+
*/
|
|
8
|
+
constructor(base) {
|
|
9
|
+
this.inner = base ?? new URLSearchParams();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Appends a new query parameter.
|
|
13
|
+
* @param name - The name of the parameter.
|
|
14
|
+
* @param value - The value of the parameter.
|
|
15
|
+
* @returns The current instance for method chaining.
|
|
16
|
+
*/
|
|
17
|
+
push(name, value) {
|
|
18
|
+
this.inner.append(name, value);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Finalizes the builder and returns the query string.
|
|
23
|
+
* @returns The serialized query parameters as a string.
|
|
24
|
+
*/
|
|
25
|
+
finalize() {
|
|
26
|
+
return this.inner.toString();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns the underlying {@link URLSearchParams} instance.
|
|
30
|
+
*
|
|
31
|
+
* @note **Terminal Operation**: This method transfers ownership of the
|
|
32
|
+
* internal state. Do not use this builder instance after calling this method.
|
|
33
|
+
*
|
|
34
|
+
* @returns The raw URLSearchParams object.
|
|
35
|
+
*/
|
|
36
|
+
asInner() {
|
|
37
|
+
return this.inner;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=params-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params-builder.js","sourceRoot":"","sources":["../src/params-builder.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,MAAM,OAAO,aAAa;IAChB,KAAK,CAAkB;IAE/B;;;OAGG;IACH,YAAmB,IAAsB;QACvC,IAAI,CAAC,KAAK,GAAG,IAAI,IAAI,IAAI,eAAe,EAAE,CAAC;IAC7C,CAAC;IAED;;;;;OAKG;IACI,IAAI,CAAC,IAAY,EAAE,KAAa;QACrC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACI,QAAQ;QACb,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC/B,CAAC;IAED;;;;;;;OAOG;IACI,OAAO;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF"}
|
package/dist/result.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A discriminated union type that can represent either a successful value of type `T` or an error of type `U`.
|
|
3
|
+
*/
|
|
1
4
|
export type Result<T, U> = {
|
|
2
5
|
result: undefined;
|
|
3
6
|
error: U;
|
|
@@ -6,13 +9,23 @@ export type Result<T, U> = {
|
|
|
6
9
|
error: undefined;
|
|
7
10
|
};
|
|
8
11
|
export declare namespace Result {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a successful result object containing the provided `result` value.
|
|
14
|
+
* @param result The successful value to wrap.
|
|
15
|
+
*/
|
|
9
16
|
function ok<T>(result: T): {
|
|
10
17
|
result: T;
|
|
11
18
|
error: undefined;
|
|
12
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Creates an error result object containing the provided `error` value.
|
|
22
|
+
* @param error The error value or object to wrap.
|
|
23
|
+
*/
|
|
13
24
|
function err<U>(error: U): {
|
|
14
25
|
result: undefined;
|
|
15
26
|
error: U;
|
|
16
27
|
};
|
|
17
28
|
}
|
|
29
|
+
export declare const Ok: typeof Result.ok;
|
|
30
|
+
export declare const Err: typeof Result.err;
|
|
18
31
|
//# sourceMappingURL=result.d.ts.map
|
package/dist/result.d.ts.map
CHANGED
|
@@ -1 +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;
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,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;;;OAGG;IACH,SAAgB,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG;QAAE,MAAM,EAAE,CAAC,CAAC;QAAC,KAAK,EAAE,SAAS,CAAA;KAAE,CAEhE;IAED;;;OAGG;IACH,SAAgB,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG;QAAE,MAAM,EAAE,SAAS,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,CAEhE;CACF;AAED,eAAO,MAAM,EAAE,kBAAY,CAAC;AAC5B,eAAO,MAAM,GAAG,mBAAa,CAAC"}
|
package/dist/result.js
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
export var Result;
|
|
2
2
|
(function (Result) {
|
|
3
|
+
/**
|
|
4
|
+
* Creates a successful result object containing the provided `result` value.
|
|
5
|
+
* @param result The successful value to wrap.
|
|
6
|
+
*/
|
|
3
7
|
function ok(result) {
|
|
4
8
|
return { error: undefined, result };
|
|
5
9
|
}
|
|
6
10
|
Result.ok = ok;
|
|
11
|
+
/**
|
|
12
|
+
* Creates an error result object containing the provided `error` value.
|
|
13
|
+
* @param error The error value or object to wrap.
|
|
14
|
+
*/
|
|
7
15
|
function err(error) {
|
|
8
16
|
return { error, result: undefined };
|
|
9
17
|
}
|
|
10
18
|
Result.err = err;
|
|
11
19
|
})(Result || (Result = {}));
|
|
20
|
+
export const Ok = Result.ok;
|
|
21
|
+
export const Err = Result.err;
|
|
12
22
|
//# sourceMappingURL=result.js.map
|
package/dist/result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAaA,MAAM,KAAW,MAAM,CAgBtB;AAhBD,WAAiB,MAAM;IACrB;;;OAGG;IACH,SAAgB,EAAE,CAAI,MAAS;QAC7B,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IACtC,CAAC;IAFe,SAAE,KAEjB,CAAA;IAED;;;OAGG;IACH,SAAgB,GAAG,CAAI,KAAQ;QAC7B,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IACtC,CAAC;IAFe,UAAG,MAElB,CAAA;AACH,CAAC,EAhBgB,MAAM,KAAN,MAAM,QAgBtB;AAED,MAAM,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;AAC5B,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC"}
|
package/dist/timeid.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare namespace TimeId {
|
|
2
|
+
/**
|
|
3
|
+
* Generates a unique, time-sortable identifier string.
|
|
4
|
+
*
|
|
5
|
+
* The identifier is composed of four hyphen-separated segments:
|
|
6
|
+
* - The first two segments encode the current millisecond timestamp.
|
|
7
|
+
* - The last two segments (16 characters) contain random entropy.
|
|
8
|
+
*
|
|
9
|
+
* @returns {string} A unique ID string (e.g., "XXXX-XXXX-YYYY-YYYY").
|
|
10
|
+
*/
|
|
11
|
+
function create(timebase?: Date | null): string;
|
|
12
|
+
/**
|
|
13
|
+
* Parses a timeId string and returns the original Date and random bytes.
|
|
14
|
+
*
|
|
15
|
+
* @param id The timeId string to parse.
|
|
16
|
+
* @returns An object containing the decoded `date` and `randbytes`.
|
|
17
|
+
* @throws If the timeId format or payload is invalid.
|
|
18
|
+
*/
|
|
19
|
+
function parse(id: string): {
|
|
20
|
+
date: Date;
|
|
21
|
+
randbytes: Uint8Array;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=timeid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeid.d.ts","sourceRoot":"","sources":["../src/timeid.ts"],"names":[],"mappings":"AAEA,yBAAiB,MAAM,CAAC;IACtB;;;;;;;;OAQG;IACH,SAAgB,MAAM,CAAC,QAAQ,GAAE,IAAI,GAAG,IAAW,GAAG,MAAM,CAsB3D;IAED;;;;;;OAMG;IACH,SAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,SAAS,EAAE,UAAU,CAAA;KAAE,CA+BvE;CACF"}
|
package/dist/timeid.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Base32 } from "./base32.js";
|
|
2
|
+
export var TimeId;
|
|
3
|
+
(function (TimeId) {
|
|
4
|
+
/**
|
|
5
|
+
* Generates a unique, time-sortable identifier string.
|
|
6
|
+
*
|
|
7
|
+
* The identifier is composed of four hyphen-separated segments:
|
|
8
|
+
* - The first two segments encode the current millisecond timestamp.
|
|
9
|
+
* - The last two segments (16 characters) contain random entropy.
|
|
10
|
+
*
|
|
11
|
+
* @returns {string} A unique ID string (e.g., "XXXX-XXXX-YYYY-YYYY").
|
|
12
|
+
*/
|
|
13
|
+
function create(timebase = null) {
|
|
14
|
+
// Create random values
|
|
15
|
+
const randbytes = crypto.getRandomValues(new Uint8Array(8));
|
|
16
|
+
const k32 = Base32.fromUint8Array(randbytes);
|
|
17
|
+
const lower = k32.substring(0, 8);
|
|
18
|
+
const upper = k32.substring(8, 16);
|
|
19
|
+
const timepad = k32.substring(16, 19);
|
|
20
|
+
// Construct time Uint8Array
|
|
21
|
+
timebase ??= new Date();
|
|
22
|
+
const timestamp = BigInt(timebase.getTime());
|
|
23
|
+
const timebuffer = new ArrayBuffer(8);
|
|
24
|
+
const timeview = new DataView(timebuffer);
|
|
25
|
+
timeview.setBigUint64(0, timestamp, false);
|
|
26
|
+
const timearr = new Uint8Array(timebuffer);
|
|
27
|
+
// Assemble id
|
|
28
|
+
const timestr = Base32.fromUint8Array(timearr).substring(0, 13) + timepad;
|
|
29
|
+
const timelower = timestr.substring(0, 8);
|
|
30
|
+
const timeupper = timestr.substring(8, 16);
|
|
31
|
+
const generated = `${timelower}-${timeupper}-${lower}-${upper}`;
|
|
32
|
+
return generated;
|
|
33
|
+
}
|
|
34
|
+
TimeId.create = create;
|
|
35
|
+
/**
|
|
36
|
+
* Parses a timeId string and returns the original Date and random bytes.
|
|
37
|
+
*
|
|
38
|
+
* @param id The timeId string to parse.
|
|
39
|
+
* @returns An object containing the decoded `date` and `randbytes`.
|
|
40
|
+
* @throws If the timeId format or payload is invalid.
|
|
41
|
+
*/
|
|
42
|
+
function parse(id) {
|
|
43
|
+
const parts = id.split("-");
|
|
44
|
+
if (parts.length !== 4) {
|
|
45
|
+
throw new Error("Invalid timeId format");
|
|
46
|
+
}
|
|
47
|
+
const [timeLower, timeUpper, randLower, randUpper] = parts;
|
|
48
|
+
const randStr = randLower + randUpper;
|
|
49
|
+
const randbytes = Base32.toUint8Array(randStr);
|
|
50
|
+
const timeStr = timeLower + timeUpper;
|
|
51
|
+
const timearr = Base32.toUint8Array(timeStr);
|
|
52
|
+
if (randbytes.length !== 8 || timearr.length !== 8) {
|
|
53
|
+
throw new Error("Invalid timeId payload");
|
|
54
|
+
}
|
|
55
|
+
const timeview = new DataView(timearr.buffer, timearr.byteOffset, timearr.byteLength);
|
|
56
|
+
const timestamp = timeview.getBigUint64(0, false);
|
|
57
|
+
const date = new Date(Number(timestamp));
|
|
58
|
+
return { date, randbytes };
|
|
59
|
+
}
|
|
60
|
+
TimeId.parse = parse;
|
|
61
|
+
})(TimeId || (TimeId = {}));
|
|
62
|
+
//# sourceMappingURL=timeid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeid.js","sourceRoot":"","sources":["../src/timeid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,KAAW,MAAM,CAyEtB;AAzED,WAAiB,MAAM;IACrB;;;;;;;;OAQG;IACH,SAAgB,MAAM,CAAC,WAAwB,IAAI;QACjD,uBAAuB;QACvB,MAAM,SAAS,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5D,MAAM,GAAG,GAAG,MAAM,CAAC,cAA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AC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,GAAG,KAKpD,CAAC;QAEF,MAAM,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC;QACtC,MAAM,SAAS,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;QAE/C,MAAM,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC;QACtC,MAAM,OAAO,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;QAE7C,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnD,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC5C,CAAC;QAED,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAC3B,OAAO,CAAC,MAAM,EACd,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,UAAU,CACnB,CAAC;QACF,MAAM,SAAS,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;QAClD,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;QAEzC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;IAC7B,CAAC;IA/Be,YAAK,QA+BpB,CAAA;AACH,CAAC,EAzEgB,MAAM,KAAN,MAAM,QAyEtB"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,83 @@
|
|
|
1
|
+
import { Result } from "./result.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a union type to an intersection type.
|
|
4
|
+
* @template U The union type to be converted.
|
|
5
|
+
*/
|
|
1
6
|
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
7
|
+
/**
|
|
8
|
+
* Pads a given number with leading zeros to meet the specified length.
|
|
9
|
+
* @param {number} num The number to be padded.
|
|
10
|
+
* @param {number} [zeroLength=2] The desired total length of the resulting string. Defaults to 2.
|
|
11
|
+
* @returns {string} The padded string representation of the number.
|
|
12
|
+
*/
|
|
13
|
+
export declare function zeroPadding(num: number, length?: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* Safely parses a JSON string. Returns null if a parsing error occurs.
|
|
16
|
+
* @template T The expected type of the parsed data.
|
|
17
|
+
* @param {string | undefined | null} content The JSON string to parse.
|
|
18
|
+
* @returns {T | null} The parsed object, or null if parsing fails.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseJsonSafe<T = any>(content: string | undefined | null): T | null;
|
|
21
|
+
/**
|
|
22
|
+
* Executes a promise and returns a boolean indicating success.
|
|
23
|
+
* Logs an error and returns false if the promise is rejected.
|
|
24
|
+
* @param {Promise<any>} promise The promise to execute.
|
|
25
|
+
* @returns {Promise<boolean>} True if the promise resolves successfully, false otherwise.
|
|
26
|
+
*/
|
|
27
|
+
export declare function AsOk(promise: Promise<any>): Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Executes a promise and returns a wrapped result.
|
|
30
|
+
* @param {Promise<T>} promise The promise to execute.
|
|
31
|
+
* @returns {Promise<Result<T, Error>>} Result.ok(T) if the promise resolves successfully, Result.err(Error) otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export declare function AsResult<T>(promise: Promise<T>): Promise<Result<T, Error>>;
|
|
34
|
+
/**
|
|
35
|
+
* Executes a promise and returns its result, or null if an error occurs.
|
|
36
|
+
* Logs the error if the promise is rejected.
|
|
37
|
+
* @template T The expected return type of the promise.
|
|
38
|
+
* @param {Promise<T>} promise The promise to execute.
|
|
39
|
+
* @returns {Promise<T | null>} The result of the promise, or null if it fails.
|
|
40
|
+
*/
|
|
41
|
+
export declare function NullCatch<T>(promise: Promise<T>): Promise<T | null>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns an empty promise that resolves to undefined.
|
|
44
|
+
* @returns {Promise<undefined>}
|
|
45
|
+
*/
|
|
46
|
+
export declare function EmptyPromise(): Promise<undefined>;
|
|
47
|
+
/**
|
|
48
|
+
* Returns an promise that resolves to input value.
|
|
49
|
+
* @param {T} input The input value
|
|
50
|
+
* @returns {Promise<T>}
|
|
51
|
+
*/
|
|
52
|
+
export declare function ValuePromise<T>(input: T): Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Returns a promise that immediately rejects with the provided error.
|
|
55
|
+
* @param {Error} err The error to be thrown.
|
|
56
|
+
* @returns {Promise<any>}
|
|
57
|
+
*/
|
|
58
|
+
export declare function ErrorPromise(err: Error): Promise<any>;
|
|
59
|
+
/**
|
|
60
|
+
* Represents a valid CSS class name or a falsy value to be ignored during the merge process.
|
|
61
|
+
*/
|
|
62
|
+
export type ClassNameType = string | undefined | false | null;
|
|
63
|
+
/**
|
|
64
|
+
* Merges multiple class names and arrays of class names into a single space-separated string.
|
|
65
|
+
* Falsy values such as null, undefined, or false are automatically filtered out.
|
|
66
|
+
* This function handles single-level arrays but does not support deep nesting.
|
|
67
|
+
*
|
|
68
|
+
* @param {...(ClassNameType | ClassNameType[])} classNameList - A list of class names or arrays containing class names.
|
|
69
|
+
* @returns {string} A combined string of valid class names separated by a space.
|
|
70
|
+
*/
|
|
71
|
+
export declare function mergeClassName(...classNameList: (ClassNameType | ClassNameType[])[]): string;
|
|
72
|
+
export declare const TIB_UNIT: number;
|
|
73
|
+
export declare const GIB_UNIT: number;
|
|
74
|
+
export declare const MIB_UNIT: number;
|
|
75
|
+
export declare const KIB_UNIT: number;
|
|
76
|
+
/**
|
|
77
|
+
* Formats a file size in bytes into a human-readable string using binary prefixes (IEC standard).
|
|
78
|
+
*
|
|
79
|
+
* @param {number} sizeBytes - The size of the file in bytes.
|
|
80
|
+
* @returns {string} The formatted file size string with its corresponding unit (e.g., "1.5GiB", "500B").
|
|
81
|
+
*/
|
|
82
|
+
export declare function formatFileSize(sizeBytes: number): string;
|
|
2
83
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;GAGG;AACH,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;AAEV;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,MAAU,GAAG,MAAM,CAanE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,GAAG,GAAG,EACnC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GACjC,CAAC,GAAG,IAAI,CAOV;AAED;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAOlE;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAC9B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAClB,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAE3B;AAED;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAKzE;AAED;;;GAGG;AACH,wBAAsB,YAAY,IAAI,OAAO,CAAC,SAAS,CAAC,CAAG;AAE3D;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAE1D;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAE3D;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,SAAS,GAAG,KAAK,GAAG,IAAI,CAAC;AAE9D;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,GAAG,aAAa,EAAE,CAAC,aAAa,GAAG,aAAa,EAAE,CAAC,EAAE,GACpD,MAAM,CAYR;AAED,eAAO,MAAM,QAAQ,QAAY,CAAC;AAClC,eAAO,MAAM,QAAQ,QAAY,CAAC;AAClC,eAAO,MAAM,QAAQ,QAAY,CAAC;AAClC,eAAO,MAAM,QAAQ,QAAY,CAAC;AAClC;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAWxD"}
|
package/dist/utils.js
CHANGED
|
@@ -1,2 +1,142 @@
|
|
|
1
|
-
|
|
1
|
+
import { logErr } from "./libLog.js";
|
|
2
|
+
import { Result } from "./result.js";
|
|
3
|
+
/**
|
|
4
|
+
* Pads a given number with leading zeros to meet the specified length.
|
|
5
|
+
* @param {number} num The number to be padded.
|
|
6
|
+
* @param {number} [zeroLength=2] The desired total length of the resulting string. Defaults to 2.
|
|
7
|
+
* @returns {string} The padded string representation of the number.
|
|
8
|
+
*/
|
|
9
|
+
export function zeroPadding(num, length = 2) {
|
|
10
|
+
const numStr = Math.abs(num).toString();
|
|
11
|
+
const sign = Math.sign(num);
|
|
12
|
+
let signStr;
|
|
13
|
+
if (sign == -1) {
|
|
14
|
+
signStr = "-";
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
signStr = "";
|
|
18
|
+
}
|
|
19
|
+
const numLength = Math.max(length - signStr.length, 0);
|
|
20
|
+
return signStr + numStr.padStart(numLength, "0");
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Safely parses a JSON string. Returns null if a parsing error occurs.
|
|
24
|
+
* @template T The expected type of the parsed data.
|
|
25
|
+
* @param {string | undefined | null} content The JSON string to parse.
|
|
26
|
+
* @returns {T | null} The parsed object, or null if parsing fails.
|
|
27
|
+
*/
|
|
28
|
+
export function parseJsonSafe(content) {
|
|
29
|
+
if (!content)
|
|
30
|
+
return null;
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(content);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Executes a promise and returns a boolean indicating success.
|
|
40
|
+
* Logs an error and returns false if the promise is rejected.
|
|
41
|
+
* @param {Promise<any>} promise The promise to execute.
|
|
42
|
+
* @returns {Promise<boolean>} True if the promise resolves successfully, false otherwise.
|
|
43
|
+
*/
|
|
44
|
+
export async function AsOk(promise) {
|
|
45
|
+
return await promise
|
|
46
|
+
.then(() => true)
|
|
47
|
+
.catch((i) => {
|
|
48
|
+
logErr("Promise throw(catch in AsOk): " + i);
|
|
49
|
+
return false;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Executes a promise and returns a wrapped result.
|
|
54
|
+
* @param {Promise<T>} promise The promise to execute.
|
|
55
|
+
* @returns {Promise<Result<T, Error>>} Result.ok(T) if the promise resolves successfully, Result.err(Error) otherwise.
|
|
56
|
+
*/
|
|
57
|
+
export async function AsResult(promise) {
|
|
58
|
+
return await promise.then(Result.ok).catch(Result.err);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Executes a promise and returns its result, or null if an error occurs.
|
|
62
|
+
* Logs the error if the promise is rejected.
|
|
63
|
+
* @template T The expected return type of the promise.
|
|
64
|
+
* @param {Promise<T>} promise The promise to execute.
|
|
65
|
+
* @returns {Promise<T | null>} The result of the promise, or null if it fails.
|
|
66
|
+
*/
|
|
67
|
+
export async function NullCatch(promise) {
|
|
68
|
+
return await promise.catch((i) => {
|
|
69
|
+
logErr("Promise throw(catch in NullCatch): " + i);
|
|
70
|
+
return null;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns an empty promise that resolves to undefined.
|
|
75
|
+
* @returns {Promise<undefined>}
|
|
76
|
+
*/
|
|
77
|
+
export async function EmptyPromise() { }
|
|
78
|
+
/**
|
|
79
|
+
* Returns an promise that resolves to input value.
|
|
80
|
+
* @param {T} input The input value
|
|
81
|
+
* @returns {Promise<T>}
|
|
82
|
+
*/
|
|
83
|
+
export async function ValuePromise(input) {
|
|
84
|
+
return input;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns a promise that immediately rejects with the provided error.
|
|
88
|
+
* @param {Error} err The error to be thrown.
|
|
89
|
+
* @returns {Promise<any>}
|
|
90
|
+
*/
|
|
91
|
+
export async function ErrorPromise(err) {
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Merges multiple class names and arrays of class names into a single space-separated string.
|
|
96
|
+
* Falsy values such as null, undefined, or false are automatically filtered out.
|
|
97
|
+
* This function handles single-level arrays but does not support deep nesting.
|
|
98
|
+
*
|
|
99
|
+
* @param {...(ClassNameType | ClassNameType[])} classNameList - A list of class names or arrays containing class names.
|
|
100
|
+
* @returns {string} A combined string of valid class names separated by a space.
|
|
101
|
+
*/
|
|
102
|
+
export function mergeClassName(...classNameList) {
|
|
103
|
+
const buffer = [];
|
|
104
|
+
for (const className of classNameList) {
|
|
105
|
+
if (Array.isArray(className)) {
|
|
106
|
+
for (const item of className) {
|
|
107
|
+
if (item)
|
|
108
|
+
buffer.push(item);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (className && className.length) {
|
|
112
|
+
buffer.push(className);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return buffer.join(" ");
|
|
116
|
+
}
|
|
117
|
+
export const TIB_UNIT = 1024 ** 4;
|
|
118
|
+
export const GIB_UNIT = 1024 ** 3;
|
|
119
|
+
export const MIB_UNIT = 1024 ** 2;
|
|
120
|
+
export const KIB_UNIT = 1024 ** 1;
|
|
121
|
+
/**
|
|
122
|
+
* Formats a file size in bytes into a human-readable string using binary prefixes (IEC standard).
|
|
123
|
+
*
|
|
124
|
+
* @param {number} sizeBytes - The size of the file in bytes.
|
|
125
|
+
* @returns {string} The formatted file size string with its corresponding unit (e.g., "1.5GiB", "500B").
|
|
126
|
+
*/
|
|
127
|
+
export function formatFileSize(sizeBytes) {
|
|
128
|
+
if (sizeBytes >= TIB_UNIT) {
|
|
129
|
+
return `${(sizeBytes / TIB_UNIT).toFixed(1)}TiB`;
|
|
130
|
+
}
|
|
131
|
+
else if (sizeBytes >= GIB_UNIT) {
|
|
132
|
+
return `${(sizeBytes / GIB_UNIT).toFixed(1)}GiB`;
|
|
133
|
+
}
|
|
134
|
+
else if (sizeBytes >= MIB_UNIT) {
|
|
135
|
+
return `${(sizeBytes / MIB_UNIT).toFixed(1)}MiB`;
|
|
136
|
+
}
|
|
137
|
+
else if (sizeBytes >= KIB_UNIT) {
|
|
138
|
+
return `${(sizeBytes / KIB_UNIT).toFixed(1)}KiB`;
|
|
139
|
+
}
|
|
140
|
+
return `${sizeBytes}B`;
|
|
141
|
+
}
|
|
2
142
|
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAYrC;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW,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AI,IAAI,CAAC,CAAC;AAClC,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC,CAAC;AAClC;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,SAAiB;IAC9C,IAAI,SAAS,IAAI,QAAQ,EAAE,CAAC;QAC1B,OAAO,GAAG,CAAC,SAAS,GAAG,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;IACnD,CAAC;SAAM,IAAI,SAAS,IAAI,QAAQ,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,SAAS,GAAG,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;IACnD,CAAC;SAAM,IAAI,SAAS,IAAI,QAAQ,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,SAAS,GAAG,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;IACnD,CAAC;SAAM,IAAI,SAAS,IAAI,QAAQ,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,SAAS,GAAG,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;IACnD,CAAC;IACD,OAAO,GAAG,SAAS,GAAG,CAAC;AACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qwreey-js/ts-util",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Qwreey's typescript utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript"
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"check:format": "npm run --prefix ../../ prettier -- --check packages/ts-util/src",
|
|
27
27
|
"fix:format": "npm run --prefix ../../ prettier -- --write packages/ts-util/src",
|
|
28
28
|
"init": "npm ci --include-workspace-root --include=dev",
|
|
29
|
-
"prepack": "npm run init && npm run check && npm run build"
|
|
29
|
+
"prepack": "npm run init && npm run check && npm run build",
|
|
30
|
+
"test": "../../node_modules/.bin/tsx src/test/index.ts"
|
|
30
31
|
},
|
|
31
32
|
"exports": {
|
|
32
33
|
".": {
|