@mmb-digital/shared-libs-auto 0.0.22 → 0.0.24

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.
Files changed (124) hide show
  1. package/README.md +30 -14
  2. package/dist/formatters/formatBankAccount/utils/innerFormatBankAccount.js +1 -1
  3. package/dist/formatters/formatBytes/formatBytes.js +3 -6
  4. package/dist/formatters/formatBytes/formatBytes.test.js +7 -0
  5. package/dist/formatters/formatNumber/formatNumber.test.js +4 -0
  6. package/dist/formatters/formatNumber/utils/innerFormatNumber.js +16 -2
  7. package/dist/hooks/useDebounce/useDebounce.test.js +2 -1
  8. package/dist/hooks/usePreviousValue/usePreviousValue.js +1 -1
  9. package/dist/hooks/usePreviousValue/usePreviousValue.test.js +1 -1
  10. package/dist/hooks/useToggle/useToggle.test.js +19 -7
  11. package/dist/utils/arrayBufferToBase64/arrayBufferToBase64.js +1 -1
  12. package/dist/utils/assertions/assertNonNullish.d.ts +4 -0
  13. package/dist/utils/assertions/assertNonNullish.js +10 -0
  14. package/dist/utils/assertions/assertNonNullish.test.d.ts +1 -0
  15. package/dist/utils/assertions/assertNonNullish.test.js +19 -0
  16. package/dist/utils/assertions/assertValue.d.ts +5 -0
  17. package/dist/utils/assertions/assertValue.js +7 -0
  18. package/dist/utils/assertions/assertValue.test.d.ts +1 -0
  19. package/dist/utils/assertions/assertValue.test.js +27 -0
  20. package/dist/utils/base64ToArrayBuffer/base64ToArrayBuffer.js +1 -1
  21. package/dist/utils/chunkArray/chunkArray.js +6 -4
  22. package/dist/utils/chunkArray/chunkArray.test.js +5 -0
  23. package/dist/utils/cleanObject/cleanObject.d.ts +1 -1
  24. package/dist/utils/cleanObject/cleanObject.js +3 -16
  25. package/dist/utils/cleanObject/cleanObject.test.js +47 -9
  26. package/dist/utils/cleanObject/types.d.ts +6 -0
  27. package/dist/utils/cleanObject/types.js +1 -0
  28. package/dist/utils/cleanObject/utils/innerCleanObject.d.ts +6 -0
  29. package/dist/utils/cleanObject/utils/innerCleanObject.js +37 -0
  30. package/dist/utils/constants.d.ts +1 -0
  31. package/dist/utils/constants.js +1 -0
  32. package/dist/utils/getDateObject/getDateObject.d.ts +8 -1
  33. package/dist/utils/getDateObject/getDateObject.js +10 -2
  34. package/dist/utils/getDateObject/getDateObject.test.js +24 -3
  35. package/dist/utils/getDateObject/utils/parseDate.d.ts +6 -0
  36. package/dist/utils/getDateObject/utils/parseDate.js +21 -0
  37. package/dist/utils/getLastSpecificDate/getLastSpecificDate.d.ts +22 -0
  38. package/dist/utils/getLastSpecificDate/getLastSpecificDate.js +42 -0
  39. package/dist/utils/getLastSpecificDate/getLastSpecificDate.test.d.ts +1 -0
  40. package/dist/utils/getLastSpecificDate/getLastSpecificDate.test.js +108 -0
  41. package/dist/utils/getLastSpecificDate/utils/getSpecificDate.d.ts +4 -0
  42. package/dist/utils/getLastSpecificDate/utils/getSpecificDate.js +9 -0
  43. package/dist/utils/getNumber/getNumber.test.js +26 -26
  44. package/dist/utils/getNumberFloat/getNumberFloat.js +1 -1
  45. package/dist/utils/getNumberFloat/getNumberFloat.test.js +17 -17
  46. package/dist/utils/getObjectDiff/getObjectDiff.js +1 -1
  47. package/dist/utils/getObjectDiff/getObjectDiff.test.js +9 -0
  48. package/dist/utils/getObjectValue/getObjectValue.js +2 -1
  49. package/dist/utils/getObjectValue/getObjectValue.test.js +11 -8
  50. package/dist/utils/getQuarterFromDate/getQuarterFromDate.d.ts +4 -0
  51. package/dist/utils/getQuarterFromDate/getQuarterFromDate.js +26 -0
  52. package/dist/utils/getQuarterFromDate/getQuarterFromDate.test.d.ts +1 -0
  53. package/dist/utils/getQuarterFromDate/getQuarterFromDate.test.js +151 -0
  54. package/dist/utils/getWeekFromDate/getWeekFromDate.d.ts +4 -0
  55. package/dist/utils/getWeekFromDate/getWeekFromDate.js +28 -0
  56. package/dist/utils/getWeekFromDate/getWeekFromDate.test.d.ts +1 -0
  57. package/dist/utils/getWeekFromDate/getWeekFromDate.test.js +130 -0
  58. package/dist/utils/groupBy/groupBy.js +10 -3
  59. package/dist/utils/groupBy/groupBy.test.js +6 -0
  60. package/dist/utils/hasObjectProperty/hasObjectProperty.js +5 -4
  61. package/dist/utils/hasObjectProperty/hasObjectProperty.test.js +5 -0
  62. package/dist/utils/index.d.ts +5 -0
  63. package/dist/utils/index.js +5 -0
  64. package/dist/utils/mapObjectKeys/mapObjectKeys.js +6 -1
  65. package/dist/utils/mapObjectKeys/mapObjectKeys.test.js +7 -0
  66. package/dist/utils/mergeDeep/mergeDeep.js +2 -23
  67. package/dist/utils/mergeDeep/mergeDeep.test.js +5 -0
  68. package/dist/utils/mergeDeep/utils/innerMergeDeep.d.ts +5 -0
  69. package/dist/utils/mergeDeep/utils/innerMergeDeep.js +31 -0
  70. package/dist/utils/openFile/openFile.test.js +1 -1
  71. package/dist/utils/range/createRange.js +2 -0
  72. package/dist/utils/range/createRange.test.js +9 -5
  73. package/dist/utils/range/isInRange.js +2 -0
  74. package/dist/utils/range/isInRange.test.js +5 -0
  75. package/dist/utils/range/unionRange.test.js +2 -2
  76. package/dist/utils/removeSpecialChars/removeSpecialChars.test.js +9 -0
  77. package/dist/utils/removeWhitespacesFromUnknown/removeWhitespacesFromUnknown.test.js +2 -2
  78. package/dist/utils/setObjectValue/setObjectValue.js +1 -1
  79. package/dist/validators/index.d.ts +3 -0
  80. package/dist/validators/index.js +3 -0
  81. package/dist/validators/isBankAccount/isBankAccount.d.ts +3 -1
  82. package/dist/validators/isBankAccount/isBankAccount.js +11 -21
  83. package/dist/validators/isBankAccount/isBankAccount.test.js +6 -3
  84. package/dist/validators/isEmail/isEmail.js +5 -1
  85. package/dist/validators/isEmail/isEmail.test.js +15 -0
  86. package/dist/validators/isEqual/isEqual.d.ts +1 -1
  87. package/dist/validators/isEqual/isEqual.js +7 -3
  88. package/dist/validators/isEqual/isEqual.test.js +12 -0
  89. package/dist/validators/isModulo11/isModulo11.js +2 -0
  90. package/dist/validators/isModulo11/isModulo11.test.js +3 -0
  91. package/dist/validators/isPlainObject/isPlainObject.d.ts +4 -0
  92. package/dist/validators/isPlainObject/isPlainObject.js +9 -0
  93. package/dist/validators/isPlainObject/isPlainObject.test.d.ts +1 -0
  94. package/dist/validators/isPlainObject/isPlainObject.test.js +24 -0
  95. package/dist/validators/isValidDate/isValidDate.d.ts +1 -0
  96. package/dist/validators/isValidDate/isValidDate.js +4 -26
  97. package/dist/validators/isValidDate/isValidDate.test.js +1 -0
  98. package/dist/validators/isValidDateInstance/isValidDateInstance.d.ts +4 -0
  99. package/dist/validators/isValidDateInstance/isValidDateInstance.js +11 -0
  100. package/dist/validators/isValidDateInstance/isValidDateInstance.test.d.ts +1 -0
  101. package/dist/validators/isValidDateInstance/isValidDateInstance.test.js +67 -0
  102. package/dist/validators/isValidForeignPhoneNumber/isValidForeignPhoneNumber.d.ts +1 -1
  103. package/dist/validators/isValidForeignPhoneNumber/isValidForeignPhoneNumber.js +2 -2
  104. package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.d.ts +6 -0
  105. package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.js +27 -0
  106. package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.test.d.ts +1 -0
  107. package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.test.js +26 -0
  108. package/dist/yup/index.d.ts +2 -0
  109. package/dist/yup/index.js +2 -0
  110. package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.d.ts +12 -0
  111. package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.js +32 -0
  112. package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.test.d.ts +1 -0
  113. package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.test.js +64 -0
  114. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupTest.js +12 -4
  115. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupTest.js +12 -4
  116. package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.js +2 -0
  117. package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.test.js +6 -0
  118. package/dist/yup/minDigitsLength/minDigitsLengthYupTest.js +2 -0
  119. package/dist/yup/minDigitsLength/minDigitsLengthYupTest.test.js +6 -0
  120. package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.d.ts +6 -0
  121. package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.js +7 -0
  122. package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.test.d.ts +1 -0
  123. package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.test.js +24 -0
  124. package/package.json +27 -29
package/README.md CHANGED
@@ -27,7 +27,7 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
27
27
 
28
28
  ## Features
29
29
 
30
- ### Formaters
30
+ ### Formatters
31
31
 
32
32
  #### Bank Account
33
33
  - **`formatBankAccount(value: BankAccountInput, fallback?: string): string`** - Formats bank account number to standard format (prefix-account/bankCode).
@@ -53,7 +53,7 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
53
53
  - **`inputFormatICO(value: string | number | null | undefined): string | null`** - Formats ICO input in real-time while typing.
54
54
 
55
55
  #### Numbers
56
- - **`formatNumber(value: number | string | null | undefined, decimals?: number, fallback?: string): string`** - Formats number with options for separators, decimal places, and currency.
56
+ - **`formatNumber(value: number | string | null | undefined, decimals?: number, fallback?: string): string`** - Formats number with options for separators and decimal places.
57
57
  - **`formatPercent(value: number | string | null | undefined, isDecimalValue?: boolean, decimals?: number, fallback?: string): string`** - Formats number as percentage.
58
58
  - **`inputFormatNumber(value: number | string | null | undefined): string | null`** - Formats number input in real-time while typing.
59
59
  - **`inputFormatFloat(value: number | string | null | undefined): string | null`** - Formats decimal number input in real-time while typing.
@@ -78,11 +78,17 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
78
78
 
79
79
  ### Types
80
80
  - **`Prettify<T>`** - Prettify type
81
+ - **`Gender`** - Union of the exported birth-number gender values.
82
+ - **`Range`** - Object type with numeric `min` and `max` properties.
81
83
 
82
84
  ### Utils
83
85
 
86
+ #### Assertions
87
+ - **`assertValue(value: boolean, message?: string): asserts value`** / **`assertValue<T>(value: T | null | undefined, message?: string): asserts value is T`** - Throws an `Error` with the optional message if the value is `false`, `null`, or `undefined`; accepts other falsy values such as `0` and `''`.
88
+ - **`assertNonNullish<Value>(value: Value, message: string): asserts value is NonNullable<Value>`** - Throws an `Error` with the supplied message if the value is `null` or `undefined`; accepts `false` and other non-nullish values.
89
+
84
90
  #### Array Operations
85
- - **`chunkArray<T>(input: unknown, size: number): T[]`** - Splits an array into chunks of specified size.
91
+ - **`chunkArray<T>(input: unknown, size: number): T[][]`** - Splits an array into chunks of specified size.
86
92
  - **`groupBy<T, K extends string | number | symbol>(arr: T[], getKey: (item: T) => K): Record<K, T[] | undefined>`** - Groups array elements by a specified key.
87
93
  - **`joinBy(separator: string, stringArray: Array<string | undefined>): string`** - Joins array elements by a key with a separator.
88
94
  - **`xor<T extends unknown[], R extends unknown[]>(a: T, b: R): Array<T[number] | R[number]>`** - Returns symmetric difference between two arrays (elements that exist in only one array).
@@ -93,6 +99,11 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
93
99
  - **`getDateFromBirthNumber(value: string | undefined | null): Date | null`** - Extracts date of birth from a birth number.
94
100
  - **`getGenderFromBirthNumber(value: string | undefined | null): Gender`** - Determines gender from a birth number ('male' | 'female').
95
101
 
102
+ #### Date Operations
103
+ - **`getQuarterFromDate(input: unknown): number | null`** - Returns the quarter (1–4) for a valid date input, or `null` for an invalid input.
104
+ - **`getWeekFromDate(input: unknown): number | null`** - Returns the week number (1–53) for a valid date input, or `null` for an invalid input.
105
+ - **`getLastSpecificDate(input: unknown, dayOfWeek?: number): Date | null`** - Returns the last specific date of the week, or `null` for an invalid input. The default day is Monday.
106
+
96
107
  #### File Operations
97
108
  - **`arrayBufferToBase64(buffer: ArrayBuffer | Uint8Array): string`** - Converts ArrayBuffer to Base64 string.
98
109
  - **`base64ToArrayBuffer(base64: string): Uint8Array<ArrayBuffer>`** - Converts Base64 string to ArrayBuffer.
@@ -108,7 +119,7 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
108
119
  - **`getSafeMin(a: number | null | undefined, b: number | null | undefined): number | undefined`** - Returns min of two numbers with `null`/`undefined` fallback handling.
109
120
 
110
121
  #### Object Operations
111
- - **`cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): Partial<T>`** - Removes undefined and null values from an object.
122
+ - **`cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): Partial<T>`** - Deeply removes undefined, null, empty string values, empty array and empty objects from an object.
112
123
  - **`getObjectDiff<T extends Record<string, unknown>, R extends Record<string, unknown>>(object: T | undefined | null, base: R | undefined | null): Partial<T>`** - Returns differences between two objects.
113
124
  - **`getObjectValue(obj: Record<string, unknown> | null | undefined, path: string | string[], defaultValue?: unknown): unknown`** - Gets nested object value by path string (e.g., 'user.address.city').
114
125
  - **`setObjectValue<T extends Record<string, unknown>>(obj: T, path: string, value: unknown): T`** - Sets nested object value by path string.
@@ -134,8 +145,8 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
134
145
  - **`makeAbsolutePath(...relativePaths: string[]): string`** - Converts relative path to absolute URL.
135
146
 
136
147
  #### Browser Operations
137
- - **`scrollToBottom(): void`** - Scrolls page to bottom, optionally with smooth animation.
138
- - **`scrollToTop(): void`** - Scrolls page to top, optionally with smooth animation.
148
+ - **`scrollToBottom(): void`** - Smoothly scrolls the page to the bottom.
149
+ - **`scrollToTop(): void`** - Smoothly scrolls the page to the top.
139
150
  - **`getDateObject(input: string): Date | null`** - Validates a date string and returns a `Date` instance, or `null` for invalid input.
140
151
 
141
152
  ### Validators
@@ -144,21 +155,23 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
144
155
  - **`isArrayBuffer(value: unknown): value is ArrayBuffer`** - Checks if a value is an ArrayBuffer.
145
156
  - **`isBlob(value: unknown): value is Blob`** - Checks if a value is a Blob.
146
157
  - **`isBoolean(value: unknown): value is boolean`** - Checks if a value is a boolean.
147
- - **`isFile(value: unknown): boolean`** - Checks if a value is instance of File
158
+ - **`isFile(value: unknown): value is File`** - Checks if value is an instance of File.
148
159
  - **`isFunction(value: unknown): value is Function`** - Checks if a value is a function.
149
160
  - **`isNull(value: unknown): value is null`** - Checks if a value is null.
150
161
  - **`isNullOrUndefined(value: unknown): value is null | undefined`** - Checks if a value is null or undefined.
151
162
  - **`isNumber(value: unknown): value is number`** - Checks if a value is a number.
152
163
  - **`isNumberOrString(value: unknown): value is number | string`** - Checks if a value is a number or string.
153
164
  - **`isObject<T extends Record<string, unknown>>(value: unknown): value is T`** - Checks if a value is an object (excluding null and arrays).
165
+ - **`isPlainObject(value: unknown): value is Record<string, unknown>`** - Checks if a value is a plain object with an Object or null prototype.
154
166
  - **`isString(value: unknown): value is string`** - Checks if a value is a string.
155
167
  - **`isUndefined(value: unknown): value is undefined`** - Checks if a value is undefined.
156
168
 
157
169
  #### Value Validators
158
170
  - **`isEmpty<T>(value: T | undefined | null): value is undefined | null`** - Checks if a value is empty (null, undefined, empty string, empty array, or empty object).
159
171
  - **`isEmptyString(value: unknown): value is ''`** - Checks whether a value is exactly an empty string (`''`).
160
- - **`isEqual(a: unknown, b: unknown): boolean`** - Deep equality comparison between two values.
172
+ - **`isEqual(a: unknown, b: unknown): boolean`** - Compares primitives, arrays, plain objects, and Date instances by value; other object instances are compared by reference.
161
173
  - **`isValidDate(input: string | null | undefined): boolean`** - Checks whether a date string is parseable into a valid `Date`.
174
+ - **`isValidDateInstance(input: unknown): input is Date`** - Checks if the provided input is a valid Date instance.
162
175
 
163
176
  #### String Format Validators
164
177
  - **`isEmail(value: string | undefined | null): boolean`** - Validates if a string is a valid email address.
@@ -182,20 +195,23 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
182
195
  - **`isValidIdentityCard(value: string | number | null | undefined): boolean`** - Validates Czech identity card number format.
183
196
  - **`isValidPassport(value: string | null | undefined): boolean`** - Validates Czech passport number format.
184
197
  - **`isValidVehicleIdentification(value: string | null | undefined): boolean`** - Validates vehicle identification number (VIN) format.
198
+ - **`isValidMrzCheckDigit(value: string | number | null | undefined, checkDigit: string | number | null | undefined): boolean`** - Validates MRZ check digit.
185
199
 
186
200
  #### Yup Validators
201
+ - **`yupNumberEmptyTransform(value: number, originalValue: unknown): number | undefined`** - Transforms an empty string to `undefined` so Yup `required()` or `default()` can handle empty number inputs without a `NaN` cast error.
187
202
  - **`isBirthNumberYupTest(message: string)`** - Creates Yup `test` config for Czech birth number validation.
188
- - **`isDateSameOrAfterYupWhen(message: string)`** - Creates Yup `when` condition for reference date being same or after a date.
189
- - **`isDateSameOrBeforeYupWhen(message: string)`** - Creates Yup `when` condition for reference date being same or before a date.
203
+ - **`isDateSameOrAfterYupWhen(message: string)(maxDate: unknown[], schema: StringSchema)`** - Creates Yup `when` condition for reference date being same or after a date.
204
+ - **`isDateSameOrBeforeYupWhen(message: string)(minDate: unknown[], schema: StringSchema)`** - Creates Yup `when` condition for reference date being same or before a date.
190
205
  - **`isDateYupTest(message: string)`** - Creates Yup `test` config for valid date validation.
191
206
  - **`isDICYupTest(message: string)`** - Creates Yup `test` config for DIČ validation.
192
207
  - **`isEmailYupTest(message: string)`** - Creates Yup `test` config for email validation.
193
208
  - **`isModulo11YupTest(message: string)`** - Creates Yup `test` config for modulo 11 validation.
194
209
  - **`isICOYupTest(message: string)`** - Creates Yup `test` config for IČO validation.
195
- - **`isNumberSameOrGreaterYupWhen(message: string)`** - Creates Yup `when` condition for number being same or greater than reference.
196
- - **`isNumberSameOrLowerYupWhen(message: string)`** - Creates Yup `when` condition for number being same or lower than reference.
197
- - **`maxDigitsLengthYupTest(maxDigits: number, message: string)`** - Creates Yup `test` config for maximum digits length.
198
- - **`minDigitsLengthYupTest(minDigits: number, message: string)`** - Creates Yup `test` config for minimum digits length.
210
+ - **`isNumberSameOrGreaterYupWhen(message: string)(max: unknown[], schema: NumberSchema)`** - Creates Yup `when` condition for number being same or greater than reference.
211
+ - **`isNumberSameOrLowerYupWhen(message: string)(min: unknown[], schema: NumberSchema)`** - Creates Yup `when` condition for number being same or lower than reference.
212
+ - **`isDateAtLeastYearsAgoYupTest(minimumYears: number, message: string)`** - Creates Yup `test` config for minimum year validation.
213
+ - **`maxDigitsLengthYupTest(max: number, message: (params: { max: number }) => string)`** - Creates Yup `test` config for maximum digits length.
214
+ - **`minDigitsLengthYupTest(min: number, message: (params: { min: number }) => string)`** - Creates Yup `test` config for minimum digits length.
199
215
 
200
216
 
201
217
  ## License
@@ -4,5 +4,5 @@
4
4
  export const innerFormatBankAccount = (value) => {
5
5
  const { prefix, number, bankCode } = value;
6
6
  const accountNumber = [number.trim(), bankCode.trim()].join('/');
7
- return [prefix, accountNumber].filter(Boolean).join('-');
7
+ return [prefix, accountNumber].filter(Boolean).join('-').trim();
8
8
  };
@@ -1,5 +1,6 @@
1
1
  import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
2
2
  import { EMPTY_VALUE_PLACEHOLDER } from '../const.js';
3
+ const MAX_DECIMALS = 100;
3
4
  /**
4
5
  * Formats a number of bytes into a human-readable string.
5
6
  * @example "1.5 MB"
@@ -7,16 +8,12 @@ import { EMPTY_VALUE_PLACEHOLDER } from '../const.js';
7
8
  export const formatBytes = (bytes, decimals = 2, fallback = EMPTY_VALUE_PLACEHOLDER) => {
8
9
  if (isNullOrUndefined(bytes))
9
10
  return fallback;
10
- if (bytes < 0)
11
- return fallback;
12
- if (bytes === Number.POSITIVE_INFINITY)
11
+ if (!Number.isFinite(bytes) || bytes < 0)
13
12
  return fallback;
14
13
  if (bytes === 0)
15
14
  return '0 Bytes';
16
- if (decimals < 0)
17
- decimals = 0;
18
15
  const k = 1024;
19
- const dm = Math.max(0, decimals);
16
+ const dm = Number.isFinite(decimals) ? Math.min(MAX_DECIMALS, Math.max(0, decimals)) : 0;
20
17
  const sizes = ['Bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
21
18
  const i = Math.floor(Math.log(bytes) / Math.log(k));
22
19
  return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
@@ -21,6 +21,10 @@ describe('formatBytes', () => {
21
21
  it('should handle negative decimals as 0', () => {
22
22
  expect(formatBytes(1536, -1)).toBe('2 kB');
23
23
  });
24
+ it('should normalize invalid decimal counts', () => {
25
+ expect(formatBytes(1536, 101)).toBe(formatBytes(1536, 100));
26
+ expect(formatBytes(1536, Number.POSITIVE_INFINITY)).toBe('2 kB');
27
+ });
24
28
  it('should format large numbers', () => {
25
29
  expect(formatBytes(1024 ** 4)).toBe('1 TB');
26
30
  });
@@ -34,6 +38,9 @@ describe('formatBytes', () => {
34
38
  it('should return fallback for positive infinity', () => {
35
39
  expect(formatBytes(Number.POSITIVE_INFINITY)).toBe(EMPTY_VALUE_PLACEHOLDER);
36
40
  });
41
+ it('should return fallback for NaN', () => {
42
+ expect(formatBytes(Number.NaN)).toBe(EMPTY_VALUE_PLACEHOLDER);
43
+ });
37
44
  it('should return fallback for negative infinity', () => {
38
45
  expect(formatBytes(Number.NEGATIVE_INFINITY)).toBe(EMPTY_VALUE_PLACEHOLDER);
39
46
  });
@@ -18,6 +18,10 @@ describe('formatNumber', () => {
18
18
  expect(formatNumber(1234.56, 2)).toBe('1 234,56');
19
19
  expect(formatNumber('5678.99', 1)).toBe('5 679,0');
20
20
  });
21
+ it('normalizes invalid decimal counts', () => {
22
+ expect(formatNumber(1.5, 101)).toBe(formatNumber(1.5, 100));
23
+ expect(formatNumber(1234.56, Number.POSITIVE_INFINITY)).toBe('1 235');
24
+ });
21
25
  it('formats negative numbers', () => {
22
26
  expect(formatNumber(-1234.56, 2)).toBe('-1 234,56');
23
27
  expect(formatNumber('-5678.99', 1)).toBe('-5 679,0');
@@ -1,6 +1,20 @@
1
- const THOUSAND_SEPARATOR_REG = /\B(?=(\d{3})+(?!\d))/g;
1
+ const MAX_DECIMALS = 100;
2
+ /**
3
+ * Formats the integer part of a number with space as thousand separator.
4
+ */
5
+ const formatIntegerPart = (integerPart) => {
6
+ const sign = integerPart.startsWith('-') ? '-' : '';
7
+ const digits = sign ? integerPart.slice(1) : integerPart;
8
+ const reversedDigits = digits.split('').reverse().join('');
9
+ const formattedDigits = reversedDigits.replace(/(\d{3})/g, '$1 ').trimEnd();
10
+ return `${sign}${formattedDigits.split('').reverse().join('')}`;
11
+ };
2
12
  /**
3
13
  * Internal utility to format a number with specified decimal places,
4
14
  * using comma as decimal separator and space as thousand separator.
5
15
  */
6
- export const innerFormatNumber = (valueNumber, decimals) => valueNumber.toFixed(decimals).replace('.', ',').replaceAll(THOUSAND_SEPARATOR_REG, ' ');
16
+ export const innerFormatNumber = (valueNumber, decimals) => valueNumber
17
+ .toFixed(Number.isFinite(decimals) ? Math.min(MAX_DECIMALS, Math.max(0, decimals)) : 0)
18
+ .split('.')
19
+ .map((part, index) => (index === 0 ? formatIntegerPart(part) : part))
20
+ .join(',');
@@ -1,11 +1,12 @@
1
1
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { act, renderHook } from '@testing-library/react-hooks';
2
+ import { act, cleanup, renderHook } from '@testing-library/react';
3
3
  import { useDebounce } from './useDebounce.js';
4
4
  describe('useDebounce', () => {
5
5
  beforeEach(() => {
6
6
  vi.useFakeTimers();
7
7
  });
8
8
  afterEach(() => {
9
+ cleanup();
9
10
  vi.useRealTimers();
10
11
  });
11
12
  it('returns the initial value immediately', () => {
@@ -5,7 +5,7 @@ import { isEqual } from '../../validators/isEqual/isEqual.js';
5
5
  * Useful for comparing previous and current values in functional components.
6
6
  */
7
7
  export const usePreviousValue = (value) => {
8
- const ref = useRef();
8
+ const ref = useRef(undefined);
9
9
  useEffect(() => {
10
10
  if (!isEqual(value, ref.current)) {
11
11
  ref.current = value;
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'vitest';
2
- import { renderHook } from '@testing-library/react-hooks';
2
+ import { renderHook } from '@testing-library/react';
3
3
  import { usePreviousValue } from './usePreviousValue.js';
4
4
  describe('usePreviousValue', () => {
5
5
  test('should return undefined on first render', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'vitest';
2
- import { renderHook } from '@testing-library/react-hooks';
2
+ import { act, renderHook } from '@testing-library/react';
3
3
  import { useToggle } from './useToggle.js';
4
4
  describe('useToggle', () => {
5
5
  test('should initialize with defaultState = false', () => {
@@ -18,23 +18,35 @@ describe('useToggle', () => {
18
18
  });
19
19
  test('should toggle state', () => {
20
20
  const { result } = renderHook(() => useToggle());
21
- result.current[1]();
21
+ act(() => {
22
+ result.current[1]();
23
+ });
22
24
  expect(result.current[0]).toBe(true);
23
- result.current[1]();
25
+ act(() => {
26
+ result.current[1]();
27
+ });
24
28
  expect(result.current[0]).toBe(false);
25
29
  });
26
30
  test('should open and close', () => {
27
31
  const { result } = renderHook(() => useToggle());
28
- result.current[2].open();
32
+ act(() => {
33
+ result.current[2].open();
34
+ });
29
35
  expect(result.current[0]).toBe(true);
30
- result.current[2].close();
36
+ act(() => {
37
+ result.current[2].close();
38
+ });
31
39
  expect(result.current[0]).toBe(false);
32
40
  });
33
41
  test('should set state directly', () => {
34
42
  const { result } = renderHook(() => useToggle());
35
- result.current[3](true);
43
+ act(() => {
44
+ result.current[3](true);
45
+ });
36
46
  expect(result.current[0]).toBe(true);
37
- result.current[3](false);
47
+ act(() => {
48
+ result.current[3](false);
49
+ });
38
50
  expect(result.current[0]).toBe(false);
39
51
  });
40
52
  });
@@ -6,5 +6,5 @@ export const arrayBufferToBase64 = (buffer) => {
6
6
  const binary = Array.from(bytes)
7
7
  .map(byte => String.fromCharCode(byte))
8
8
  .join('');
9
- return globalThis.window.btoa(binary);
9
+ return globalThis.btoa(binary);
10
10
  };
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Asserts that a value is not nullish (null or undefined).
3
+ */
4
+ export declare function assertNonNullish<Value>(value: Value, message: string): asserts value is NonNullable<Value>;
@@ -0,0 +1,10 @@
1
+ import { isNull } from '../../validators/isNull/isNull.js';
2
+ import { isUndefined } from '../../validators/isUndefined/isUndefined.js';
3
+ /**
4
+ * Asserts that a value is not nullish (null or undefined).
5
+ */
6
+ export function assertNonNullish(value, message) {
7
+ if (isUndefined(value) || isNull(value)) {
8
+ throw new Error(message);
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import { describe, expect, expectTypeOf, it } from 'vitest';
2
+ import { assertNonNullish } from './assertNonNullish.js';
3
+ describe('assertNonNullish', () => {
4
+ it.each([null, undefined])('throws the supplied message for %s', value => {
5
+ expect(() => assertNonNullish(value, 'Value is required')).toThrow('Value is required');
6
+ });
7
+ it.each([false, true, 0, '', Number.NaN, 0n, [], {}])('accepts %s', value => {
8
+ expect(() => assertNonNullish(value, 'Value is required')).not.toThrow();
9
+ });
10
+ it('narrows nullable values while preserving false', () => {
11
+ const useValue = (value) => {
12
+ assertNonNullish(value, 'Value is required');
13
+ expectTypeOf(value).toEqualTypeOf();
14
+ return value;
15
+ };
16
+ expect(useValue(false)).toBe(false);
17
+ expect(useValue('value')).toBe('value');
18
+ });
19
+ });
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Asserts that a value is not nullish (null or undefined) or false.
3
+ */
4
+ export declare function assertValue(value: boolean, message?: string): asserts value;
5
+ export declare function assertValue<T>(value: T | null | undefined, message?: string): asserts value is T;
@@ -0,0 +1,7 @@
1
+ import { isNull } from '../../validators/isNull/isNull.js';
2
+ import { isUndefined } from '../../validators/isUndefined/isUndefined.js';
3
+ export function assertValue(value, message) {
4
+ if (value === false || isNull(value) || isUndefined(value)) {
5
+ throw new Error(message);
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { describe, expect, expectTypeOf, it } from 'vitest';
2
+ import { assertValue } from './assertValue.js';
3
+ describe('assertValue', () => {
4
+ it.each([null, undefined, false])('throws the supplied message for %s', value => {
5
+ expect(() => assertValue(value, 'Value is required')).toThrow('Value is required');
6
+ });
7
+ it('throws an error without a message when none is supplied', () => {
8
+ expect(() => assertValue(false)).toThrow(new Error());
9
+ });
10
+ it.each([true, 0, '', Number.NaN, 0n, [], {}])('accepts %s', value => {
11
+ expect(() => assertValue(value)).not.toThrow();
12
+ });
13
+ it('narrows nullable values and boolean conditions', () => {
14
+ const useValue = (value) => {
15
+ assertValue(value);
16
+ expectTypeOf(value).toEqualTypeOf();
17
+ return value.toUpperCase();
18
+ };
19
+ const checkCondition = (condition) => {
20
+ assertValue(condition);
21
+ expectTypeOf(condition).toEqualTypeOf();
22
+ return condition;
23
+ };
24
+ expect(useValue('value')).toBe('VALUE');
25
+ expect(checkCondition(true)).toBe(true);
26
+ });
27
+ });
@@ -2,7 +2,7 @@
2
2
  * Converts a base64 string to an ArrayBuffer.
3
3
  */
4
4
  export const base64ToArrayBuffer = (base64) => {
5
- const binaryString = globalThis.window.atob(base64);
5
+ const binaryString = globalThis.atob(base64);
6
6
  const binaryLen = binaryString.length;
7
7
  const bytes = new Uint8Array(binaryLen);
8
8
  return bytes.map((byte, index) => binaryString.charCodeAt(index));
@@ -5,9 +5,11 @@
5
5
  export const chunkArray = (input, size) => {
6
6
  if (!Array.isArray(input))
7
7
  return [];
8
- if (size <= 0)
8
+ if (!Number.isInteger(size) || size <= 0)
9
9
  return [];
10
- return input.reduce((arr, item, idx) => {
11
- return idx % size === 0 ? [...arr, [item]] : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
12
- }, []);
10
+ const chunks = [];
11
+ for (let index = 0; index < input.length; index += size) {
12
+ chunks.push(input.slice(index, index + size));
13
+ }
14
+ return chunks;
13
15
  };
@@ -30,4 +30,9 @@ describe('chunkArray', () => {
30
30
  expect(chunkArray([1, 2, 3], 0)).toEqual([]);
31
31
  expect(chunkArray([1, 2, 3], -1)).toEqual([]);
32
32
  });
33
+ test('returns an empty array for non-integer or non-finite size', () => {
34
+ expect(chunkArray([1, 2, 3], 1.5)).toEqual([]);
35
+ expect(chunkArray([1, 2, 3], Number.NaN)).toEqual([]);
36
+ expect(chunkArray([1, 2, 3], Number.POSITIVE_INFINITY)).toEqual([]);
37
+ });
33
38
  });
@@ -1,4 +1,4 @@
1
1
  /**
2
- * Deeply removes undefined, null, and empty string values from an object.
2
+ * Deeply removes undefined, null, empty string values, empty array and empty objects from an object.
3
3
  */
4
4
  export declare function cleanObject<T extends Record<string, unknown>>(obj: T | undefined | null): Partial<T>;
@@ -1,20 +1,7 @@
1
- import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
2
- import { isObject } from '../../validators/isObject/isObject.js';
3
- import { isString } from '../../validators/isString/isString.js';
1
+ import { innerCleanObject } from './utils/innerCleanObject.js';
4
2
  /**
5
- * Deeply removes undefined, null, and empty string values from an object.
3
+ * Deeply removes undefined, null, empty string values, empty array and empty objects from an object.
6
4
  */
7
5
  export function cleanObject(obj) {
8
- if (isNullOrUndefined(obj) || !isObject(obj))
9
- return {};
10
- const cleanedObject = Object.entries(obj).reduce((result, [key, value]) => {
11
- if (isNullOrUndefined(value) || (isString(value) && value.trim() === ''))
12
- return result;
13
- if (isObject(value)) {
14
- const cleanedNestedObject = cleanObject(value);
15
- return { ...result, [key]: cleanedNestedObject };
16
- }
17
- return { ...result, [key]: value };
18
- }, {});
19
- return cleanedObject;
6
+ return innerCleanObject(obj, new WeakSet());
20
7
  }
@@ -16,9 +16,7 @@ describe('cleanObject', () => {
16
16
  expect(cleanObject(obj)).toEqual({
17
17
  e: 'valid',
18
18
  f: 0,
19
- g: false,
20
- h: [],
21
- i: {}
19
+ g: false
22
20
  });
23
21
  });
24
22
  test('preserves non-empty strings, numbers, booleans, objects, arrays', () => {
@@ -39,22 +37,62 @@ describe('cleanObject', () => {
39
37
  const obj = { a: ' ', b: 'not empty ', c: 'valid' };
40
38
  expect(cleanObject(obj)).toEqual({ b: 'not empty ', c: 'valid' });
41
39
  });
42
- test('does not remove zero, false, empty arrays/objects', () => {
43
- const obj = { a: 0, b: false, c: [], d: {} };
40
+ test('does not remove zero and false', () => {
41
+ const obj = { a: 0, b: false };
44
42
  expect(cleanObject(obj)).toEqual(obj);
45
43
  });
46
- test('does clean nested objects', () => {
44
+ test('does clean nested objects and arrays', () => {
47
45
  const obj = {
48
46
  a: { x: null, y: '' },
49
- b: [''],
47
+ b: ['', null, undefined, 'valid'],
50
48
  c: 'valid',
51
49
  d: null,
52
50
  e: ''
53
51
  };
54
52
  expect(cleanObject(obj)).toEqual({
55
- a: {},
56
- b: [''],
53
+ b: ['valid'],
57
54
  c: 'valid'
58
55
  });
59
56
  });
57
+ test('does clean deeply nested objects and arrays', () => {
58
+ const obj = {
59
+ a: [0, 1, false, true, '', 'ok', null, undefined],
60
+ b: [{ x: null }, { y: 'valid' }, { z: '' }, { w: undefined }],
61
+ c: [{ x: [{ y: [{ z: null }] }] }]
62
+ };
63
+ expect(cleanObject(obj)).toEqual({
64
+ a: [0, 1, false, true, 'ok'],
65
+ b: [{ y: 'valid' }]
66
+ });
67
+ });
68
+ test('removes whitespace-only strings inside arrays', () => {
69
+ const obj = { a: [' ', 'valid', '\t', 'ok'] };
70
+ expect(cleanObject(obj)).toEqual({ a: ['valid', 'ok'] });
71
+ });
72
+ test('return empty object if all values are null, undefined, or empty strings', () => {
73
+ const obj = {
74
+ a: null,
75
+ b: undefined,
76
+ c: '',
77
+ d: ' ',
78
+ e: { f: null, g: '' },
79
+ h: [null, undefined, ''],
80
+ q: [{ x: null }, { y: '' }]
81
+ };
82
+ expect(cleanObject(obj)).toEqual({});
83
+ });
84
+ test('preserves non-plain object values', () => {
85
+ const date = new Date('2025-01-01');
86
+ const map = new Map([['key', 'value']]);
87
+ expect(cleanObject({ date, map, values: [date, map] })).toEqual({
88
+ date,
89
+ map,
90
+ values: [date, map]
91
+ });
92
+ });
93
+ test('does not stack overflow on circular references', () => {
94
+ const obj = { a: 'valid' };
95
+ obj.self = obj;
96
+ expect(() => cleanObject(obj)).not.toThrow();
97
+ });
60
98
  });
@@ -0,0 +1,6 @@
1
+ type CleanedArrayItem<Item> = Item extends Record<string, unknown> ? CleanedObject<Item> : Exclude<Item, null | undefined | ''>;
2
+ type CleanedArray<A> = A extends Array<infer Item> ? Array<CleanedArrayItem<Item>> : never;
3
+ export type CleanedObject<T extends Record<string, unknown>> = {
4
+ [K in keyof T]?: NonNullable<T[K]> extends Array<unknown> ? CleanedArray<NonNullable<T[K]>> : NonNullable<T[K]> extends Record<string, unknown> ? CleanedObject<NonNullable<T[K]>> : Exclude<T[K], null | ''>;
5
+ };
6
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { CleanedObject } from '../types.js';
2
+ /**
3
+ * Internal utility that recursively removes undefined, null, empty string values, empty array
4
+ * and empty objects from an object, guarding against circular references via `seen`.
5
+ */
6
+ export declare const innerCleanObject: <T extends Record<string, unknown>>(obj: T | undefined | null, seen: WeakSet<object>) => CleanedObject<T>;
@@ -0,0 +1,37 @@
1
+ import { isNullOrUndefined } from '../../../validators/isNullOrUndefined/isNullOrUndefined.js';
2
+ import { isPlainObject } from '../../../validators/isPlainObject/isPlainObject.js';
3
+ import { isString } from '../../../validators/isString/isString.js';
4
+ /**
5
+ * Internal utility that recursively removes undefined, null, empty string values, empty array
6
+ * and empty objects from an object, guarding against circular references via `seen`.
7
+ */
8
+ export const innerCleanObject = (obj, seen) => {
9
+ if (isNullOrUndefined(obj) || !isPlainObject(obj))
10
+ return {};
11
+ if (seen.has(obj))
12
+ return {};
13
+ seen.add(obj);
14
+ const cleanedObject = Object.entries(obj).reduce((result, [key, value]) => {
15
+ if (isNullOrUndefined(value) || (isString(value) && value.trim() === ''))
16
+ return result;
17
+ if (isPlainObject(value)) {
18
+ const cleanedNestedObject = innerCleanObject(value, seen);
19
+ if (Object.keys(cleanedNestedObject).length === 0)
20
+ return result;
21
+ return { ...result, [key]: cleanedNestedObject };
22
+ }
23
+ if (Array.isArray(value)) {
24
+ const cleanedArray = value
25
+ .map(item => (isPlainObject(item) ? innerCleanObject(item, seen) : item))
26
+ .filter(item => !isNullOrUndefined(item) &&
27
+ !(isString(item) && item.trim() === '') &&
28
+ !(isPlainObject(item) && Object.keys(item).length === 0));
29
+ if (cleanedArray.length === 0)
30
+ return result;
31
+ return { ...result, [key]: cleanedArray };
32
+ }
33
+ return { ...result, [key]: value };
34
+ }, {});
35
+ seen.delete(obj);
36
+ return cleanedObject;
37
+ };
@@ -0,0 +1 @@
1
+ export declare const DANGEROUS_KEYS: Set<string>;
@@ -0,0 +1 @@
1
+ export const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
@@ -1,4 +1,11 @@
1
1
  /**
2
- * Get Date object from string.
2
+ * Gets a Date object from a supported date string.
3
+ *
4
+ * Supported formats:
5
+ * - `2025-05-21 12:34:51` (YYYY-MM-DD HH:mm:ss)
6
+ * - `2025-05-21 12:34` (YYYY-MM-DD HH:mm)
7
+ * - `2025-09-03T13:32:21.582Z` (ISO 8601)
8
+ * - `2025-05-21` (YYYY-MM-DD)
9
+ * - `05/21/2025` (MM/DD/YYYY)
3
10
  */
4
11
  export declare const getDateObject: (input: string) => Date | null;