@khgtrn/lib 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +113 -0
- package/dist/cjs/base-enum.d.ts +106 -0
- package/dist/cjs/base-enum.js +137 -0
- package/dist/cjs/func.d.ts +227 -0
- package/dist/cjs/func.js +574 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/number-to-words/helpers.d.ts +17 -0
- package/dist/cjs/number-to-words/helpers.js +72 -0
- package/dist/cjs/number-to-words/index.d.ts +30 -0
- package/dist/cjs/number-to-words/index.js +50 -0
- package/dist/cjs/number-to-words/locales.d.ts +7 -0
- package/dist/cjs/number-to-words/locales.js +106 -0
- package/dist/cjs/number-to-words/types.d.ts +35 -0
- package/dist/cjs/number-to-words/types.js +2 -0
- package/dist/cjs/package.json +4 -0
- package/dist/cjs/round.d.ts +13 -0
- package/dist/cjs/round.js +19 -0
- package/dist/esm/base-enum.d.ts +106 -0
- package/dist/esm/base-enum.js +133 -0
- package/dist/esm/func.d.ts +227 -0
- package/dist/esm/func.js +547 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/number-to-words/helpers.d.ts +17 -0
- package/dist/esm/number-to-words/helpers.js +67 -0
- package/dist/esm/number-to-words/index.d.ts +30 -0
- package/dist/esm/number-to-words/index.js +47 -0
- package/dist/esm/number-to-words/locales.d.ts +7 -0
- package/dist/esm/number-to-words/locales.js +103 -0
- package/dist/esm/number-to-words/types.d.ts +35 -0
- package/dist/esm/number-to-words/types.js +1 -0
- package/dist/esm/package.json +4 -0
- package/dist/esm/round.d.ts +13 -0
- package/dist/esm/round.js +16 -0
- package/package.json +50 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether a value is considered "empty".
|
|
3
|
+
*
|
|
4
|
+
* - string: empty after trimming.
|
|
5
|
+
* - number: equal to `0`.
|
|
6
|
+
* - boolean: `false`.
|
|
7
|
+
* - `null`/`undefined`: always empty.
|
|
8
|
+
* - array: length `0`.
|
|
9
|
+
* - other objects: no own enumerable keys.
|
|
10
|
+
* - anything else (function, symbol, ...): never empty.
|
|
11
|
+
*
|
|
12
|
+
* @param value - Value to check.
|
|
13
|
+
* @returns `true` if the value is considered empty.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isEmpty(value: any): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether a value is a valid number (not `NaN`, not `Infinity`).
|
|
18
|
+
* @param value - Value to check.
|
|
19
|
+
* @returns Type-guard: `true` if `value` is a finite number.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isNumber(value: any): value is number;
|
|
22
|
+
/**
|
|
23
|
+
* Converts Vietnamese diacritics to their plain ASCII equivalents
|
|
24
|
+
* (e.g. `"Điều chỉnh"` -> `"Dieu chinh"`).
|
|
25
|
+
*
|
|
26
|
+
* @param s - Input string.
|
|
27
|
+
* @returns The string with Vietnamese diacritics removed.
|
|
28
|
+
*/
|
|
29
|
+
export declare function vi2en(s: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Normalizes Windows-style line endings (`\r\n`) to Unix-style (`\n`).
|
|
32
|
+
* @param value - Input string.
|
|
33
|
+
* @returns The string with all `\r\n` replaced by `\n`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function crlf2lf(value: string): string;
|
|
36
|
+
/**
|
|
37
|
+
* Removes every newline from a string, after normalizing line endings and
|
|
38
|
+
* trimming surrounding whitespace.
|
|
39
|
+
* @param value - Input string.
|
|
40
|
+
* @returns The string with all newlines removed.
|
|
41
|
+
*/
|
|
42
|
+
export declare function removeNewline(value: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Shuffles an array in place using the Fisher-Yates algorithm.
|
|
45
|
+
* @param array - Array to shuffle (mutated directly).
|
|
46
|
+
* @returns The same array reference, shuffled.
|
|
47
|
+
*/
|
|
48
|
+
export declare function shuffleArray(array: any[]): any[];
|
|
49
|
+
/**
|
|
50
|
+
* Collects an object's own enumerable values into an array.
|
|
51
|
+
* @param obj - Source object. `null`/`undefined` yields an empty array.
|
|
52
|
+
* @returns Array of the object's values.
|
|
53
|
+
*/
|
|
54
|
+
export declare function objectValueToArray(obj: any): any[];
|
|
55
|
+
/**
|
|
56
|
+
* Groups list items by the key returned from `fn`, similar to Lodash's
|
|
57
|
+
* `groupBy`. Keys are compared by their JSON representation, so they can be
|
|
58
|
+
* primitives or plain objects.
|
|
59
|
+
*
|
|
60
|
+
* @param list - Array-like list of items to group. `null`/`undefined` yields an empty array.
|
|
61
|
+
* @param fn - Maps an item to the value used to group it.
|
|
62
|
+
* @returns Array of groups, each an array of items sharing the same key.
|
|
63
|
+
*/
|
|
64
|
+
export declare function groupBy(list: any, fn: (item: any) => any): any[];
|
|
65
|
+
/**
|
|
66
|
+
* Remove keys from object or array
|
|
67
|
+
* @param objectOrArray Object or array
|
|
68
|
+
* @param keys Keys to remove
|
|
69
|
+
* @returns Object or array with keys removed
|
|
70
|
+
*/
|
|
71
|
+
export declare function removeByKey<T = any>(objectOrArray: T, keys: string[]): T;
|
|
72
|
+
/**
|
|
73
|
+
* Remove empty values from object or array
|
|
74
|
+
* @param objectOrArray Object or array
|
|
75
|
+
* @param options Options:
|
|
76
|
+
* - removeNull: Remove null values. Default: true
|
|
77
|
+
* - removeUndefined: Remove undefined values. Default: true
|
|
78
|
+
* - removeEmptyString: Remove empty string values. Default: true
|
|
79
|
+
* @returns Object or array with empty values removed
|
|
80
|
+
*/
|
|
81
|
+
export declare function removeEmptyValue<T = any>(objectOrArray: T, options?: {
|
|
82
|
+
removeNull?: boolean;
|
|
83
|
+
removeUndefined?: boolean;
|
|
84
|
+
removeEmptyString?: boolean;
|
|
85
|
+
}): T;
|
|
86
|
+
/**
|
|
87
|
+
* Generates a random string of the given length, optionally guaranteeing at
|
|
88
|
+
* least one character from each requested category ("upper", "lower",
|
|
89
|
+
* "number", "specific").
|
|
90
|
+
*
|
|
91
|
+
* @param length - Desired length of the resulting string.
|
|
92
|
+
* @param opt - Character categories that must each appear at least once.
|
|
93
|
+
* `"specific"` only counts as a required category when `specificChars` is
|
|
94
|
+
* non-empty; otherwise it's silently ignored (as if not requested at all).
|
|
95
|
+
* @param specificChars - Custom character set used for the `"specific"` category.
|
|
96
|
+
* @returns A random string of exactly `length` characters. If `opt` is empty
|
|
97
|
+
* (or every requested category ends up unusable), falls back to alphanumeric
|
|
98
|
+
* characters rather than returning an empty string.
|
|
99
|
+
* @throws {Error} If `length` is smaller than the number of required categories,
|
|
100
|
+
* since the "at least one of each" guarantee couldn't fit otherwise.
|
|
101
|
+
*/
|
|
102
|
+
export declare function randomString(length: number, opt?: ("upper" | "lower" | "number" | "specific")[], specificChars?: string): string;
|
|
103
|
+
/**
|
|
104
|
+
* Formats a byte value (0-255) as a zero-padded, 2-digit lowercase hex string.
|
|
105
|
+
* Intended for internal use with well-formed byte values (e.g. from a
|
|
106
|
+
* `Uint8Array`); values outside 0-255 are not validated.
|
|
107
|
+
*
|
|
108
|
+
* @param b - Byte value, expected in the 0-255 range.
|
|
109
|
+
* @returns 2-character hex string, e.g. `"0f"`.
|
|
110
|
+
*/
|
|
111
|
+
export declare function byte2hex(b: number): string;
|
|
112
|
+
/**
|
|
113
|
+
* Generates the raw 16 bytes of a UUIDv7 (timestamp + random, RFC 4122
|
|
114
|
+
* variant). Relies on the Web Crypto `crypto.getRandomValues` API, available
|
|
115
|
+
* in browsers and modern Node.js.
|
|
116
|
+
*
|
|
117
|
+
* @returns 16-byte `Uint8Array` encoding a UUIDv7.
|
|
118
|
+
*/
|
|
119
|
+
export declare function uuid7bin(): Uint8Array;
|
|
120
|
+
/**
|
|
121
|
+
* Generates a UUIDv7 string (e.g. `"01890a5d-ac96-774b-bcce-b302099a8057"`).
|
|
122
|
+
* @returns UUIDv7 in the standard 8-4-4-4-12 hex string format.
|
|
123
|
+
*/
|
|
124
|
+
export declare function uuid7(): string;
|
|
125
|
+
/**
|
|
126
|
+
* Encodes a string to base64 (UTF-8 bytes).
|
|
127
|
+
* @param str - String to encode.
|
|
128
|
+
* @returns Base64-encoded string.
|
|
129
|
+
*/
|
|
130
|
+
export declare function base64encode(str: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Decodes a base64 string back to its original (UTF-8) string.
|
|
133
|
+
* @param base64 - Base64-encoded string.
|
|
134
|
+
* @returns Decoded string.
|
|
135
|
+
*/
|
|
136
|
+
export declare function base64decode(base64: string): string;
|
|
137
|
+
/**
|
|
138
|
+
* Reads a nested property from an object using a dot-separated path.
|
|
139
|
+
* Example: `getObjectValue(user, 'abc.def')` returns `user.abc.def`.
|
|
140
|
+
*
|
|
141
|
+
* Equivalent to optional chaining (`object?.abc?.def`), for use on
|
|
142
|
+
* TypeScript versions below 3.7 (e.g. Angular versions below 9). On newer
|
|
143
|
+
* TypeScript versions, prefer optional chaining directly instead.
|
|
144
|
+
*
|
|
145
|
+
* @param object - Source object.
|
|
146
|
+
* @param path - Dot-separated property path.
|
|
147
|
+
* @returns The resolved value, or `null`/`undefined` if any segment is missing.
|
|
148
|
+
* @global
|
|
149
|
+
*/
|
|
150
|
+
export declare function getObjectValue(object: null | undefined | Record<string, any>, path: string): any;
|
|
151
|
+
/**
|
|
152
|
+
* Short alias for {@link getObjectValue}.
|
|
153
|
+
* @param object - Source object.
|
|
154
|
+
* @param path - Dot-separated property path.
|
|
155
|
+
* @returns The resolved value, or `null`/`undefined` if any segment is missing.
|
|
156
|
+
*/
|
|
157
|
+
export declare function ov(object: null | undefined | Record<string, any>, path: string): any;
|
|
158
|
+
/**
|
|
159
|
+
* Converts a value to an integer, similar to `parseInt`/`Number` but with an
|
|
160
|
+
* explicit fallback for empty values (see {@link isEmpty}).
|
|
161
|
+
*
|
|
162
|
+
* @param value - Value to convert.
|
|
163
|
+
* @param defaultValue - Value returned when `value` is empty. Defaults to `0`.
|
|
164
|
+
* @returns The parsed integer, `defaultValue` if `value` is empty, or `NaN`
|
|
165
|
+
* if `value` is a non-numeric, non-empty string.
|
|
166
|
+
*/
|
|
167
|
+
export declare function toInt(value: any, defaultValue?: number | null | undefined): number | null | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* Converts a positive integer to a Roman numeral. Only supports the standard
|
|
170
|
+
* range (1-3999); larger numbers produce a non-standard repeating "M" prefix.
|
|
171
|
+
*
|
|
172
|
+
* @param num - Number to convert.
|
|
173
|
+
* @returns Roman numeral string, or `""` if `num` is zero or negative.
|
|
174
|
+
*/
|
|
175
|
+
export declare function numberToRoman(num: number): string;
|
|
176
|
+
/**
|
|
177
|
+
* Converts a Roman numeral string to a number. Does not validate that the
|
|
178
|
+
* input is a well-formed Roman numeral (use {@link isRomanNumber} first if
|
|
179
|
+
* that matters) — unrecognized characters make the result `NaN`.
|
|
180
|
+
*
|
|
181
|
+
* @param roman - Roman numeral string (case-sensitive, uppercase letters).
|
|
182
|
+
* @returns The numeric value, or `NaN` if `roman` contains unrecognized characters.
|
|
183
|
+
*/
|
|
184
|
+
export declare function romanToNumber(roman: string): number;
|
|
185
|
+
/**
|
|
186
|
+
* Checks whether a string is a well-formed Roman numeral (1-3999), matching
|
|
187
|
+
* the standard subtractive notation. Case-insensitive.
|
|
188
|
+
*
|
|
189
|
+
* @param value - String to validate.
|
|
190
|
+
* @returns `true` if `value` is a valid Roman numeral.
|
|
191
|
+
*/
|
|
192
|
+
export declare function isRomanNumber(value: string): boolean;
|
|
193
|
+
/**
|
|
194
|
+
* Converts a base64 string to a `Blob`. Browser-only (relies on `atob` and
|
|
195
|
+
* `Blob`).
|
|
196
|
+
*
|
|
197
|
+
* @param base64 - Base64-encoded content.
|
|
198
|
+
* @param mimeType - MIME type to assign to the resulting `Blob`.
|
|
199
|
+
* @param sliceSize - Chunk size (in decoded characters) used while building
|
|
200
|
+
* the byte arrays, to avoid excessive memory allocation for large inputs.
|
|
201
|
+
* @returns A `Blob` containing the decoded bytes.
|
|
202
|
+
*/
|
|
203
|
+
export declare function base64ToBlob(base64: string, mimeType: string, sliceSize?: number): Blob;
|
|
204
|
+
/**
|
|
205
|
+
* Triggers a browser download (or opens in a tab) for base64-encoded file
|
|
206
|
+
* content. Browser-only (relies on `document`, `URL.createObjectURL`).
|
|
207
|
+
*
|
|
208
|
+
* @param fileName - Suggested file name for the download.
|
|
209
|
+
* @param mimeType - MIME type/subtype (a bare subtype like `"pdf"` is
|
|
210
|
+
* expanded to `"application/pdf"`).
|
|
211
|
+
* @param base64Content - Base64-encoded file content.
|
|
212
|
+
* @param action - `"download"` saves the file, `"open"` opens it in the same
|
|
213
|
+
* tab, `"open_blank"` opens it in a new tab. Defaults to `"download"`.
|
|
214
|
+
*/
|
|
215
|
+
export declare function downloadFile(fileName: string, mimeType: string, base64Content: string, action?: "download" | "open" | "open_blank"): void;
|
|
216
|
+
/**
|
|
217
|
+
* Recursively clones plain objects, arrays and `Date` instances.
|
|
218
|
+
*
|
|
219
|
+
* Note: `Date` has no own enumerable properties, so a plain for-in copy
|
|
220
|
+
* would silently turn every `Date` into an empty `{}` — it's special-cased
|
|
221
|
+
* here. Other special object types (`Map`, `Set`, `RegExp`, ...) are not
|
|
222
|
+
* handled and will also be cloned as plain `{}`.
|
|
223
|
+
*
|
|
224
|
+
* @param obj - Value to clone.
|
|
225
|
+
* @returns A deep copy of `obj` (primitives are returned as-is).
|
|
226
|
+
*/
|
|
227
|
+
export declare function deepClone(obj: any): any;
|