@mmb-digital/shared-libs-auto 0.0.22 → 0.0.23
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 +25 -14
- package/dist/formatters/formatBankAccount/utils/innerFormatBankAccount.js +1 -1
- package/dist/formatters/formatBytes/formatBytes.js +3 -6
- package/dist/formatters/formatBytes/formatBytes.test.js +7 -0
- package/dist/formatters/formatNumber/formatNumber.test.js +4 -0
- package/dist/formatters/formatNumber/utils/innerFormatNumber.js +16 -2
- package/dist/utils/arrayBufferToBase64/arrayBufferToBase64.js +1 -1
- package/dist/utils/base64ToArrayBuffer/base64ToArrayBuffer.js +1 -1
- package/dist/utils/chunkArray/chunkArray.js +6 -4
- package/dist/utils/chunkArray/chunkArray.test.js +5 -0
- package/dist/utils/cleanObject/cleanObject.d.ts +3 -2
- package/dist/utils/cleanObject/cleanObject.js +3 -16
- package/dist/utils/cleanObject/cleanObject.test.js +47 -9
- package/dist/utils/cleanObject/types.d.ts +6 -0
- package/dist/utils/cleanObject/types.js +1 -0
- package/dist/utils/cleanObject/utils/innerCleanObject.d.ts +6 -0
- package/dist/utils/cleanObject/utils/innerCleanObject.js +37 -0
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.js +1 -0
- package/dist/utils/getDateObject/getDateObject.d.ts +8 -1
- package/dist/utils/getDateObject/getDateObject.js +10 -2
- package/dist/utils/getDateObject/getDateObject.test.js +24 -3
- package/dist/utils/getDateObject/utils/parseDate.d.ts +6 -0
- package/dist/utils/getDateObject/utils/parseDate.js +21 -0
- package/dist/utils/getLastSpecificDate/getLastSpecificDate.d.ts +22 -0
- package/dist/utils/getLastSpecificDate/getLastSpecificDate.js +42 -0
- package/dist/utils/getLastSpecificDate/getLastSpecificDate.test.d.ts +1 -0
- package/dist/utils/getLastSpecificDate/getLastSpecificDate.test.js +108 -0
- package/dist/utils/getLastSpecificDate/utils/getSpecificDate.d.ts +4 -0
- package/dist/utils/getLastSpecificDate/utils/getSpecificDate.js +9 -0
- package/dist/utils/getNumber/getNumber.test.js +26 -26
- package/dist/utils/getNumberFloat/getNumberFloat.js +1 -1
- package/dist/utils/getNumberFloat/getNumberFloat.test.js +17 -17
- package/dist/utils/getObjectDiff/getObjectDiff.js +1 -1
- package/dist/utils/getObjectDiff/getObjectDiff.test.js +9 -0
- package/dist/utils/getObjectValue/getObjectValue.js +2 -1
- package/dist/utils/getObjectValue/getObjectValue.test.js +11 -8
- package/dist/utils/getQuarterFromDate/getQuarterFromDate.d.ts +4 -0
- package/dist/utils/getQuarterFromDate/getQuarterFromDate.js +26 -0
- package/dist/utils/getQuarterFromDate/getQuarterFromDate.test.d.ts +1 -0
- package/dist/utils/getQuarterFromDate/getQuarterFromDate.test.js +151 -0
- package/dist/utils/getWeekFromDate/getWeekFromDate.d.ts +4 -0
- package/dist/utils/getWeekFromDate/getWeekFromDate.js +28 -0
- package/dist/utils/getWeekFromDate/getWeekFromDate.test.d.ts +1 -0
- package/dist/utils/getWeekFromDate/getWeekFromDate.test.js +130 -0
- package/dist/utils/groupBy/groupBy.js +10 -3
- package/dist/utils/groupBy/groupBy.test.js +6 -0
- package/dist/utils/hasObjectProperty/hasObjectProperty.js +5 -4
- package/dist/utils/hasObjectProperty/hasObjectProperty.test.js +5 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/mapObjectKeys/mapObjectKeys.js +6 -1
- package/dist/utils/mapObjectKeys/mapObjectKeys.test.js +7 -0
- package/dist/utils/mergeDeep/mergeDeep.js +2 -23
- package/dist/utils/mergeDeep/mergeDeep.test.js +5 -0
- package/dist/utils/mergeDeep/utils/innerMergeDeep.d.ts +5 -0
- package/dist/utils/mergeDeep/utils/innerMergeDeep.js +31 -0
- package/dist/utils/range/createRange.js +2 -0
- package/dist/utils/range/createRange.test.js +9 -5
- package/dist/utils/range/isInRange.js +2 -0
- package/dist/utils/range/isInRange.test.js +5 -0
- package/dist/utils/range/unionRange.test.js +2 -2
- package/dist/utils/removeSpecialChars/removeSpecialChars.test.js +9 -0
- package/dist/utils/removeWhitespacesFromUnknown/removeWhitespacesFromUnknown.test.js +2 -2
- package/dist/utils/setObjectValue/setObjectValue.js +1 -1
- package/dist/validators/index.d.ts +3 -0
- package/dist/validators/index.js +3 -0
- package/dist/validators/isBankAccount/isBankAccount.d.ts +3 -1
- package/dist/validators/isBankAccount/isBankAccount.js +11 -21
- package/dist/validators/isBankAccount/isBankAccount.test.js +6 -3
- package/dist/validators/isEmail/isEmail.js +5 -1
- package/dist/validators/isEmail/isEmail.test.js +15 -0
- package/dist/validators/isEqual/isEqual.d.ts +1 -1
- package/dist/validators/isEqual/isEqual.js +7 -3
- package/dist/validators/isEqual/isEqual.test.js +12 -0
- package/dist/validators/isModulo11/isModulo11.js +2 -0
- package/dist/validators/isModulo11/isModulo11.test.js +3 -0
- package/dist/validators/isPlainObject/isPlainObject.d.ts +4 -0
- package/dist/validators/isPlainObject/isPlainObject.js +9 -0
- package/dist/validators/isPlainObject/isPlainObject.test.d.ts +1 -0
- package/dist/validators/isPlainObject/isPlainObject.test.js +24 -0
- package/dist/validators/isValidDate/isValidDate.d.ts +1 -0
- package/dist/validators/isValidDate/isValidDate.js +4 -26
- package/dist/validators/isValidDate/isValidDate.test.js +12 -1
- package/dist/validators/isValidDateInstance/isValidDateInstance.d.ts +4 -0
- package/dist/validators/isValidDateInstance/isValidDateInstance.js +11 -0
- package/dist/validators/isValidDateInstance/isValidDateInstance.test.d.ts +1 -0
- package/dist/validators/isValidDateInstance/isValidDateInstance.test.js +67 -0
- package/dist/validators/isValidForeignPhoneNumber/isValidForeignPhoneNumber.d.ts +1 -1
- package/dist/validators/isValidForeignPhoneNumber/isValidForeignPhoneNumber.js +2 -2
- package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.d.ts +6 -0
- package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.js +27 -0
- package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.test.d.ts +1 -0
- package/dist/validators/isValidMrzCheckDigit/isValidMrzCheckDigit.test.js +26 -0
- package/dist/yup/index.d.ts +1 -0
- package/dist/yup/index.js +1 -0
- package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.d.ts +12 -0
- package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.js +32 -0
- package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.test.d.ts +1 -0
- package/dist/yup/isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.test.js +64 -0
- package/dist/yup/isDateSameOrAfter/isDateSameOrAfterYupTest.js +12 -4
- package/dist/yup/isDateSameOrBefore/isDateSameOrBeforeYupTest.js +12 -4
- package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.js +2 -0
- package/dist/yup/maxDigitsLength/maxDigitsLengthYupTest.test.js +6 -0
- package/dist/yup/minDigitsLength/minDigitsLengthYupTest.js +2 -0
- package/dist/yup/minDigitsLength/minDigitsLengthYupTest.test.js +6 -0
- package/package.json +18 -18
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { getWeekFromDate } from './getWeekFromDate.js';
|
|
3
|
+
describe('Date object input', () => {
|
|
4
|
+
describe('Začátek roku (1. 1. 2024 = pondělí)', () => {
|
|
5
|
+
it('Should return week 1 for January 1st', () => {
|
|
6
|
+
expect(getWeekFromDate(new Date(2024, 0, 1))).toStrictEqual(1);
|
|
7
|
+
});
|
|
8
|
+
it('Should return week 2 for January 8th', () => {
|
|
9
|
+
expect(getWeekFromDate(new Date(2024, 0, 8))).toStrictEqual(2);
|
|
10
|
+
});
|
|
11
|
+
it('Should return week 2 for January 7th based on current week-numbering logic', () => {
|
|
12
|
+
expect(getWeekFromDate(new Date(2024, 0, 7))).toStrictEqual(2);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
describe('Střed roku 2024 (přestupný rok)', () => {
|
|
16
|
+
it('Should return week 27 for June 30th', () => {
|
|
17
|
+
expect(getWeekFromDate(new Date(2024, 5, 30))).toStrictEqual(27);
|
|
18
|
+
});
|
|
19
|
+
it('Should return correct week for mid-year dates', () => {
|
|
20
|
+
expect(getWeekFromDate(new Date(2024, 5, 1))).toStrictEqual(22);
|
|
21
|
+
});
|
|
22
|
+
it('Should return correct week for September', () => {
|
|
23
|
+
expect(getWeekFromDate(new Date(2024, 8, 15))).toStrictEqual(38);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe('Konec roku 2024 (přestupný rok)', () => {
|
|
27
|
+
it('Should return week 52 for December 28th', () => {
|
|
28
|
+
expect(getWeekFromDate(new Date(2024, 11, 28))).toStrictEqual(52);
|
|
29
|
+
});
|
|
30
|
+
it('Should return week 53 for December 31st', () => {
|
|
31
|
+
expect(getWeekFromDate(new Date(2024, 11, 31))).toStrictEqual(53);
|
|
32
|
+
});
|
|
33
|
+
it('Should return week 51 for December 20th', () => {
|
|
34
|
+
expect(getWeekFromDate(new Date(2024, 11, 20))).toStrictEqual(51);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe('Rok 2025 (1. 1. = středa)', () => {
|
|
38
|
+
it('Should return week 1 for January 1st 2025', () => {
|
|
39
|
+
expect(getWeekFromDate(new Date(2025, 0, 1))).toStrictEqual(1);
|
|
40
|
+
});
|
|
41
|
+
it('Should return week 2 for January 5th 2025', () => {
|
|
42
|
+
expect(getWeekFromDate(new Date(2025, 0, 5))).toStrictEqual(2);
|
|
43
|
+
});
|
|
44
|
+
it('Should return week 1 for January 4th 2025', () => {
|
|
45
|
+
expect(getWeekFromDate(new Date(2025, 0, 4))).toStrictEqual(1);
|
|
46
|
+
});
|
|
47
|
+
it('Should return week 2 for January 6th 2025', () => {
|
|
48
|
+
expect(getWeekFromDate(new Date(2025, 0, 6))).toStrictEqual(2);
|
|
49
|
+
});
|
|
50
|
+
it('Should return week 53 for December 31st 2025', () => {
|
|
51
|
+
expect(getWeekFromDate(new Date(2025, 11, 31))).toStrictEqual(53);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe('Různé roky', () => {
|
|
55
|
+
it('Should return correct week for year 2023', () => {
|
|
56
|
+
expect(getWeekFromDate(new Date(2023, 0, 1))).toStrictEqual(1);
|
|
57
|
+
});
|
|
58
|
+
it('Should return correct week for year 2026', () => {
|
|
59
|
+
expect(getWeekFromDate(new Date(2026, 0, 1))).toStrictEqual(1);
|
|
60
|
+
});
|
|
61
|
+
it('Should return correct week for leap year transition', () => {
|
|
62
|
+
expect(getWeekFromDate(new Date(2024, 1, 29))).toStrictEqual(9);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('Null or undefined input', () => {
|
|
67
|
+
it('Should return null for undefined input', () => {
|
|
68
|
+
expect(getWeekFromDate(undefined)).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
it('Should return null for null input', () => {
|
|
71
|
+
expect(getWeekFromDate(null)).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
it('Should return null for invalid Date instance', () => {
|
|
74
|
+
expect(getWeekFromDate(new Date('invalid-date'))).toBeNull();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe('Object input', () => {
|
|
78
|
+
it('Should return null for plain object', () => {
|
|
79
|
+
expect(getWeekFromDate({ year: 2024, month: 0, day: 1 })).toBeNull();
|
|
80
|
+
});
|
|
81
|
+
it('Should return null for empty object', () => {
|
|
82
|
+
expect(getWeekFromDate({})).toBeNull();
|
|
83
|
+
});
|
|
84
|
+
it('Should return null for array', () => {
|
|
85
|
+
expect(getWeekFromDate([2024, 0, 1])).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe('String input', () => {
|
|
89
|
+
it('Should work with valid ISO date string', () => {
|
|
90
|
+
const result = getWeekFromDate('2024-01-01');
|
|
91
|
+
expect(result).toStrictEqual(1);
|
|
92
|
+
});
|
|
93
|
+
it('Should work with valid date string for mid-year', () => {
|
|
94
|
+
const result = getWeekFromDate('2024-06-30');
|
|
95
|
+
expect(result).toStrictEqual(27);
|
|
96
|
+
});
|
|
97
|
+
it('Should return null for invalid date string', () => {
|
|
98
|
+
expect(getWeekFromDate('invalid-date')).toBeNull();
|
|
99
|
+
});
|
|
100
|
+
it('Should return null for empty string', () => {
|
|
101
|
+
expect(getWeekFromDate('')).toBeNull();
|
|
102
|
+
});
|
|
103
|
+
it('Should work with various date string formats', () => {
|
|
104
|
+
const result = getWeekFromDate('2024-01-01');
|
|
105
|
+
expect(result).not.toBeNull();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
describe('Edge cases', () => {
|
|
109
|
+
it('Should handle first day of year correctly', () => {
|
|
110
|
+
const result = getWeekFromDate(new Date(2024, 0, 1));
|
|
111
|
+
expect(result).toStrictEqual(1);
|
|
112
|
+
});
|
|
113
|
+
it('Should handle last day of year correctly', () => {
|
|
114
|
+
const result = getWeekFromDate(new Date(2024, 11, 31));
|
|
115
|
+
expect(result).not.toBeNull();
|
|
116
|
+
expect(result).toBeGreaterThanOrEqual(1);
|
|
117
|
+
expect(result).toBeLessThanOrEqual(53);
|
|
118
|
+
});
|
|
119
|
+
it('Should preserve time information correctly', () => {
|
|
120
|
+
const result1 = getWeekFromDate(new Date(2024, 0, 1, 0, 0, 0));
|
|
121
|
+
const result2 = getWeekFromDate(new Date(2024, 0, 1, 23, 59, 59));
|
|
122
|
+
expect(result1).toStrictEqual(result2);
|
|
123
|
+
});
|
|
124
|
+
it('Should work with dates from different centuries', () => {
|
|
125
|
+
const result2000 = getWeekFromDate(new Date(2000, 0, 1));
|
|
126
|
+
const result2100 = getWeekFromDate(new Date(2100, 0, 1));
|
|
127
|
+
expect(result2000).not.toBeNull();
|
|
128
|
+
expect(result2100).not.toBeNull();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -2,9 +2,16 @@
|
|
|
2
2
|
* Groups an array of objects by a key.
|
|
3
3
|
*/
|
|
4
4
|
export const groupBy = (arr, getKey) => {
|
|
5
|
-
const grouped =
|
|
5
|
+
const grouped = {};
|
|
6
|
+
for (const item of arr) {
|
|
6
7
|
const key = getKey(item);
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
const group = Object.hasOwn(grouped, key) ? grouped[key] : [];
|
|
9
|
+
Object.defineProperty(grouped, key, {
|
|
10
|
+
configurable: true,
|
|
11
|
+
enumerable: true,
|
|
12
|
+
value: [...group, item],
|
|
13
|
+
writable: true
|
|
14
|
+
});
|
|
15
|
+
}
|
|
9
16
|
return grouped;
|
|
10
17
|
};
|
|
@@ -49,4 +49,10 @@ describe('groupBy', () => {
|
|
|
49
49
|
]);
|
|
50
50
|
expect(result[symB]).toEqual([{ k: symB, v: 2 }]);
|
|
51
51
|
});
|
|
52
|
+
it('should handle prototype property names', () => {
|
|
53
|
+
const result = groupBy(['first', 'second', 'third'], item => item === 'third' ? 'constructor' : '__proto__');
|
|
54
|
+
expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
|
|
55
|
+
expect(result.__proto__).toEqual(['first', 'second']);
|
|
56
|
+
expect(result.constructor).toEqual(['third']);
|
|
57
|
+
});
|
|
52
58
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
|
|
2
2
|
import { isObject } from '../../validators/isObject/isObject.js';
|
|
3
|
+
import { DANGEROUS_KEYS } from '../constants.js';
|
|
3
4
|
/**
|
|
4
5
|
* Checks if key is a direct property of object.
|
|
5
6
|
* Key may be a path of a value separated by dots.
|
|
@@ -9,14 +10,14 @@ export const hasObjectProperty = (object, key) => {
|
|
|
9
10
|
if (isNullOrUndefined(object))
|
|
10
11
|
return false;
|
|
11
12
|
const keyParts = key.split('.');
|
|
13
|
+
const firstKey = keyParts[0];
|
|
14
|
+
if (isNullOrUndefined(firstKey) || DANGEROUS_KEYS.has(firstKey))
|
|
15
|
+
return false;
|
|
12
16
|
if (keyParts.length > 1) {
|
|
13
|
-
const firstKey = keyParts[0];
|
|
14
|
-
if (isNullOrUndefined(firstKey))
|
|
15
|
-
return false;
|
|
16
17
|
const deepObject = object[firstKey];
|
|
17
18
|
if (!isObject(deepObject))
|
|
18
19
|
return false;
|
|
19
20
|
return hasObjectProperty(deepObject, keyParts.slice(1).join('.'));
|
|
20
21
|
}
|
|
21
|
-
return Object.keys(object).includes(
|
|
22
|
+
return Object.keys(object).includes(firstKey);
|
|
22
23
|
};
|
|
@@ -30,4 +30,9 @@ describe('hasObjectProperty', () => {
|
|
|
30
30
|
test('returns false for empty string in path', () => {
|
|
31
31
|
expect(hasObjectProperty({ a: 1 }, '')).toBe(false);
|
|
32
32
|
});
|
|
33
|
+
test('returns false for dangerous keys in path', () => {
|
|
34
|
+
expect(hasObjectProperty({}, '__proto__')).toBe(false);
|
|
35
|
+
expect(hasObjectProperty({ a: {} }, 'a.constructor')).toBe(false);
|
|
36
|
+
expect(hasObjectProperty({ a: {} }, 'a.prototype.polluted')).toBe(false);
|
|
37
|
+
});
|
|
33
38
|
});
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -11,13 +11,16 @@ export { downloadFile } from './downloadFile/downloadFile.js';
|
|
|
11
11
|
export { getDateFromBirthNumber } from './birthNumber/getDateFromBirthNumber.js';
|
|
12
12
|
export { getDateObject } from './getDateObject/getDateObject.js';
|
|
13
13
|
export { getGenderFromBirthNumber } from './birthNumber/getGenderFromBirthNumber.js';
|
|
14
|
+
export { getLastSpecificDate } from './getLastSpecificDate/getLastSpecificDate.js';
|
|
14
15
|
export { getNumber } from './getNumber/getNumber.js';
|
|
15
16
|
export { getNumberFloat } from './getNumberFloat/getNumberFloat.js';
|
|
16
17
|
export { getNumberString } from './getNumberString/getNumberString.js';
|
|
17
18
|
export { getObjectDiff } from './getObjectDiff/getObjectDiff.js';
|
|
18
19
|
export { getObjectValue } from './getObjectValue/getObjectValue.js';
|
|
20
|
+
export { getQuarterFromDate } from './getQuarterFromDate/getQuarterFromDate.js';
|
|
19
21
|
export { getSafeMax } from './getSafeMax/getSafeMax.js';
|
|
20
22
|
export { getSafeMin } from './getSafeMin/getSafeMin.js';
|
|
23
|
+
export { getWeekFromDate } from './getWeekFromDate/getWeekFromDate.js';
|
|
21
24
|
export { groupBy } from './groupBy/groupBy.js';
|
|
22
25
|
export { hasObjectProperty } from './hasObjectProperty/hasObjectProperty.js';
|
|
23
26
|
export { isBirthNumber } from './birthNumber/isBirthNumber.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -9,13 +9,16 @@ export { downloadFile } from './downloadFile/downloadFile.js';
|
|
|
9
9
|
export { getDateFromBirthNumber } from './birthNumber/getDateFromBirthNumber.js';
|
|
10
10
|
export { getDateObject } from './getDateObject/getDateObject.js';
|
|
11
11
|
export { getGenderFromBirthNumber } from './birthNumber/getGenderFromBirthNumber.js';
|
|
12
|
+
export { getLastSpecificDate } from './getLastSpecificDate/getLastSpecificDate.js';
|
|
12
13
|
export { getNumber } from './getNumber/getNumber.js';
|
|
13
14
|
export { getNumberFloat } from './getNumberFloat/getNumberFloat.js';
|
|
14
15
|
export { getNumberString } from './getNumberString/getNumberString.js';
|
|
15
16
|
export { getObjectDiff } from './getObjectDiff/getObjectDiff.js';
|
|
16
17
|
export { getObjectValue } from './getObjectValue/getObjectValue.js';
|
|
18
|
+
export { getQuarterFromDate } from './getQuarterFromDate/getQuarterFromDate.js';
|
|
17
19
|
export { getSafeMax } from './getSafeMax/getSafeMax.js';
|
|
18
20
|
export { getSafeMin } from './getSafeMin/getSafeMin.js';
|
|
21
|
+
export { getWeekFromDate } from './getWeekFromDate/getWeekFromDate.js';
|
|
19
22
|
export { groupBy } from './groupBy/groupBy.js';
|
|
20
23
|
export { hasObjectProperty } from './hasObjectProperty/hasObjectProperty.js';
|
|
21
24
|
export { isBirthNumber } from './birthNumber/isBirthNumber.js';
|
|
@@ -5,7 +5,12 @@
|
|
|
5
5
|
export const mapObjectKeys = (obj, iteratee) => {
|
|
6
6
|
const result = {};
|
|
7
7
|
for (const key of Object.keys(obj)) {
|
|
8
|
-
result
|
|
8
|
+
Object.defineProperty(result, key, {
|
|
9
|
+
configurable: true,
|
|
10
|
+
enumerable: true,
|
|
11
|
+
value: iteratee(obj[key], key),
|
|
12
|
+
writable: true
|
|
13
|
+
});
|
|
9
14
|
}
|
|
10
15
|
return result;
|
|
11
16
|
};
|
|
@@ -48,4 +48,11 @@ describe('mapObjectKeys', () => {
|
|
|
48
48
|
const result = mapObjectKeys(obj, value => value && value * 2);
|
|
49
49
|
expect(result).toEqual({ own: 4 });
|
|
50
50
|
});
|
|
51
|
+
test('maps __proto__ as an own data property', () => {
|
|
52
|
+
const obj = JSON.parse('{"__proto__": 1}');
|
|
53
|
+
const result = mapObjectKeys(obj, value => value + 1);
|
|
54
|
+
expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
|
|
55
|
+
expect(Object.hasOwn(result, '__proto__')).toBe(true);
|
|
56
|
+
expect(result.__proto__).toBe(2);
|
|
57
|
+
});
|
|
51
58
|
});
|
|
@@ -1,26 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
1
|
+
import { innerMergeDeep } from './utils/innerMergeDeep.js';
|
|
3
2
|
/**
|
|
4
3
|
* Deeply merges two objects. Arrays are concatenated, objects are merged recursively.
|
|
5
4
|
*/
|
|
6
|
-
export const mergeDeep = (target, source) =>
|
|
7
|
-
if (!isObject(source))
|
|
8
|
-
return target;
|
|
9
|
-
const targetClone = { ...target };
|
|
10
|
-
Object.keys(source).forEach(key => {
|
|
11
|
-
if (DANGEROUS_KEYS.has(key))
|
|
12
|
-
return;
|
|
13
|
-
const targetValue = target[key];
|
|
14
|
-
const sourceValue = source[key];
|
|
15
|
-
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
16
|
-
targetClone[key] = targetValue.concat(sourceValue);
|
|
17
|
-
}
|
|
18
|
-
else if (isObject(targetValue) && isObject(sourceValue)) {
|
|
19
|
-
targetClone[key] = mergeDeep(targetValue, sourceValue);
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
targetClone[key] = sourceValue;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
return targetClone;
|
|
26
|
-
};
|
|
5
|
+
export const mergeDeep = (target, source) => innerMergeDeep(target, source, new WeakSet());
|
|
@@ -50,4 +50,9 @@ describe('mergeDeep', () => {
|
|
|
50
50
|
expect(result).toEqual({ safe: { value: 1 } });
|
|
51
51
|
expect({}.polluted).toBeUndefined();
|
|
52
52
|
});
|
|
53
|
+
it('should not stack overflow on circular references in source', () => {
|
|
54
|
+
const source = { foo: 1 };
|
|
55
|
+
source.self = source;
|
|
56
|
+
expect(() => mergeDeep({}, source)).not.toThrow();
|
|
57
|
+
});
|
|
53
58
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isObject } from '../../../validators/isObject/isObject.js';
|
|
2
|
+
import { DANGEROUS_KEYS } from '../../constants.js';
|
|
3
|
+
/**
|
|
4
|
+
* Internal utility that recursively deep-merges two objects, guarding against
|
|
5
|
+
* circular references in `source` via `seen`.
|
|
6
|
+
*/
|
|
7
|
+
export const innerMergeDeep = (target, source, seen) => {
|
|
8
|
+
if (!isObject(source))
|
|
9
|
+
return target;
|
|
10
|
+
if (seen.has(source))
|
|
11
|
+
return target;
|
|
12
|
+
seen.add(source);
|
|
13
|
+
const targetClone = { ...target };
|
|
14
|
+
Object.keys(source).forEach(key => {
|
|
15
|
+
if (DANGEROUS_KEYS.has(key))
|
|
16
|
+
return;
|
|
17
|
+
const targetValue = target[key];
|
|
18
|
+
const sourceValue = source[key];
|
|
19
|
+
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
20
|
+
targetClone[key] = targetValue.concat(sourceValue);
|
|
21
|
+
}
|
|
22
|
+
else if (isObject(targetValue) && isObject(sourceValue)) {
|
|
23
|
+
targetClone[key] = innerMergeDeep(targetValue, sourceValue, seen);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
targetClone[key] = sourceValue;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
seen.delete(source);
|
|
30
|
+
return targetClone;
|
|
31
|
+
};
|
|
@@ -6,6 +6,8 @@ import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUn
|
|
|
6
6
|
export const createRange = (min, max) => {
|
|
7
7
|
if (isNullOrUndefined(min) || isNullOrUndefined(max))
|
|
8
8
|
return null;
|
|
9
|
+
if (Number.isNaN(min) || Number.isNaN(max))
|
|
10
|
+
return null;
|
|
9
11
|
if (min > max)
|
|
10
12
|
return null;
|
|
11
13
|
return { min, max };
|
|
@@ -6,14 +6,18 @@ describe('createRange', () => {
|
|
|
6
6
|
expect(createRange(5, 5)).toEqual({ min: 5, max: 5 });
|
|
7
7
|
});
|
|
8
8
|
it('returns null if min is null or undefined', () => {
|
|
9
|
-
expect(createRange(null, 5)).
|
|
10
|
-
expect(createRange(undefined, 10)).
|
|
9
|
+
expect(createRange(null, 5)).toBeNull();
|
|
10
|
+
expect(createRange(undefined, 10)).toBeNull();
|
|
11
11
|
});
|
|
12
12
|
it('returns null if max is null or undefined', () => {
|
|
13
|
-
expect(createRange(1, null)).
|
|
14
|
-
expect(createRange(2, undefined)).
|
|
13
|
+
expect(createRange(1, null)).toBeNull();
|
|
14
|
+
expect(createRange(2, undefined)).toBeNull();
|
|
15
15
|
});
|
|
16
16
|
it('returns null if min > max', () => {
|
|
17
|
-
expect(createRange(10, 5)).
|
|
17
|
+
expect(createRange(10, 5)).toBeNull();
|
|
18
|
+
});
|
|
19
|
+
it('returns null if min or max is NaN', () => {
|
|
20
|
+
expect(createRange(Number.NaN, 5)).toBeNull();
|
|
21
|
+
expect(createRange(1, Number.NaN)).toBeNull();
|
|
18
22
|
});
|
|
19
23
|
});
|
|
@@ -8,6 +8,8 @@ export const isInRange = (value, range) => {
|
|
|
8
8
|
return false;
|
|
9
9
|
if (isNullOrUndefined(range))
|
|
10
10
|
return true;
|
|
11
|
+
if (Number.isNaN(value) || Number.isNaN(range.min) || Number.isNaN(range.max))
|
|
12
|
+
return false;
|
|
11
13
|
if (value < range.min || value > range.max)
|
|
12
14
|
return false;
|
|
13
15
|
return true;
|
|
@@ -19,4 +19,9 @@ describe('isInRange', () => {
|
|
|
19
19
|
expect(isInRange(null, range)).toBe(false);
|
|
20
20
|
expect(isInRange(undefined, range)).toBe(false);
|
|
21
21
|
});
|
|
22
|
+
it('returns false if value or range bounds are NaN', () => {
|
|
23
|
+
expect(isInRange(Number.NaN, range)).toBe(false);
|
|
24
|
+
expect(isInRange(3, { min: Number.NaN, max: 5 })).toBe(false);
|
|
25
|
+
expect(isInRange(3, { min: 1, max: Number.NaN })).toBe(false);
|
|
26
|
+
});
|
|
22
27
|
});
|
|
@@ -12,7 +12,7 @@ describe('unionRange', () => {
|
|
|
12
12
|
expect(unionRange(null, r1)).toEqual(r1);
|
|
13
13
|
});
|
|
14
14
|
it('returns null if both ranges are null', () => {
|
|
15
|
-
expect(unionRange(null, null)).
|
|
15
|
+
expect(unionRange(null, null)).toBeNull();
|
|
16
16
|
});
|
|
17
17
|
it('returns the overlapping range when one is inside the other', () => {
|
|
18
18
|
const r1 = { min: 1, max: 10 };
|
|
@@ -22,6 +22,6 @@ describe('unionRange', () => {
|
|
|
22
22
|
it('returns null if ranges do not overlap', () => {
|
|
23
23
|
const r1 = { min: 1, max: 5 };
|
|
24
24
|
const r2 = { min: 6, max: 10 };
|
|
25
|
-
expect(unionRange(r1, r2)).
|
|
25
|
+
expect(unionRange(r1, r2)).toBeNull();
|
|
26
26
|
});
|
|
27
27
|
});
|
|
@@ -24,5 +24,14 @@ describe('removeSpecialChars', () => {
|
|
|
24
24
|
});
|
|
25
25
|
test('preserves numbers and accented letters', () => {
|
|
26
26
|
expect(removeSpecialChars('1234567890áčďéěíňóřšťúůýž')).toBe('1234567890áčďéěíňóřšťúůýž');
|
|
27
|
+
expect(removeSpecialChars('ÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ')).toBe('ÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ');
|
|
28
|
+
});
|
|
29
|
+
test('handles complex input gracefully', () => {
|
|
30
|
+
const input = 'Příliš žluťoučký kůň úpěl ďábelské ódy. Červený kohout zakikirikal na střeše,' +
|
|
31
|
+
' zatímco starý bradatec mlel svou věčnou pravdu o životě. Šílený vědec v téže' +
|
|
32
|
+
' chvíli dokončoval svůj tajný projekt. Úplněk osvětloval temná zákoutí starého' +
|
|
33
|
+
' města, kde se stíny míhaly rychlostí blesku. Každý tón tiché hudby připomínal' +
|
|
34
|
+
' dávno zapomenuté časy, kdy lidé ještě věřili na kouzla a mýty.';
|
|
35
|
+
expect(removeSpecialChars(input)).toBe(input);
|
|
27
36
|
});
|
|
28
37
|
});
|
|
@@ -10,8 +10,8 @@ describe('removeWhitespacesFromUnknown', () => {
|
|
|
10
10
|
});
|
|
11
11
|
it('returns non-string input unchanged', () => {
|
|
12
12
|
expect(removeWhitespacesFromUnknown(123)).toBe(123);
|
|
13
|
-
expect(removeWhitespacesFromUnknown(null)).
|
|
14
|
-
expect(removeWhitespacesFromUnknown(undefined)).
|
|
13
|
+
expect(removeWhitespacesFromUnknown(null)).toBeNull();
|
|
14
|
+
expect(removeWhitespacesFromUnknown(undefined)).toBeUndefined();
|
|
15
15
|
expect(removeWhitespacesFromUnknown({ a: 1 })).toEqual({ a: 1 });
|
|
16
16
|
expect(removeWhitespacesFromUnknown([1, 2, 3])).toEqual([1, 2, 3]);
|
|
17
17
|
expect(removeWhitespacesFromUnknown(true)).toBe(true);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
|
|
2
2
|
import { isObject } from '../../validators/isObject/isObject.js';
|
|
3
3
|
import { isString } from '../../validators/isString/isString.js';
|
|
4
|
+
import { DANGEROUS_KEYS } from '../constants.js';
|
|
4
5
|
const PATH_TOKEN_REGEX = /([^.[\]]+)|\[(\d+)\]/g;
|
|
5
|
-
const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
6
6
|
const parsePath = (path) => {
|
|
7
7
|
const tokens = [];
|
|
8
8
|
for (const match of path.matchAll(PATH_TOKEN_REGEX)) {
|
|
@@ -19,13 +19,16 @@ export { isNumber } from './isNumber/isNumber.js';
|
|
|
19
19
|
export { isNumberOrString } from './isNumberOrString/isNumberOrString.js';
|
|
20
20
|
export { isNumericString } from './isNumericString/isNumericString.js';
|
|
21
21
|
export { isObject } from './isObject/isObject.js';
|
|
22
|
+
export { isPlainObject } from './isPlainObject/isPlainObject.js';
|
|
22
23
|
export { isString } from './isString/isString.js';
|
|
23
24
|
export { isUndefined } from './isUndefined/isUndefined.js';
|
|
24
25
|
export { isValidCzechPhoneNumber } from './isValidCzechPhoneNumber/isValidCzechPhoneNumber.js';
|
|
25
26
|
export { isValidDate } from './isValidDate/isValidDate.js';
|
|
27
|
+
export { isValidDateInstance } from './isValidDateInstance/isValidDateInstance.js';
|
|
26
28
|
export { isValidDrivingLicense } from './isValidDrivingLicense/isValidDrivingLicense.js';
|
|
27
29
|
export { isValidForeignPhoneNumber } from './isValidForeignPhoneNumber/isValidForeignPhoneNumber.js';
|
|
28
30
|
export { isValidIdentityCard } from './isValidIdentityCard/isValidIdentityCard.js';
|
|
31
|
+
export { isValidMrzCheckDigit } from './isValidMrzCheckDigit/isValidMrzCheckDigit.js';
|
|
29
32
|
export { isValidPassport } from './isValidPassport/isValidPassport.js';
|
|
30
33
|
export { isValidVehicleIdentification } from './isValidVehicleIdentification/isValidVehicleIdentification.js';
|
|
31
34
|
export { isZipCode } from './isZipCode/isZipCode.js';
|
package/dist/validators/index.js
CHANGED
|
@@ -19,13 +19,16 @@ export { isNumber } from './isNumber/isNumber.js';
|
|
|
19
19
|
export { isNumberOrString } from './isNumberOrString/isNumberOrString.js';
|
|
20
20
|
export { isNumericString } from './isNumericString/isNumericString.js';
|
|
21
21
|
export { isObject } from './isObject/isObject.js';
|
|
22
|
+
export { isPlainObject } from './isPlainObject/isPlainObject.js';
|
|
22
23
|
export { isString } from './isString/isString.js';
|
|
23
24
|
export { isUndefined } from './isUndefined/isUndefined.js';
|
|
24
25
|
export { isValidCzechPhoneNumber } from './isValidCzechPhoneNumber/isValidCzechPhoneNumber.js';
|
|
25
26
|
export { isValidDate } from './isValidDate/isValidDate.js';
|
|
27
|
+
export { isValidDateInstance } from './isValidDateInstance/isValidDateInstance.js';
|
|
26
28
|
export { isValidDrivingLicense } from './isValidDrivingLicense/isValidDrivingLicense.js';
|
|
27
29
|
export { isValidForeignPhoneNumber } from './isValidForeignPhoneNumber/isValidForeignPhoneNumber.js';
|
|
28
30
|
export { isValidIdentityCard } from './isValidIdentityCard/isValidIdentityCard.js';
|
|
31
|
+
export { isValidMrzCheckDigit } from './isValidMrzCheckDigit/isValidMrzCheckDigit.js';
|
|
29
32
|
export { isValidPassport } from './isValidPassport/isValidPassport.js';
|
|
30
33
|
export { isValidVehicleIdentification } from './isValidVehicleIdentification/isValidVehicleIdentification.js';
|
|
31
34
|
export { isZipCode } from './isZipCode/isZipCode.js';
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Validates if the provided string is a valid czech bank account number.
|
|
3
|
-
* A valid czech bank account number can be in the format
|
|
3
|
+
* A valid czech bank account number can be in the format
|
|
4
|
+
* "123", "123-123", "123-123/1234" or "123/1234".
|
|
5
|
+
*
|
|
4
6
|
* The function checks for correct length, format, and checksum.
|
|
5
7
|
* http://www.cnb.cz/cs/platebni_styk/pravni_predpisy/download/vyhl_169_2011.pdf
|
|
6
8
|
*/
|
|
@@ -1,24 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isModulo11 } from '../isModulo11/isModulo11.js';
|
|
2
2
|
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
3
|
-
const
|
|
4
|
-
const BANK_ACCOUNT_NUMBER_REGEX = /^(\d{2,10}|\d{1,6}-\d{2,10})$/;
|
|
5
|
-
const checkSum = (input) => {
|
|
6
|
-
const numbers = input
|
|
7
|
-
.split('')
|
|
8
|
-
.map(getNumber)
|
|
9
|
-
.filter(item => !isNullOrUndefined(item));
|
|
10
|
-
let sum = 0;
|
|
11
|
-
numbers.forEach((item, index) => {
|
|
12
|
-
const weight = BANK_ACCOUNT_WEIGHTS[index];
|
|
13
|
-
if (isNullOrUndefined(weight))
|
|
14
|
-
return; // This should not happen due to regex check
|
|
15
|
-
sum += item * weight;
|
|
16
|
-
});
|
|
17
|
-
return sum % 11 === 0;
|
|
18
|
-
};
|
|
3
|
+
const BANK_ACCOUNT_NUMBER_REGEX = /^(\d{2,10}|\d{1,6}-\d{2,10}|\d{1,6}-\d{2,10}\/\d{4}|\d{2,10}\/\d{4})$/;
|
|
19
4
|
/**
|
|
20
5
|
* Validates if the provided string is a valid czech bank account number.
|
|
21
|
-
* A valid czech bank account number can be in the format
|
|
6
|
+
* A valid czech bank account number can be in the format
|
|
7
|
+
* "123", "123-123", "123-123/1234" or "123/1234".
|
|
8
|
+
*
|
|
22
9
|
* The function checks for correct length, format, and checksum.
|
|
23
10
|
* http://www.cnb.cz/cs/platebni_styk/pravni_predpisy/download/vyhl_169_2011.pdf
|
|
24
11
|
*/
|
|
@@ -27,7 +14,10 @@ export const isBankAccount = (account) => {
|
|
|
27
14
|
return false;
|
|
28
15
|
if (!BANK_ACCOUNT_NUMBER_REGEX.test(account))
|
|
29
16
|
return false;
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
17
|
+
// Remove the branch code if present
|
|
18
|
+
const accountWithoutBranch = account.split('/')[0];
|
|
19
|
+
if (isNullOrUndefined(accountWithoutBranch))
|
|
20
|
+
return false;
|
|
21
|
+
const numbers = accountWithoutBranch.split('-');
|
|
22
|
+
return numbers.every(item => isModulo11(Number(item)));
|
|
33
23
|
};
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import { isBankAccount } from './isBankAccount.js';
|
|
3
3
|
describe('isBankAccount', () => {
|
|
4
|
+
it('returns true for valid two-part account number with valid checksums', () => {
|
|
5
|
+
expect(isBankAccount('123-123')).toBe(true);
|
|
6
|
+
expect(isBankAccount('123-123/1234')).toBe(true);
|
|
7
|
+
expect(isBankAccount('123')).toBe(true);
|
|
8
|
+
expect(isBankAccount('123/1234')).toBe(true);
|
|
9
|
+
});
|
|
4
10
|
it('returns false for null or undefined', () => {
|
|
5
11
|
expect(isBankAccount(null)).toBe(false);
|
|
6
12
|
expect(isBankAccount(undefined)).toBe(false);
|
|
@@ -22,9 +28,6 @@ describe('isBankAccount', () => {
|
|
|
22
28
|
it('returns false for valid single part account number with invalid checksum', () => {
|
|
23
29
|
expect(isBankAccount('1234567890')).toBe(false);
|
|
24
30
|
});
|
|
25
|
-
it('returns true for valid two-part account number with valid checksums', () => {
|
|
26
|
-
expect(isBankAccount('123-123')).toBe(true);
|
|
27
|
-
});
|
|
28
31
|
it('returns false for two-part account number with one invalid checksum', () => {
|
|
29
32
|
expect(isBankAccount('123456-0000000000')).toBe(false);
|
|
30
33
|
expect(isBankAccount('000001-1234567890')).toBe(false);
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Simplified RFC 5322 Official Standard regex;
|
|
4
|
+
* blocks leading/trailing/consecutive dots and hyphen-leading domain labels.
|
|
5
|
+
*/
|
|
6
|
+
const EMAIL_REGEX = /^[a-z0-9_%+-]+(?:\.[a-z0-9_%+-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i;
|
|
3
7
|
/**
|
|
4
8
|
* Checks if a string is a valid email address.
|
|
5
9
|
*/
|
|
@@ -7,6 +7,9 @@ describe('isEmail', () => {
|
|
|
7
7
|
expect(isEmail('user_name@example.io')).toBe(true);
|
|
8
8
|
expect(isEmail('user-name@sub.domain.com')).toBe(true);
|
|
9
9
|
expect(isEmail('a@b.cz')).toBe(true);
|
|
10
|
+
expect(isEmail('_______@example.com')).toBe(true);
|
|
11
|
+
expect(isEmail('email@example-one.com')).toBe(true);
|
|
12
|
+
expect(isEmail('email@example.co.jp')).toBe(true);
|
|
10
13
|
});
|
|
11
14
|
it('returns false for invalid emails', () => {
|
|
12
15
|
expect(isEmail('plainaddress')).toBe(false);
|
|
@@ -18,6 +21,18 @@ describe('isEmail', () => {
|
|
|
18
21
|
expect(isEmail('user@domain,com')).toBe(false);
|
|
19
22
|
expect(isEmail('user@domain@domain.com')).toBe(false);
|
|
20
23
|
expect(isEmail('user@domain.')).toBe(false);
|
|
24
|
+
expect(isEmail('user@seznam...............cz')).toBe(false);
|
|
25
|
+
expect(isEmail('.email@example.com')).toBe(false);
|
|
26
|
+
expect(isEmail('email.@example.com')).toBe(false);
|
|
27
|
+
expect(isEmail('email..email@example.com')).toBe(false);
|
|
28
|
+
expect(isEmail('Abc..123@example.com')).toBe(false);
|
|
29
|
+
expect(isEmail('email@-example.com')).toBe(false);
|
|
30
|
+
expect(isEmail('email@example..com')).toBe(false);
|
|
31
|
+
expect(isEmail('email@111.222.333.44444')).toBe(false);
|
|
32
|
+
expect(isEmail('Joe Smith <email@example.com>')).toBe(false);
|
|
33
|
+
expect(isEmail('あいうえお@example.com')).toBe(false);
|
|
34
|
+
expect(isEmail('email@example.com ')).toBe(false);
|
|
35
|
+
expect(isEmail(' email@example.com')).toBe(false);
|
|
21
36
|
expect(isEmail('')).toBe(false);
|
|
22
37
|
expect(isEmail(null)).toBe(false);
|
|
23
38
|
expect(isEmail(undefined)).toBe(false);
|