@mmb-digital/shared-libs-auto 0.0.20 → 0.0.21

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 (106) hide show
  1. package/README.md +19 -0
  2. package/dist/formatters/formatBytes/formatBytes.js +6 -0
  3. package/dist/formatters/formatBytes/formatBytes.test.js +9 -0
  4. package/dist/formatters/formatCZSKPhoneNumber/formatCZSKPhoneNumber.js +1 -7
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.js +2 -0
  7. package/dist/types/Prettify/Prettify.d.ts +19 -0
  8. package/dist/types/Prettify/Prettify.js +1 -0
  9. package/dist/types/index.d.ts +1 -0
  10. package/dist/types/index.js +1 -0
  11. package/dist/utils/arrayBufferToBase64/arrayBufferToBase64.js +1 -1
  12. package/dist/utils/base64ToArrayBuffer/base64ToArrayBuffer.js +1 -1
  13. package/dist/utils/chunkArray/chunkArray.d.ts +1 -1
  14. package/dist/utils/downloadFile/downloadFile.test.js +25 -25
  15. package/dist/utils/downloadFile/utils/processDownload.js +5 -5
  16. package/dist/utils/joinBy/joinBy.d.ts +1 -1
  17. package/dist/utils/joinBy/joinBy.js +1 -1
  18. package/dist/utils/mapObjectKeys/mapObjectKeys.js +1 -1
  19. package/dist/utils/mapObjectKeys/mapObjectKeys.test.js +7 -0
  20. package/dist/utils/mergeDeep/mergeDeep.js +2 -3
  21. package/dist/utils/openFile/openFile.test.js +7 -7
  22. package/dist/utils/openFile/utils/innerOpenFile.js +2 -2
  23. package/dist/utils/scrollToBottom/scrollToBottom.js +3 -3
  24. package/dist/utils/scrollToBottom/scrollToBottom.test.js +4 -4
  25. package/dist/utils/scrollToTop/scrollToTop.js +2 -2
  26. package/dist/utils/scrollToTop/scrollToTop.test.js +3 -3
  27. package/dist/utils/setObjectValue/setObjectValue.js +40 -7
  28. package/dist/utils/setObjectValue/setObjectValue.test.js +26 -0
  29. package/dist/validators/index.d.ts +1 -0
  30. package/dist/validators/index.js +1 -0
  31. package/dist/validators/isEmpty/isEmpty.d.ts +7 -0
  32. package/dist/validators/isEmpty/isEmpty.js +7 -0
  33. package/dist/validators/isFile/isFile.d.ts +4 -0
  34. package/dist/validators/isFile/isFile.js +4 -0
  35. package/dist/validators/isFile/isFile.test.d.ts +1 -0
  36. package/dist/validators/isFile/isFile.test.js +37 -0
  37. package/dist/validators/isValidDate/isValidDate.d.ts +13 -0
  38. package/dist/validators/isValidDate/isValidDate.js +39 -3
  39. package/dist/validators/isValidDate/isValidDate.test.js +13 -15
  40. package/dist/yup/index.d.ts +12 -0
  41. package/dist/yup/index.js +12 -0
  42. package/dist/yup/isBirthNumber/isBirthNumberYupTest.d.ts +12 -0
  43. package/dist/yup/isBirthNumber/isBirthNumberYupTest.js +24 -0
  44. package/dist/yup/isBirthNumber/isBirthNumberYupTest.test.d.ts +1 -0
  45. package/dist/yup/isBirthNumber/isBirthNumberYupTest.test.js +34 -0
  46. package/dist/yup/isDICYup/isDICYupTest.d.ts +12 -0
  47. package/dist/yup/isDICYup/isDICYupTest.js +24 -0
  48. package/dist/yup/isDICYup/isDICYupTest.test.d.ts +1 -0
  49. package/dist/yup/isDICYup/isDICYupTest.test.js +36 -0
  50. package/dist/yup/isDate/isDateYupTest.d.ts +12 -0
  51. package/dist/yup/isDate/isDateYupTest.js +25 -0
  52. package/dist/yup/isDate/isDateYupTest.test.d.ts +1 -0
  53. package/dist/yup/isDate/isDateYupTest.test.js +41 -0
  54. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupTest.d.ts +2 -0
  55. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupTest.js +26 -0
  56. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupTest.test.d.ts +1 -0
  57. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupTest.test.js +32 -0
  58. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupWhen.d.ts +13 -0
  59. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupWhen.js +13 -0
  60. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupWhen.test.d.ts +1 -0
  61. package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupWhen.test.js +28 -0
  62. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupTest.d.ts +2 -0
  63. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupTest.js +26 -0
  64. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupTest.test.d.ts +1 -0
  65. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupTest.test.js +32 -0
  66. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupWhen.d.ts +13 -0
  67. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupWhen.js +13 -0
  68. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupWhen.test.d.ts +1 -0
  69. package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupWhen.test.js +28 -0
  70. package/dist/yup/isEmail/isEmailYupTest.d.ts +12 -0
  71. package/dist/yup/isEmail/isEmailYupTest.js +24 -0
  72. package/dist/yup/isEmail/isEmailYupTest.test.d.ts +1 -0
  73. package/dist/yup/isEmail/isEmailYupTest.test.js +36 -0
  74. package/dist/yup/isICO/isICOYupTest.d.ts +12 -0
  75. package/dist/yup/isICO/isICOYupTest.js +24 -0
  76. package/dist/yup/isICO/isICOYupTest.test.d.ts +1 -0
  77. package/dist/yup/isICO/isICOYupTest.test.js +36 -0
  78. package/dist/yup/isModulo11/isModulo11YupTest.d.ts +12 -0
  79. package/dist/yup/isModulo11/isModulo11YupTest.js +24 -0
  80. package/dist/yup/isModulo11/isModulo11YupTest.test.d.ts +1 -0
  81. package/dist/yup/isModulo11/isModulo11YupTest.test.js +28 -0
  82. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupTest.d.ts +2 -0
  83. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupTest.js +23 -0
  84. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupTest.test.d.ts +1 -0
  85. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupTest.test.js +32 -0
  86. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupWhen.d.ts +13 -0
  87. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupWhen.js +13 -0
  88. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupWhen.test.d.ts +1 -0
  89. package/dist/yup/isNumberSameOrGreater/isNumberSameOrGreaterYupWhen.test.js +28 -0
  90. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupTest.d.ts +2 -0
  91. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupTest.js +23 -0
  92. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupTest.test.d.ts +1 -0
  93. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupTest.test.js +32 -0
  94. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupWhen.d.ts +13 -0
  95. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupWhen.js +13 -0
  96. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupWhen.test.d.ts +1 -0
  97. package/dist/yup/isNumberSameOrLower/isNumberSameOrLowerYupWhen.test.js +28 -0
  98. package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.d.ts +14 -0
  99. package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.js +24 -0
  100. package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.test.d.ts +1 -0
  101. package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.test.js +24 -0
  102. package/dist/yup/minDigitsLength/minDigitsLengthYupTest.d.ts +14 -0
  103. package/dist/yup/minDigitsLength/minDigitsLengthYupTest.js +24 -0
  104. package/dist/yup/minDigitsLength/minDigitsLengthYupTest.test.d.ts +1 -0
  105. package/dist/yup/minDigitsLength/minDigitsLengthYupTest.test.js +24 -0
  106. package/package.json +18 -16
package/README.md CHANGED
@@ -76,6 +76,9 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
76
76
  #### useToggle
77
77
  - **`useToggle(defaultState?: boolean): readonly [boolean, () => void, { readonly open: () => void; readonly close: () => void; }, React.Dispatch<React.SetStateAction<boolean>>]`** - Returns boolean state, a toggle function, helper actions (`open`/`close`), and the underlying state setter.
78
78
 
79
+ ### Types
80
+ - **`Prettify<T>`** - Prettify type
81
+
79
82
  ### Utils
80
83
 
81
84
  #### Array Operations
@@ -141,6 +144,7 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
141
144
  - **`isArrayBuffer(value: unknown): value is ArrayBuffer`** - Checks if a value is an ArrayBuffer.
142
145
  - **`isBlob(value: unknown): value is Blob`** - Checks if a value is a Blob.
143
146
  - **`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
144
148
  - **`isFunction(value: unknown): value is Function`** - Checks if a value is a function.
145
149
  - **`isNull(value: unknown): value is null`** - Checks if a value is null.
146
150
  - **`isNullOrUndefined(value: unknown): value is null | undefined`** - Checks if a value is null or undefined.
@@ -179,6 +183,21 @@ import { isEmail, removeWhitespaces, useToggle } from '@mmb-digital/shared-libs-
179
183
  - **`isValidPassport(value: string | null | undefined): boolean`** - Validates Czech passport number format.
180
184
  - **`isValidVehicleIdentification(value: string | null | undefined): boolean`** - Validates vehicle identification number (VIN) format.
181
185
 
186
+ #### Yup Validators
187
+ - **`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.
190
+ - **`isDateYupTest(message: string)`** - Creates Yup `test` config for valid date validation.
191
+ - **`isDICYupTest(message: string)`** - Creates Yup `test` config for DIČ validation.
192
+ - **`isEmailYupTest(message: string)`** - Creates Yup `test` config for email validation.
193
+ - **`isModulo11YupTest(message: string)`** - Creates Yup `test` config for modulo 11 validation.
194
+ - **`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.
199
+
200
+
182
201
  ## License
183
202
 
184
203
  UNLICENSED
@@ -7,8 +7,14 @@ import { EMPTY_VALUE_PLACEHOLDER } from '../const.js';
7
7
  export const formatBytes = (bytes, decimals = 2, fallback = EMPTY_VALUE_PLACEHOLDER) => {
8
8
  if (isNullOrUndefined(bytes))
9
9
  return fallback;
10
+ if (bytes < 0)
11
+ return fallback;
12
+ if (bytes === Number.POSITIVE_INFINITY)
13
+ return fallback;
10
14
  if (bytes === 0)
11
15
  return '0 Bytes';
16
+ if (decimals < 0)
17
+ decimals = 0;
12
18
  const k = 1024;
13
19
  const dm = Math.max(0, decimals);
14
20
  const sizes = ['Bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
@@ -28,4 +28,13 @@ describe('formatBytes', () => {
28
28
  expect(formatBytes(null)).toBe(EMPTY_VALUE_PLACEHOLDER);
29
29
  expect(formatBytes(undefined)).toBe(EMPTY_VALUE_PLACEHOLDER);
30
30
  });
31
+ it('should return fallback for negative bytes', () => {
32
+ expect(formatBytes(-1)).toBe(EMPTY_VALUE_PLACEHOLDER);
33
+ });
34
+ it('should return fallback for positive infinity', () => {
35
+ expect(formatBytes(Number.POSITIVE_INFINITY)).toBe(EMPTY_VALUE_PLACEHOLDER);
36
+ });
37
+ it('should return fallback for negative infinity', () => {
38
+ expect(formatBytes(Number.NEGATIVE_INFINITY)).toBe(EMPTY_VALUE_PLACEHOLDER);
39
+ });
31
40
  });
@@ -1,13 +1,7 @@
1
- import { isEmpty } from '../../validators/isEmpty/isEmpty.js';
2
1
  import { EMPTY_VALUE_PLACEHOLDER } from '../const.js';
3
2
  import { innerFormatCZSKPhoneNumber } from './utils/innerFormatCZSKPhoneNumber.js';
4
3
  /**
5
4
  * Formats a Czech or Slovak phone number.
6
5
  * @example "+420 123 456 789" or "+421 123 456 789"
7
6
  */
8
- export const formatCZSKPhoneNumber = (value, fallback = EMPTY_VALUE_PLACEHOLDER) => {
9
- const formattedPhoneNumber = innerFormatCZSKPhoneNumber(value, fallback);
10
- if (isEmpty(formattedPhoneNumber))
11
- return fallback;
12
- return formattedPhoneNumber;
13
- };
7
+ export const formatCZSKPhoneNumber = (value, fallback = EMPTY_VALUE_PLACEHOLDER) => innerFormatCZSKPhoneNumber(value, fallback) ?? fallback;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './formatters/index.js';
2
2
  export * from './hooks/index.js';
3
+ export * from './types/index.js';
3
4
  export * from './utils/index.js';
4
5
  export * from './validators/index.js';
6
+ export * from './yup/index.js';
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './formatters/index.js';
2
2
  export * from './hooks/index.js';
3
+ export * from './types/index.js';
3
4
  export * from './utils/index.js';
4
5
  export * from './validators/index.js';
6
+ export * from './yup/index.js';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Prettify is a utility type that takes a type T and returns a new type that is a "prettified" version of T.
3
+ *
4
+ * Transforms a type like:
5
+ * type A = { a: number } & { b: string } & { c: boolean }
6
+ *
7
+ * Into a more readable type:
8
+ * type A = {
9
+ * a: number;
10
+ * b: string;
11
+ * c: boolean;
12
+ * }
13
+ *
14
+ * More info at:
15
+ * https://www.totaltypescript.com/concepts/the-prettify-helper
16
+ */
17
+ export type Prettify<T> = {
18
+ [K in keyof T]: T[K];
19
+ } & {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export type { Prettify } from './Prettify/Prettify.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -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 window.btoa(binary);
9
+ return globalThis.window.btoa(binary);
10
10
  };
@@ -2,7 +2,7 @@
2
2
  * Converts a base64 string to an ArrayBuffer.
3
3
  */
4
4
  export const base64ToArrayBuffer = (base64) => {
5
- const binaryString = window.atob(base64);
5
+ const binaryString = globalThis.window.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));
@@ -2,4 +2,4 @@
2
2
  * Chunks an array into smaller arrays of a specified size.
3
3
  * Replace for _.chunk
4
4
  */
5
- export declare const chunkArray: <T>(input: unknown, size: number) => T[];
5
+ export declare const chunkArray: <T>(input: unknown, size: number) => T[][];
@@ -1,12 +1,12 @@
1
1
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
2
  import { downloadFile } from './downloadFile.js';
3
3
  function setupDomMocks() {
4
- vi.spyOn(window, 'atob');
5
- vi.spyOn(window.URL, 'createObjectURL');
6
- vi.spyOn(window.URL, 'revokeObjectURL');
7
- vi.spyOn(document, 'createElement');
8
- vi.spyOn(document.body, 'appendChild');
9
- vi.spyOn(document.body, 'removeChild');
4
+ vi.spyOn(globalThis.window, 'atob');
5
+ vi.spyOn(globalThis.window.URL, 'createObjectURL');
6
+ vi.spyOn(globalThis.window.URL, 'revokeObjectURL');
7
+ vi.spyOn(globalThis.document, 'createElement');
8
+ vi.spyOn(globalThis.document.body, 'appendChild');
9
+ vi.spyOn(globalThis.document.body, 'removeChild');
10
10
  }
11
11
  describe('downloadFile', () => {
12
12
  beforeEach(() => {
@@ -17,41 +17,41 @@ describe('downloadFile', () => {
17
17
  });
18
18
  it('does nothing if file is null', () => {
19
19
  downloadFile(null, 'file.txt');
20
- expect(document.body.appendChild).not.toHaveBeenCalled();
20
+ expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
21
21
  });
22
22
  it('does nothing if file is undefined', () => {
23
23
  downloadFile(undefined, 'file.txt');
24
- expect(document.body.appendChild).not.toHaveBeenCalled();
24
+ expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
25
25
  });
26
26
  it('does nothing if file is empty string', () => {
27
27
  downloadFile('', 'file.txt');
28
- expect(document.body.appendChild).not.toHaveBeenCalled();
28
+ expect(globalThis.document.body.appendChild).not.toHaveBeenCalled();
29
29
  });
30
30
  it('downloads file from Blob', () => {
31
31
  const blob = new Blob(['test']);
32
32
  downloadFile(blob, 'file.txt');
33
- expect(document.body.appendChild).toHaveBeenCalled();
34
- expect(document.body.appendChild).toHaveBeenCalled();
35
- expect(document.body.removeChild).toHaveBeenCalled();
36
- expect(window.URL.createObjectURL).toHaveBeenCalledWith(blob);
37
- expect(window.URL.revokeObjectURL).toHaveBeenCalled();
33
+ expect(globalThis.document.body.appendChild).toHaveBeenCalled();
34
+ expect(globalThis.document.body.appendChild).toHaveBeenCalled();
35
+ expect(globalThis.document.body.removeChild).toHaveBeenCalled();
36
+ expect(globalThis.window.URL.createObjectURL).toHaveBeenCalledWith(blob);
37
+ expect(globalThis.window.URL.revokeObjectURL).toHaveBeenCalled();
38
38
  });
39
39
  it('downloads file from base64 string', () => {
40
40
  downloadFile('dGVzdA==', 'file.txt', 'text/plain');
41
- expect(document.body.appendChild).toHaveBeenCalled();
42
- expect(document.body.appendChild).toHaveBeenCalled();
43
- expect(document.body.removeChild).toHaveBeenCalled();
44
- expect(window.URL.createObjectURL).toHaveBeenCalled();
45
- expect(window.URL.revokeObjectURL).toHaveBeenCalled();
46
- expect(window.atob).toHaveBeenCalledWith('dGVzdA==');
41
+ expect(globalThis.document.body.appendChild).toHaveBeenCalled();
42
+ expect(globalThis.document.body.appendChild).toHaveBeenCalled();
43
+ expect(globalThis.document.body.removeChild).toHaveBeenCalled();
44
+ expect(globalThis.window.URL.createObjectURL).toHaveBeenCalled();
45
+ expect(globalThis.window.URL.revokeObjectURL).toHaveBeenCalled();
46
+ expect(globalThis.window.atob).toHaveBeenCalledWith('dGVzdA==');
47
47
  });
48
48
  it('downloads file from ArrayBuffer', () => {
49
49
  const buffer = new ArrayBuffer(4);
50
50
  downloadFile(buffer, 'file.txt', 'text/plain');
51
- expect(document.body.appendChild).toHaveBeenCalled();
52
- expect(document.body.appendChild).toHaveBeenCalled();
53
- expect(document.body.removeChild).toHaveBeenCalled();
54
- expect(window.URL.createObjectURL).toHaveBeenCalled();
55
- expect(window.URL.revokeObjectURL).toHaveBeenCalled();
51
+ expect(globalThis.document.body.appendChild).toHaveBeenCalled();
52
+ expect(globalThis.document.body.appendChild).toHaveBeenCalled();
53
+ expect(globalThis.document.body.removeChild).toHaveBeenCalled();
54
+ expect(globalThis.window.URL.createObjectURL).toHaveBeenCalled();
55
+ expect(globalThis.window.URL.revokeObjectURL).toHaveBeenCalled();
56
56
  });
57
57
  });
@@ -2,16 +2,16 @@
2
2
  * Process the download of a Blob file.
3
3
  */
4
4
  export const processDownload = (blob, filename) => {
5
- const blobURL = window.URL.createObjectURL(blob);
6
- const tempLink = document.createElement('a');
5
+ const blobURL = globalThis.window.URL.createObjectURL(blob);
6
+ const tempLink = globalThis.document.createElement('a');
7
7
  tempLink.style.display = 'none';
8
8
  tempLink.href = blobURL;
9
9
  tempLink.setAttribute('download', filename);
10
10
  if (typeof tempLink.download === 'undefined') {
11
11
  tempLink.setAttribute('target', '_blank');
12
12
  }
13
- document?.body?.appendChild(tempLink);
13
+ globalThis.document?.body?.appendChild(tempLink);
14
14
  tempLink.click();
15
- document?.body?.removeChild(tempLink);
16
- window.URL.revokeObjectURL(blobURL);
15
+ globalThis.document?.body?.removeChild(tempLink);
16
+ globalThis.window.URL.revokeObjectURL(blobURL);
17
17
  };
@@ -1,4 +1,4 @@
1
1
  /**
2
- * Joins an array of strings using a specified separator, filtering out any undefined values.
2
+ * Joins an array of strings using a specified separator, filtering out any empty values.
3
3
  */
4
4
  export declare const joinBy: (separator: string, stringArray: Array<string | undefined>) => string;
@@ -1,4 +1,4 @@
1
1
  /**
2
- * Joins an array of strings using a specified separator, filtering out any undefined values.
2
+ * Joins an array of strings using a specified separator, filtering out any empty values.
3
3
  */
4
4
  export const joinBy = (separator, stringArray) => [...stringArray].filter(text => !!text).join(separator);
@@ -4,7 +4,7 @@
4
4
  */
5
5
  export const mapObjectKeys = (obj, iteratee) => {
6
6
  const result = {};
7
- for (const key in obj) {
7
+ for (const key of Object.keys(obj)) {
8
8
  result[key] = iteratee(obj[key], key);
9
9
  }
10
10
  return result;
@@ -41,4 +41,11 @@ describe('mapObjectKeys', () => {
41
41
  const result = mapObjectKeys(obj, v => v.age);
42
42
  expect(result).toEqual({ fred: 40, pebbles: 1 });
43
43
  });
44
+ test('does not map enumerable properties from prototype chain', () => {
45
+ const proto = { inherited: 1 };
46
+ const obj = Object.create(proto);
47
+ obj.own = 2;
48
+ const result = mapObjectKeys(obj, value => value && value * 2);
49
+ expect(result).toEqual({ own: 4 });
50
+ });
44
51
  });
@@ -6,13 +6,12 @@ const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
6
6
  export const mergeDeep = (target, source) => {
7
7
  if (!isObject(source))
8
8
  return target;
9
- const sourceRecord = source;
10
9
  const targetClone = { ...target };
11
- Object.keys(sourceRecord).forEach(key => {
10
+ Object.keys(source).forEach(key => {
12
11
  if (DANGEROUS_KEYS.has(key))
13
12
  return;
14
13
  const targetValue = target[key];
15
- const sourceValue = sourceRecord[key];
14
+ const sourceValue = source[key];
16
15
  if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
17
16
  targetClone[key] = targetValue.concat(sourceValue);
18
17
  }
@@ -4,7 +4,7 @@ describe('openFile', () => {
4
4
  const mockBlobUrl = 'blob:http://localhost/fake';
5
5
  beforeEach(() => {
6
6
  vi.spyOn(URL, 'createObjectURL').mockReturnValue(mockBlobUrl);
7
- vi.spyOn(window, 'open').mockImplementation(() => null);
7
+ vi.spyOn(globalThis.window, 'open').mockImplementation(() => null);
8
8
  });
9
9
  afterEach(() => {
10
10
  vi.restoreAllMocks();
@@ -13,32 +13,32 @@ describe('openFile', () => {
13
13
  const blob = new Blob(['test content'], { type: 'text/plain' });
14
14
  openFile(blob);
15
15
  expect(URL.createObjectURL).toHaveBeenCalledWith(blob);
16
- expect(window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
16
+ expect(globalThis.window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
17
17
  });
18
- it('should open Blob in a new window when newWindow is true', () => {
18
+ it('should open Blob in a new globalThis.window when newWindow is true', () => {
19
19
  const blob = new Blob(['test content'], { type: 'text/plain' });
20
20
  openFile(blob, undefined, true);
21
21
  expect(URL.createObjectURL).toHaveBeenCalledWith(blob);
22
- expect(window.open).toHaveBeenCalledWith(mockBlobUrl, '', 'height=650,width=840');
22
+ expect(globalThis.window.open).toHaveBeenCalledWith(mockBlobUrl, '', 'height=650,width=840');
23
23
  });
24
24
  it('should open base64 string in a new tab', () => {
25
25
  const base64 = 'ZmFrZQ=='; // "fake"
26
26
  const arrayBuffer = new Uint8Array([102, 97, 107, 101]).buffer;
27
27
  openFile(base64);
28
28
  expect(URL.createObjectURL).toHaveBeenCalledWith(new Blob([arrayBuffer]));
29
- expect(window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
29
+ expect(globalThis.window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
30
30
  });
31
31
  it('should open ArrayBuffer in a new tab', () => {
32
32
  const arrayBuffer = new Uint8Array([102, 97, 107, 101]).buffer;
33
33
  openFile(arrayBuffer);
34
34
  expect(URL.createObjectURL).toHaveBeenCalledWith(new Blob([arrayBuffer]));
35
- expect(window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
35
+ expect(globalThis.window.open).toHaveBeenCalledWith(mockBlobUrl, '_blank');
36
36
  });
37
37
  it('should do nothing for null, undefined, or empty string', () => {
38
38
  openFile(null);
39
39
  openFile(undefined);
40
40
  openFile('');
41
41
  expect(URL.createObjectURL).not.toHaveBeenCalled();
42
- expect(window.open).not.toHaveBeenCalled();
42
+ expect(globalThis.window.open).not.toHaveBeenCalled();
43
43
  });
44
44
  });
@@ -6,10 +6,10 @@ export const innerOpenFile = (blob, newWindow) => {
6
6
  if (newWindow) {
7
7
  // This is a workaround to open the blob in a new window
8
8
  // Without specify height and width, the new window open in a new tab
9
- window.open(blobUrl, '', 'height=650,width=840');
9
+ globalThis.window.open(blobUrl, '', 'height=650,width=840');
10
10
  }
11
11
  else {
12
12
  // This will open the blob in new tab or window depending on the browser settings
13
- window.open(blobUrl, '_blank');
13
+ globalThis.window.open(blobUrl, '_blank');
14
14
  }
15
15
  };
@@ -3,10 +3,10 @@ import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUn
3
3
  * Scrolls the window to the bottom smoothly.
4
4
  */
5
5
  export const scrollToBottom = () => {
6
- if (isNullOrUndefined(window) || isNullOrUndefined(document))
6
+ if (isNullOrUndefined(globalThis.window) || isNullOrUndefined(globalThis.document))
7
7
  return;
8
- window.scrollTo({
9
- top: document.body.scrollHeight,
8
+ globalThis.window.scrollTo({
9
+ top: globalThis.document.body.scrollHeight,
10
10
  behavior: 'smooth'
11
11
  });
12
12
  };
@@ -1,11 +1,11 @@
1
1
  import { describe, expect, test, vi } from 'vitest';
2
2
  import { scrollToBottom } from './scrollToBottom.js';
3
3
  describe('scrollToBottom', () => {
4
- test('should call window.scrollTo with top: 0 and behavior: smooth', () => {
5
- vi.spyOn(window, 'scrollTo');
4
+ test('should call globalThis.window.scrollTo with top: 0 and behavior: smooth', () => {
5
+ vi.spyOn(globalThis.window, 'scrollTo');
6
6
  scrollToBottom();
7
- expect(window.scrollTo).toHaveBeenCalledWith({
8
- top: document.body.scrollHeight,
7
+ expect(globalThis.window.scrollTo).toHaveBeenCalledWith({
8
+ top: globalThis.document.body.scrollHeight,
9
9
  behavior: 'smooth'
10
10
  });
11
11
  });
@@ -3,7 +3,7 @@ import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUn
3
3
  * Scrolls the window to the top smoothly.
4
4
  */
5
5
  export const scrollToTop = () => {
6
- if (isNullOrUndefined(window))
6
+ if (isNullOrUndefined(globalThis.window))
7
7
  return;
8
- window.scrollTo({ top: 0, behavior: 'smooth' });
8
+ globalThis.window.scrollTo({ top: 0, behavior: 'smooth' });
9
9
  };
@@ -1,9 +1,9 @@
1
1
  import { describe, expect, test, vi } from 'vitest';
2
2
  import { scrollToTop } from './scrollToTop.js';
3
3
  describe('scrollToTop', () => {
4
- test('should call window.scrollTo with top: 0 and behavior: smooth', () => {
5
- vi.spyOn(window, 'scrollTo');
4
+ test('should call globalThis.window.scrollTo with top: 0 and behavior: smooth', () => {
5
+ vi.spyOn(globalThis.window, 'scrollTo');
6
6
  scrollToTop();
7
- expect(window.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' });
7
+ expect(globalThis.window.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' });
8
8
  });
9
9
  });
@@ -1,6 +1,42 @@
1
+ import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
1
2
  import { isObject } from '../../validators/isObject/isObject.js';
2
- import { mergeDeep } from '../mergeDeep/mergeDeep.js';
3
- const PATH_DELIMITER_REGEX = /[.]+/;
3
+ import { isString } from '../../validators/isString/isString.js';
4
+ const PATH_TOKEN_REGEX = /([^.[\]]+)|\[(\d+)\]/g;
5
+ const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
6
+ const parsePath = (path) => {
7
+ const tokens = [];
8
+ for (const match of path.matchAll(PATH_TOKEN_REGEX)) {
9
+ const propertyKey = match[1];
10
+ const arrayIndex = match[2];
11
+ if (isString(propertyKey)) {
12
+ tokens.push(propertyKey);
13
+ continue;
14
+ }
15
+ if (isString(arrayIndex)) {
16
+ tokens.push(Number(arrayIndex));
17
+ }
18
+ }
19
+ return tokens;
20
+ };
21
+ const setAtPath = (target, keys, value, depth = 0) => {
22
+ if (depth >= keys.length)
23
+ return value;
24
+ const key = keys[depth];
25
+ if (isNullOrUndefined(key))
26
+ return target;
27
+ if (isString(key)) {
28
+ if (DANGEROUS_KEYS.has(key))
29
+ return target;
30
+ const currentObject = isObject(target) ? target : {};
31
+ return {
32
+ ...currentObject,
33
+ [key]: setAtPath(currentObject[key], keys, value, depth + 1)
34
+ };
35
+ }
36
+ const currentArray = Array.isArray(target) ? target.slice() : [];
37
+ currentArray[key] = setAtPath(currentArray[key], keys, value, depth + 1);
38
+ return currentArray;
39
+ };
4
40
  /**
5
41
  * Sets the value of a nested object property by path.
6
42
  * Replace for _.set
@@ -8,11 +44,8 @@ const PATH_DELIMITER_REGEX = /[.]+/;
8
44
  export const setObjectValue = (obj, path, value) => {
9
45
  if (path.length === 0)
10
46
  return obj;
11
- const keys = path.split(PATH_DELIMITER_REGEX).filter(Boolean);
47
+ const keys = parsePath(path);
12
48
  if (keys.length === 0)
13
49
  return obj;
14
- const current = keys.reduceRight((acc, key) => ({ [key]: acc }), value);
15
- if (!isObject(current))
16
- return obj; // This is only to satisfy TypeScript, value should always be object here
17
- return mergeDeep(obj, current);
50
+ return setAtPath(obj, keys, value);
18
51
  };
@@ -46,4 +46,30 @@ describe('setObjectValue', () => {
46
46
  const result = setObjectValue(obj, 'foo.bar', 1);
47
47
  expect(result).toEqual({ foo: { bar: 1 } });
48
48
  });
49
+ test('handles bracket notation array path', () => {
50
+ const obj = {};
51
+ const result = setObjectValue(obj, 'conditions[0].financedAmountUpTo', 10);
52
+ expect(result).toEqual({
53
+ conditions: [{ financedAmountUpTo: 10 }]
54
+ });
55
+ });
56
+ test('updates existing item in array path', () => {
57
+ const obj = { conditions: [{ financedAmountFrom: 1 }] };
58
+ const result = setObjectValue(obj, 'conditions[0].financedAmountUpTo', 10);
59
+ expect(result).toEqual({
60
+ conditions: [{ financedAmountFrom: 1, financedAmountUpTo: 10 }]
61
+ });
62
+ });
63
+ test('updates existing item in array of array path', () => {
64
+ const obj = { conditions: [{ financedAmountFrom: 1 }] };
65
+ const result = setObjectValue(obj, 'conditions[0].obj.conditions[0].financedAmountUpTo', 10);
66
+ expect(result).toEqual({
67
+ conditions: [{ financedAmountFrom: 1, obj: { conditions: [{ financedAmountUpTo: 10 }] } }]
68
+ });
69
+ });
70
+ test.each(['__proto__', 'constructor', 'prototype'])('blocks dangerous key "%s"', dangerousKey => {
71
+ const obj = { a: 1 };
72
+ const result = setObjectValue(obj, dangerousKey, 'evil');
73
+ expect(result).toBe(obj);
74
+ });
49
75
  });
@@ -7,6 +7,7 @@ export { isEmail } from './isEmail/isEmail.js';
7
7
  export { isEmpty } from './isEmpty/isEmpty.js';
8
8
  export { isEmptyString } from './isEmptyString/isEmptyString.js';
9
9
  export { isEqual } from './isEqual/isEqual.js';
10
+ export { isFile } from './isFile/isFile.js';
10
11
  export { isFunction } from './isFunction/isFunction.js';
11
12
  export { isICO } from './isICO/isICO.js';
12
13
  export { isModulo11 } from './isModulo11/isModulo11.js';
@@ -7,6 +7,7 @@ export { isEmail } from './isEmail/isEmail.js';
7
7
  export { isEmpty } from './isEmpty/isEmpty.js';
8
8
  export { isEmptyString } from './isEmptyString/isEmptyString.js';
9
9
  export { isEqual } from './isEqual/isEqual.js';
10
+ export { isFile } from './isFile/isFile.js';
10
11
  export { isFunction } from './isFunction/isFunction.js';
11
12
  export { isICO } from './isICO/isICO.js';
12
13
  export { isModulo11 } from './isModulo11/isModulo11.js';
@@ -1,4 +1,11 @@
1
1
  /**
2
2
  * Checks if the given value is empty.
3
+ *
4
+ * Empty values include:
5
+ * - `undefined` or `null`
6
+ * - An empty string (`''`)
7
+ * - The number `0`
8
+ * - An empty array (`[]`) or an array where all elements are empty
9
+ * - An empty object (`{}`)
3
10
  */
4
11
  export declare const isEmpty: <T>(value: T | undefined | null) => value is undefined | null;
@@ -5,6 +5,13 @@ import { isObject } from '../isObject/isObject.js';
5
5
  import { isString } from '../isString/isString.js';
6
6
  /**
7
7
  * Checks if the given value is empty.
8
+ *
9
+ * Empty values include:
10
+ * - `undefined` or `null`
11
+ * - An empty string (`''`)
12
+ * - The number `0`
13
+ * - An empty array (`[]`) or an array where all elements are empty
14
+ * - An empty object (`{}`)
8
15
  */
9
16
  export const isEmpty = (value) => {
10
17
  if (isNullOrUndefined(value))
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Checks if the given value is a File.
3
+ */
4
+ export declare const isFile: (value: unknown) => value is File;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Checks if the given value is a File.
3
+ */
4
+ export const isFile = (value) => value instanceof File;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { isFile } from './isFile.js';
3
+ describe('isFile', () => {
4
+ test('should return true for a real File', () => {
5
+ const file = new File(['hello'], 'hello.txt', { type: 'text/plain' });
6
+ expect(isFile(file)).toBe(true);
7
+ });
8
+ test('should return false for Blob', () => {
9
+ const blob = new Blob(['hello'], { type: 'text/plain' });
10
+ expect(isFile(blob)).toBe(false);
11
+ });
12
+ test('should return false for strings', () => {
13
+ expect(isFile('file')).toBe(false);
14
+ expect(isFile('')).toBe(false);
15
+ });
16
+ test('should return false for numbers', () => {
17
+ expect(isFile(123)).toBe(false);
18
+ expect(isFile(0)).toBe(false);
19
+ expect(isFile(Number.NaN)).toBe(false);
20
+ });
21
+ test('should return false for booleans', () => {
22
+ expect(isFile(true)).toBe(false);
23
+ expect(isFile(false)).toBe(false);
24
+ });
25
+ test('should return false for arrays', () => {
26
+ expect(isFile([])).toBe(false);
27
+ expect(isFile([1, 2, 3])).toBe(false);
28
+ });
29
+ test('should return false for plain objects', () => {
30
+ expect(isFile({})).toBe(false);
31
+ expect(isFile({ name: 'hello.txt', size: 5, type: 'text/plain' })).toBe(false);
32
+ });
33
+ test('should return false for null and undefined', () => {
34
+ expect(isFile(null)).toBe(false);
35
+ expect(isFile(undefined)).toBe(false);
36
+ });
37
+ });