@khgtrn/lib 1.0.1 → 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.
package/README.md CHANGED
@@ -43,7 +43,17 @@ Role.Admin.name(); // 'Admin'
43
43
  new Role(2, 'x'); // compile error — constructor is protected
44
44
  ```
45
45
 
46
- The constructor also accepts an optional `opts?: Record<string, any>` parameter for storing arbitrary extra data per constant.
46
+ The constructor also accepts an optional third parameter, `opt?: O`, for storing arbitrary extra data per constant. Pass the second type argument on `BaseEnum<T, O>` to get accurate type hints on `opt`:
47
+
48
+ ```ts
49
+ class Role extends BaseEnum<number, { icon: string }> {
50
+ static readonly Admin = new Role(1, 'Administrator', { icon: 'shield' });
51
+ }
52
+
53
+ Role.Admin.opt?.icon; // typed as `string | undefined`
54
+ ```
55
+
56
+ Without an explicit `O`, it defaults to `any`.
47
57
 
48
58
  ## Utility functions (`func.ts`)
49
59
 
@@ -51,9 +61,12 @@ The constructor also accepts an optional `opts?: Record<string, any>` parameter
51
61
  | --- | --- |
52
62
  | `isEmpty(value)` | Checks whether a value is empty (string, `0`, `false`, null/undefined, empty array/object) |
53
63
  | `isNumber(value)` | Type-guard: a valid finite number (not NaN/Infinity) |
64
+ | `nullish(value, defaultValue)` | Returns `value` unless it's `null`/`undefined`, in which case returns `defaultValue` — a `??`-equivalent for pre-3.7 TypeScript |
65
+ | `jsonParse(s, defaultValue?)` | `JSON.parse` with a fallback value instead of throwing on invalid input |
54
66
  | `vi2en(s)` | Strips Vietnamese diacritics (`"Điều chỉnh"` -> `"Dieu chinh"`) |
55
67
  | `crlf2lf(value)` | Normalizes `\r\n` -> `\n` |
56
68
  | `removeNewline(value)` | Removes all newlines, trims surrounding whitespace |
69
+ | `padStart(str, targetLength, padChar?)` / `padEnd(str, targetLength, padChar?)` | Pads a string to a target length by adding characters to the left/right (defaults to `"0"`) |
57
70
  | `shuffleArray(array)` | Shuffles an array in place (Fisher-Yates) |
58
71
  | `objectValueToArray(obj)` | Collects an object's values into an array |
59
72
  | `groupBy(list, fn)` | Groups items by a computed key |
@@ -2,7 +2,7 @@
2
2
  * Base class for simulating Java-style enums in TypeScript.
3
3
  *
4
4
  * Subclasses only need to `extends BaseEnum<...>` and declare constants as
5
- * `static readonly Xxx = new SubClass(value, label, opts?)`, without
5
+ * `static readonly Xxx = new SubClass(value, label, opt?)`, without
6
6
  * redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
7
7
  * subclasses inherit it while keeping the same protection — instances can't
8
8
  * be `new`-ed from outside the class, which preserves enum singleton/identity
@@ -10,10 +10,10 @@
10
10
  *
11
11
  * @typeParam T - Type of the `value` field (defaults to `number`).
12
12
  */
13
- export declare abstract class BaseEnum<T = number> {
13
+ export declare abstract class BaseEnum<T = number, O = any> {
14
14
  readonly value: T;
15
15
  readonly label: string;
16
- readonly opts?: Record<string, any> | undefined;
16
+ readonly opt?: O | undefined;
17
17
  /**
18
18
  * Registry of every instance created, keyed per subclass and per `value`.
19
19
  * The outer key is the subclass constructor (so each subclass has its own
@@ -28,9 +28,9 @@ export declare abstract class BaseEnum<T = number> {
28
28
  *
29
29
  * @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
30
30
  * @param label - Display label/description of the constant.
31
- * @param opts - Optional extra data, freely defined by the subclass as needed.
31
+ * @param opt - Optional extra data, freely defined by the subclass as needed.
32
32
  */
33
- protected constructor(value: T, label: string, opts?: Record<string, any> | undefined);
33
+ protected constructor(value: T, label: string, opt?: O | undefined);
34
34
  /**
35
35
  * Returns the names of the `static readonly` fields declared on the
36
36
  * subclass, in declaration order. Mirrors the idea of an enum constant's
@@ -5,7 +5,7 @@ exports.BaseEnum = void 0;
5
5
  * Base class for simulating Java-style enums in TypeScript.
6
6
  *
7
7
  * Subclasses only need to `extends BaseEnum<...>` and declare constants as
8
- * `static readonly Xxx = new SubClass(value, label, opts?)`, without
8
+ * `static readonly Xxx = new SubClass(value, label, opt?)`, without
9
9
  * redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
10
10
  * subclasses inherit it while keeping the same protection — instances can't
11
11
  * be `new`-ed from outside the class, which preserves enum singleton/identity
@@ -21,12 +21,12 @@ class BaseEnum {
21
21
  *
22
22
  * @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
23
23
  * @param label - Display label/description of the constant.
24
- * @param opts - Optional extra data, freely defined by the subclass as needed.
24
+ * @param opt - Optional extra data, freely defined by the subclass as needed.
25
25
  */
26
- constructor(value, label, opts) {
26
+ constructor(value, label, opt) {
27
27
  this.value = value;
28
28
  this.label = label;
29
- this.opts = opts;
29
+ this.opt = opt;
30
30
  let map = BaseEnum.registry.get(this.constructor);
31
31
  if (!map) {
32
32
  map = new Map();
@@ -19,6 +19,21 @@ export declare function isEmpty(value: any): boolean;
19
19
  * @returns Type-guard: `true` if `value` is a finite number.
20
20
  */
21
21
  export declare function isNumber(value: any): value is number;
22
+ /**
23
+ * Nếu giá trị của `value` không phải là null hoặc undefined thì lấy, ngược lại trả về `defaultValue`.
24
+ * @note Sử dụng thay cho cú pháp `??`. Nếu TS3.7 trở lên thì không cần dùng.
25
+ * @param value Giá trị cần kiểm tra
26
+ * @param defaultValue Giá trị mặc định trả về nếu `value` là null hoặc undefined
27
+ * @global
28
+ */
29
+ export declare function nullish(value: any, defaultValue: any): any;
30
+ /**
31
+ * JSON.parse với giá trị mặc định nếu có lỗi
32
+ * @param s giá trị JSON cần parse
33
+ * @param defaultValue giá trị mặc định trả về nếu có lỗi khi parse. Mặc định là null
34
+ * @returns any
35
+ */
36
+ export declare function jsonParse<T = any>(s: string, defaultValue?: T | any): T | null;
22
37
  /**
23
38
  * Converts Vietnamese diacritics to their plain ASCII equivalents
24
39
  * (e.g. `"Điều chỉnh"` -> `"Dieu chinh"`).
@@ -40,6 +55,24 @@ export declare function crlf2lf(value: string): string;
40
55
  * @returns The string with all newlines removed.
41
56
  */
42
57
  export declare function removeNewline(value: string): string;
58
+ /**
59
+ * Thêm ký tự vào đầu chuỗi cho đến khi đạt được độ dài mục tiêu
60
+ * @param str Chuỗi gốc
61
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
62
+ * @param padChar Ký tự dùng để thêm vào đầu chuỗi (mặc định là "0")
63
+ * @returns Chuỗi đã được thêm ký tự vào đầu nếu cần thiết
64
+ * @global
65
+ */
66
+ export declare function padStart(str: string, targetLength: number, padChar?: string): string;
67
+ /**
68
+ * Thêm ký tự vào cuối chuỗi cho đến khi đạt được độ dài mục tiêu
69
+ * @param str Chuỗi gốc
70
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
71
+ * @param padChar Ký tự dùng để thêm vào cuối chuỗi (mặc định là "0")
72
+ * @returns Chuỗi đã được thêm ký tự vào cuối nếu cần thiết
73
+ * @global
74
+ */
75
+ export declare function padEnd(str: string, targetLength: number, padChar?: string): string;
43
76
  /**
44
77
  * Shuffles an array in place using the Fisher-Yates algorithm.
45
78
  * @param array - Array to shuffle (mutated directly).
package/dist/cjs/func.js CHANGED
@@ -2,9 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isEmpty = isEmpty;
4
4
  exports.isNumber = isNumber;
5
+ exports.nullish = nullish;
6
+ exports.jsonParse = jsonParse;
5
7
  exports.vi2en = vi2en;
6
8
  exports.crlf2lf = crlf2lf;
7
9
  exports.removeNewline = removeNewline;
10
+ exports.padStart = padStart;
11
+ exports.padEnd = padEnd;
8
12
  exports.shuffleArray = shuffleArray;
9
13
  exports.objectValueToArray = objectValueToArray;
10
14
  exports.groupBy = groupBy;
@@ -68,6 +72,30 @@ function isEmpty(value) {
68
72
  function isNumber(value) {
69
73
  return typeof value === "number" && Number.isFinite(value);
70
74
  }
75
+ /**
76
+ * Nếu giá trị của `value` không phải là null hoặc undefined thì lấy, ngược lại trả về `defaultValue`.
77
+ * @note Sử dụng thay cho cú pháp `??`. Nếu TS3.7 trở lên thì không cần dùng.
78
+ * @param value Giá trị cần kiểm tra
79
+ * @param defaultValue Giá trị mặc định trả về nếu `value` là null hoặc undefined
80
+ * @global
81
+ */
82
+ function nullish(value, defaultValue) {
83
+ return value !== null && value !== undefined ? value : defaultValue;
84
+ }
85
+ /**
86
+ * JSON.parse với giá trị mặc định nếu có lỗi
87
+ * @param s giá trị JSON cần parse
88
+ * @param defaultValue giá trị mặc định trả về nếu có lỗi khi parse. Mặc định là null
89
+ * @returns any
90
+ */
91
+ function jsonParse(s, defaultValue = null) {
92
+ try {
93
+ return JSON.parse(s);
94
+ }
95
+ catch (e) {
96
+ return defaultValue;
97
+ }
98
+ }
71
99
  /**
72
100
  * Converts Vietnamese diacritics to their plain ASCII equivalents
73
101
  * (e.g. `"Điều chỉnh"` -> `"Dieu chinh"`).
@@ -102,6 +130,40 @@ function crlf2lf(value) {
102
130
  function removeNewline(value) {
103
131
  return crlf2lf(value).trim().replace(/\n/g, "");
104
132
  }
133
+ /**
134
+ * Thêm ký tự vào đầu chuỗi cho đến khi đạt được độ dài mục tiêu
135
+ * @param str Chuỗi gốc
136
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
137
+ * @param padChar Ký tự dùng để thêm vào đầu chuỗi (mặc định là "0")
138
+ * @returns Chuỗi đã được thêm ký tự vào đầu nếu cần thiết
139
+ * @global
140
+ */
141
+ function padStart(str, targetLength, padChar = "0") {
142
+ str = String(str);
143
+ if (str.length >= targetLength || !padChar)
144
+ return str;
145
+ const padding = padChar
146
+ .repeat(Math.ceil((targetLength - str.length) / padChar.length))
147
+ .substring(0, targetLength - str.length);
148
+ return padding + str;
149
+ }
150
+ /**
151
+ * Thêm ký tự vào cuối chuỗi cho đến khi đạt được độ dài mục tiêu
152
+ * @param str Chuỗi gốc
153
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
154
+ * @param padChar Ký tự dùng để thêm vào cuối chuỗi (mặc định là "0")
155
+ * @returns Chuỗi đã được thêm ký tự vào cuối nếu cần thiết
156
+ * @global
157
+ */
158
+ function padEnd(str, targetLength, padChar = "0") {
159
+ str = String(str);
160
+ if (str.length >= targetLength || !padChar)
161
+ return str;
162
+ const padding = padChar
163
+ .repeat(Math.ceil((targetLength - str.length) / padChar.length))
164
+ .substring(0, targetLength - str.length);
165
+ return str + padding;
166
+ }
105
167
  /**
106
168
  * Shuffles an array in place using the Fisher-Yates algorithm.
107
169
  * @param array - Array to shuffle (mutated directly).
@@ -121,10 +183,9 @@ function shuffleArray(array) {
121
183
  * @returns Array of the object's values.
122
184
  */
123
185
  function objectValueToArray(obj) {
124
- var arr = [];
125
- for (var i in obj)
126
- arr.push(obj[i]);
127
- return arr;
186
+ if (obj === null || obj === undefined)
187
+ return [];
188
+ return Object.keys(obj).map((key) => obj[key]);
128
189
  }
129
190
  /**
130
191
  * Groups list items by the key returned from `fn`, similar to Lodash's
@@ -161,15 +222,18 @@ function removeByKey(objectOrArray, keys) {
161
222
  return objectOrArray.map((item) => typeof item === "object" && item !== null ? removeByKey(item, keys) : item);
162
223
  }
163
224
  if (typeof objectOrArray === "object" && objectOrArray !== null) {
164
- const entries = Object.entries(objectOrArray)
165
- .filter(([key]) => !keys.includes(key))
166
- .map(([key, value]) => {
167
- if (Array.isArray(value) || (typeof value === "object" && value !== null)) {
168
- return [key, removeByKey(value, keys)];
169
- }
170
- return [key, value];
225
+ const source = objectOrArray;
226
+ const result = {};
227
+ Object.keys(source).forEach((key) => {
228
+ if (keys.includes(key))
229
+ return;
230
+ const value = source[key];
231
+ result[key] =
232
+ Array.isArray(value) || (typeof value === "object" && value !== null)
233
+ ? removeByKey(value, keys)
234
+ : value;
171
235
  });
172
- return Object.fromEntries(entries);
236
+ return result;
173
237
  }
174
238
  // Primitives are returned as-is
175
239
  return objectOrArray;
@@ -204,22 +268,18 @@ function removeEmptyValue(objectOrArray, options = {}) {
204
268
  return result;
205
269
  }
206
270
  if (typeof objectOrArray === "object" && objectOrArray !== null) {
207
- const entries = Object.entries(objectOrArray)
208
- .map(([key, value]) => {
209
- if (Array.isArray(value) || (typeof value === "object" && value !== null)) {
210
- return [
211
- key,
212
- removeEmptyValue(value, {
213
- removeNull,
214
- removeUndefined,
215
- removeEmptyString,
216
- }),
217
- ];
271
+ const source = objectOrArray;
272
+ const result = {};
273
+ Object.keys(source).forEach((key) => {
274
+ const rawValue = source[key];
275
+ const value = Array.isArray(rawValue) || (typeof rawValue === "object" && rawValue !== null)
276
+ ? removeEmptyValue(rawValue, { removeNull, removeUndefined, removeEmptyString })
277
+ : rawValue;
278
+ if (!shouldRemove(value)) {
279
+ result[key] = value;
218
280
  }
219
- return [key, value];
220
- })
221
- .filter(([, value]) => !shouldRemove(value));
222
- return Object.fromEntries(entries);
281
+ });
282
+ return result;
223
283
  }
224
284
  return objectOrArray;
225
285
  }
@@ -2,7 +2,7 @@
2
2
  * Base class for simulating Java-style enums in TypeScript.
3
3
  *
4
4
  * Subclasses only need to `extends BaseEnum<...>` and declare constants as
5
- * `static readonly Xxx = new SubClass(value, label, opts?)`, without
5
+ * `static readonly Xxx = new SubClass(value, label, opt?)`, without
6
6
  * redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
7
7
  * subclasses inherit it while keeping the same protection — instances can't
8
8
  * be `new`-ed from outside the class, which preserves enum singleton/identity
@@ -10,10 +10,10 @@
10
10
  *
11
11
  * @typeParam T - Type of the `value` field (defaults to `number`).
12
12
  */
13
- export declare abstract class BaseEnum<T = number> {
13
+ export declare abstract class BaseEnum<T = number, O = any> {
14
14
  readonly value: T;
15
15
  readonly label: string;
16
- readonly opts?: Record<string, any> | undefined;
16
+ readonly opt?: O | undefined;
17
17
  /**
18
18
  * Registry of every instance created, keyed per subclass and per `value`.
19
19
  * The outer key is the subclass constructor (so each subclass has its own
@@ -28,9 +28,9 @@ export declare abstract class BaseEnum<T = number> {
28
28
  *
29
29
  * @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
30
30
  * @param label - Display label/description of the constant.
31
- * @param opts - Optional extra data, freely defined by the subclass as needed.
31
+ * @param opt - Optional extra data, freely defined by the subclass as needed.
32
32
  */
33
- protected constructor(value: T, label: string, opts?: Record<string, any> | undefined);
33
+ protected constructor(value: T, label: string, opt?: O | undefined);
34
34
  /**
35
35
  * Returns the names of the `static readonly` fields declared on the
36
36
  * subclass, in declaration order. Mirrors the idea of an enum constant's
@@ -2,7 +2,7 @@
2
2
  * Base class for simulating Java-style enums in TypeScript.
3
3
  *
4
4
  * Subclasses only need to `extends BaseEnum<...>` and declare constants as
5
- * `static readonly Xxx = new SubClass(value, label, opts?)`, without
5
+ * `static readonly Xxx = new SubClass(value, label, opt?)`, without
6
6
  * redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
7
7
  * subclasses inherit it while keeping the same protection — instances can't
8
8
  * be `new`-ed from outside the class, which preserves enum singleton/identity
@@ -18,12 +18,12 @@ export class BaseEnum {
18
18
  *
19
19
  * @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
20
20
  * @param label - Display label/description of the constant.
21
- * @param opts - Optional extra data, freely defined by the subclass as needed.
21
+ * @param opt - Optional extra data, freely defined by the subclass as needed.
22
22
  */
23
- constructor(value, label, opts) {
23
+ constructor(value, label, opt) {
24
24
  this.value = value;
25
25
  this.label = label;
26
- this.opts = opts;
26
+ this.opt = opt;
27
27
  let map = BaseEnum.registry.get(this.constructor);
28
28
  if (!map) {
29
29
  map = new Map();
@@ -19,6 +19,21 @@ export declare function isEmpty(value: any): boolean;
19
19
  * @returns Type-guard: `true` if `value` is a finite number.
20
20
  */
21
21
  export declare function isNumber(value: any): value is number;
22
+ /**
23
+ * Nếu giá trị của `value` không phải là null hoặc undefined thì lấy, ngược lại trả về `defaultValue`.
24
+ * @note Sử dụng thay cho cú pháp `??`. Nếu TS3.7 trở lên thì không cần dùng.
25
+ * @param value Giá trị cần kiểm tra
26
+ * @param defaultValue Giá trị mặc định trả về nếu `value` là null hoặc undefined
27
+ * @global
28
+ */
29
+ export declare function nullish(value: any, defaultValue: any): any;
30
+ /**
31
+ * JSON.parse với giá trị mặc định nếu có lỗi
32
+ * @param s giá trị JSON cần parse
33
+ * @param defaultValue giá trị mặc định trả về nếu có lỗi khi parse. Mặc định là null
34
+ * @returns any
35
+ */
36
+ export declare function jsonParse<T = any>(s: string, defaultValue?: T | any): T | null;
22
37
  /**
23
38
  * Converts Vietnamese diacritics to their plain ASCII equivalents
24
39
  * (e.g. `"Điều chỉnh"` -> `"Dieu chinh"`).
@@ -40,6 +55,24 @@ export declare function crlf2lf(value: string): string;
40
55
  * @returns The string with all newlines removed.
41
56
  */
42
57
  export declare function removeNewline(value: string): string;
58
+ /**
59
+ * Thêm ký tự vào đầu chuỗi cho đến khi đạt được độ dài mục tiêu
60
+ * @param str Chuỗi gốc
61
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
62
+ * @param padChar Ký tự dùng để thêm vào đầu chuỗi (mặc định là "0")
63
+ * @returns Chuỗi đã được thêm ký tự vào đầu nếu cần thiết
64
+ * @global
65
+ */
66
+ export declare function padStart(str: string, targetLength: number, padChar?: string): string;
67
+ /**
68
+ * Thêm ký tự vào cuối chuỗi cho đến khi đạt được độ dài mục tiêu
69
+ * @param str Chuỗi gốc
70
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
71
+ * @param padChar Ký tự dùng để thêm vào cuối chuỗi (mặc định là "0")
72
+ * @returns Chuỗi đã được thêm ký tự vào cuối nếu cần thiết
73
+ * @global
74
+ */
75
+ export declare function padEnd(str: string, targetLength: number, padChar?: string): string;
43
76
  /**
44
77
  * Shuffles an array in place using the Fisher-Yates algorithm.
45
78
  * @param array - Array to shuffle (mutated directly).
package/dist/esm/func.js CHANGED
@@ -41,6 +41,30 @@ export function isEmpty(value) {
41
41
  export function isNumber(value) {
42
42
  return typeof value === "number" && Number.isFinite(value);
43
43
  }
44
+ /**
45
+ * Nếu giá trị của `value` không phải là null hoặc undefined thì lấy, ngược lại trả về `defaultValue`.
46
+ * @note Sử dụng thay cho cú pháp `??`. Nếu TS3.7 trở lên thì không cần dùng.
47
+ * @param value Giá trị cần kiểm tra
48
+ * @param defaultValue Giá trị mặc định trả về nếu `value` là null hoặc undefined
49
+ * @global
50
+ */
51
+ export function nullish(value, defaultValue) {
52
+ return value !== null && value !== undefined ? value : defaultValue;
53
+ }
54
+ /**
55
+ * JSON.parse với giá trị mặc định nếu có lỗi
56
+ * @param s giá trị JSON cần parse
57
+ * @param defaultValue giá trị mặc định trả về nếu có lỗi khi parse. Mặc định là null
58
+ * @returns any
59
+ */
60
+ export function jsonParse(s, defaultValue = null) {
61
+ try {
62
+ return JSON.parse(s);
63
+ }
64
+ catch (e) {
65
+ return defaultValue;
66
+ }
67
+ }
44
68
  /**
45
69
  * Converts Vietnamese diacritics to their plain ASCII equivalents
46
70
  * (e.g. `"Điều chỉnh"` -> `"Dieu chinh"`).
@@ -75,6 +99,40 @@ export function crlf2lf(value) {
75
99
  export function removeNewline(value) {
76
100
  return crlf2lf(value).trim().replace(/\n/g, "");
77
101
  }
102
+ /**
103
+ * Thêm ký tự vào đầu chuỗi cho đến khi đạt được độ dài mục tiêu
104
+ * @param str Chuỗi gốc
105
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
106
+ * @param padChar Ký tự dùng để thêm vào đầu chuỗi (mặc định là "0")
107
+ * @returns Chuỗi đã được thêm ký tự vào đầu nếu cần thiết
108
+ * @global
109
+ */
110
+ export function padStart(str, targetLength, padChar = "0") {
111
+ str = String(str);
112
+ if (str.length >= targetLength || !padChar)
113
+ return str;
114
+ const padding = padChar
115
+ .repeat(Math.ceil((targetLength - str.length) / padChar.length))
116
+ .substring(0, targetLength - str.length);
117
+ return padding + str;
118
+ }
119
+ /**
120
+ * Thêm ký tự vào cuối chuỗi cho đến khi đạt được độ dài mục tiêu
121
+ * @param str Chuỗi gốc
122
+ * @param targetLength Độ dài mục tiêu sau khi thêm ký tự
123
+ * @param padChar Ký tự dùng để thêm vào cuối chuỗi (mặc định là "0")
124
+ * @returns Chuỗi đã được thêm ký tự vào cuối nếu cần thiết
125
+ * @global
126
+ */
127
+ export function padEnd(str, targetLength, padChar = "0") {
128
+ str = String(str);
129
+ if (str.length >= targetLength || !padChar)
130
+ return str;
131
+ const padding = padChar
132
+ .repeat(Math.ceil((targetLength - str.length) / padChar.length))
133
+ .substring(0, targetLength - str.length);
134
+ return str + padding;
135
+ }
78
136
  /**
79
137
  * Shuffles an array in place using the Fisher-Yates algorithm.
80
138
  * @param array - Array to shuffle (mutated directly).
@@ -94,10 +152,9 @@ export function shuffleArray(array) {
94
152
  * @returns Array of the object's values.
95
153
  */
96
154
  export function objectValueToArray(obj) {
97
- var arr = [];
98
- for (var i in obj)
99
- arr.push(obj[i]);
100
- return arr;
155
+ if (obj === null || obj === undefined)
156
+ return [];
157
+ return Object.keys(obj).map((key) => obj[key]);
101
158
  }
102
159
  /**
103
160
  * Groups list items by the key returned from `fn`, similar to Lodash's
@@ -134,15 +191,18 @@ export function removeByKey(objectOrArray, keys) {
134
191
  return objectOrArray.map((item) => typeof item === "object" && item !== null ? removeByKey(item, keys) : item);
135
192
  }
136
193
  if (typeof objectOrArray === "object" && objectOrArray !== null) {
137
- const entries = Object.entries(objectOrArray)
138
- .filter(([key]) => !keys.includes(key))
139
- .map(([key, value]) => {
140
- if (Array.isArray(value) || (typeof value === "object" && value !== null)) {
141
- return [key, removeByKey(value, keys)];
142
- }
143
- return [key, value];
194
+ const source = objectOrArray;
195
+ const result = {};
196
+ Object.keys(source).forEach((key) => {
197
+ if (keys.includes(key))
198
+ return;
199
+ const value = source[key];
200
+ result[key] =
201
+ Array.isArray(value) || (typeof value === "object" && value !== null)
202
+ ? removeByKey(value, keys)
203
+ : value;
144
204
  });
145
- return Object.fromEntries(entries);
205
+ return result;
146
206
  }
147
207
  // Primitives are returned as-is
148
208
  return objectOrArray;
@@ -177,22 +237,18 @@ export function removeEmptyValue(objectOrArray, options = {}) {
177
237
  return result;
178
238
  }
179
239
  if (typeof objectOrArray === "object" && objectOrArray !== null) {
180
- const entries = Object.entries(objectOrArray)
181
- .map(([key, value]) => {
182
- if (Array.isArray(value) || (typeof value === "object" && value !== null)) {
183
- return [
184
- key,
185
- removeEmptyValue(value, {
186
- removeNull,
187
- removeUndefined,
188
- removeEmptyString,
189
- }),
190
- ];
240
+ const source = objectOrArray;
241
+ const result = {};
242
+ Object.keys(source).forEach((key) => {
243
+ const rawValue = source[key];
244
+ const value = Array.isArray(rawValue) || (typeof rawValue === "object" && rawValue !== null)
245
+ ? removeEmptyValue(rawValue, { removeNull, removeUndefined, removeEmptyString })
246
+ : rawValue;
247
+ if (!shouldRemove(value)) {
248
+ result[key] = value;
191
249
  }
192
- return [key, value];
193
- })
194
- .filter(([, value]) => !shouldRemove(value));
195
- return Object.fromEntries(entries);
250
+ });
251
+ return result;
196
252
  }
197
253
  return objectOrArray;
198
254
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khgtrn/lib",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Library for Typescript",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",