@qwreey-js/ts-util 1.0.3 → 1.0.4

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/utils.js CHANGED
@@ -1,2 +1,142 @@
1
- export {};
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,EAAE,SAAiB,CAAC;IACzD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;IACxC,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAE5B,IAAI,OAAe,CAAC;IACpB,IAAI,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC;QACf,OAAO,GAAG,GAAG,CAAC;IAChB,CAAC;SAAM,CAAC;QACN,OAAO,GAAG,EAAE,CAAC;IACf,CAAC;IACD,MAAM,SAAS,GAAG,IAAI,CAAC,G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AAC,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",
3
+ "version": "1.0.4",
4
4
  "description": "Qwreey's typescript utilities",
5
5
  "keywords": [
6
6
  "typescript"