@mmb-digital/shared-libs-auto 0.0.21 → 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 +23 -22
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import { isBoolean } from '../isBoolean/isBoolean.js';
|
|
2
2
|
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
3
3
|
import { isNumber } from '../isNumber/isNumber.js';
|
|
4
|
-
import {
|
|
4
|
+
import { isPlainObject } from '../isPlainObject/isPlainObject.js';
|
|
5
5
|
import { isString } from '../isString/isString.js';
|
|
6
6
|
/**
|
|
7
|
-
* Deep equality check for
|
|
7
|
+
* Deep equality check for primitives, arrays, plain objects, and Date instances.
|
|
8
8
|
* Replace for _.isEqual
|
|
9
9
|
*/
|
|
10
10
|
export function isEqual(a, b) {
|
|
11
11
|
if (isNullOrUndefined(a) && isNullOrUndefined(b))
|
|
12
12
|
return true;
|
|
13
|
+
if (a === b)
|
|
14
|
+
return true;
|
|
13
15
|
if (isBoolean(a) && isBoolean(b) && a === b)
|
|
14
16
|
return true;
|
|
15
17
|
if (isNumber(a) && isNumber(b) && (a === b || (Number.isNaN(a) && Number.isNaN(b))))
|
|
16
18
|
return true;
|
|
17
19
|
if (isString(a) && isString(b) && a === b)
|
|
18
20
|
return true;
|
|
19
|
-
if (
|
|
21
|
+
if (a instanceof Date && b instanceof Date)
|
|
22
|
+
return a.getTime() === b.getTime();
|
|
23
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
20
24
|
const aKeys = Object.keys(a);
|
|
21
25
|
const bKeys = Object.keys(b);
|
|
22
26
|
if (aKeys.length !== bKeys.length)
|
|
@@ -46,4 +46,16 @@ describe('isEqual', () => {
|
|
|
46
46
|
test('should return false for deeply different arrays/objects', () => {
|
|
47
47
|
expect(isEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 3 }])).toBe(false);
|
|
48
48
|
});
|
|
49
|
+
test('should compare dates by time value', () => {
|
|
50
|
+
expect(isEqual(new Date('2025-01-01'), new Date('2025-01-01'))).toBe(true);
|
|
51
|
+
expect(isEqual(new Date('2025-01-01'), new Date('2026-01-01'))).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
test('should compare non-plain object instances by reference', () => {
|
|
54
|
+
class CustomClass {
|
|
55
|
+
}
|
|
56
|
+
const instance = new CustomClass();
|
|
57
|
+
expect(isEqual(instance, instance)).toBe(true);
|
|
58
|
+
expect(isEqual(new CustomClass(), new CustomClass())).toBe(false);
|
|
59
|
+
expect(isEqual(new Map(), new Map())).toBe(false);
|
|
60
|
+
});
|
|
49
61
|
});
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* Checks if the given number is valid according to the Modulo 11 algorithm.
|
|
3
3
|
*/
|
|
4
4
|
export const isModulo11 = (number) => {
|
|
5
|
+
if (Number.isNaN(number))
|
|
6
|
+
return false;
|
|
5
7
|
const numbers = number.toString().split('').map(Number);
|
|
6
8
|
const factors = [6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // weighted checking factors
|
|
7
9
|
const checksum = numbers.reduce((sum, digit, index) => {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is a plain object.
|
|
3
|
+
*/
|
|
4
|
+
export const isPlainObject = (value) => {
|
|
5
|
+
if (value === null || typeof value !== 'object')
|
|
6
|
+
return false;
|
|
7
|
+
const prototype = Object.getPrototypeOf(value);
|
|
8
|
+
return prototype === Object.prototype || prototype === null;
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { isPlainObject } from './isPlainObject.js';
|
|
3
|
+
describe('isPlainObject', () => {
|
|
4
|
+
test('should return true for objects with Object or null prototype', () => {
|
|
5
|
+
expect(isPlainObject({})).toBe(true);
|
|
6
|
+
expect(isPlainObject({ a: 1 })).toBe(true);
|
|
7
|
+
expect(isPlainObject(Object.create(null))).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
test('should return false for non-plain objects', () => {
|
|
10
|
+
expect(isPlainObject([])).toBe(false);
|
|
11
|
+
expect(isPlainObject(new Date())).toBe(false);
|
|
12
|
+
expect(isPlainObject(new Map())).toBe(false);
|
|
13
|
+
expect(isPlainObject(/test/)).toBe(false);
|
|
14
|
+
expect(isPlainObject(new (class CustomClass {
|
|
15
|
+
})())).toBe(false);
|
|
16
|
+
});
|
|
17
|
+
test('should return false for primitive values', () => {
|
|
18
|
+
expect(isPlainObject(null)).toBe(false);
|
|
19
|
+
expect(isPlainObject(undefined)).toBe(false);
|
|
20
|
+
expect(isPlainObject('test')).toBe(false);
|
|
21
|
+
expect(isPlainObject(123)).toBe(false);
|
|
22
|
+
expect(isPlainObject(true)).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { isValid } from 'date-fns';
|
|
2
|
+
import { parseDate } from '../../utils/getDateObject/utils/parseDate.js';
|
|
1
3
|
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
2
4
|
/**
|
|
3
5
|
* Checks if a string is a valid date.
|
|
4
6
|
*
|
|
5
7
|
* @example
|
|
6
8
|
* isValidDate('2025-05-21 12:34:51') // true
|
|
9
|
+
* isValidDate('2025-05-21 12:34') // true
|
|
7
10
|
* isValidDate('2025-09-03T13:32:21.582Z') // true
|
|
8
11
|
* isValidDate('2025-05-21') // true
|
|
9
12
|
* isValidDate('05/21/2025') // true - MM/DD/YYYY format
|
|
@@ -18,30 +21,5 @@ import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
|
18
21
|
export const isValidDate = (input) => {
|
|
19
22
|
if (isNullOrUndefined(input))
|
|
20
23
|
return false;
|
|
21
|
-
|
|
22
|
-
// const date = new Date(input)
|
|
23
|
-
// const isInvalid = Number.isNaN(date.getTime())
|
|
24
|
-
// So we need to check if the date object created from the input has the same year, month and day as the input.
|
|
25
|
-
const dateObject = new Date(input);
|
|
26
|
-
const splitInputChar = input.includes('T') ? 'T' : ' ';
|
|
27
|
-
const [dateInput] = input.split(splitInputChar);
|
|
28
|
-
if (isNullOrUndefined(dateInput))
|
|
29
|
-
return false;
|
|
30
|
-
if (dateInput.includes('/')) {
|
|
31
|
-
const [month, day, year] = dateInput.split('/');
|
|
32
|
-
if (isNullOrUndefined(year) || isNullOrUndefined(month) || isNullOrUndefined(day))
|
|
33
|
-
return false;
|
|
34
|
-
return (dateObject.getFullYear() === +year &&
|
|
35
|
-
dateObject.getMonth() === +month - 1 &&
|
|
36
|
-
dateObject.getDate() === +day);
|
|
37
|
-
}
|
|
38
|
-
if (dateInput.includes('-')) {
|
|
39
|
-
const [year, month, day] = dateInput.split('-');
|
|
40
|
-
if (isNullOrUndefined(year) || isNullOrUndefined(month) || isNullOrUndefined(day))
|
|
41
|
-
return false;
|
|
42
|
-
return (dateObject.getFullYear() === +year &&
|
|
43
|
-
dateObject.getMonth() === +month - 1 &&
|
|
44
|
-
dateObject.getDate() === +day);
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
24
|
+
return isValid(parseDate(input));
|
|
47
25
|
};
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
2
|
import { isValidDate } from './isValidDate.js';
|
|
3
3
|
describe('Should validate date', () => {
|
|
4
4
|
it('Should validate datetime', () => {
|
|
5
5
|
expect(isValidDate('2025-05-21 12:34:51')).toStrictEqual(true);
|
|
6
|
+
expect(isValidDate('2025-05-21 12:34')).toStrictEqual(true);
|
|
6
7
|
expect(isValidDate('2025-05-21')).toStrictEqual(true);
|
|
7
8
|
expect(isValidDate('05/21/2025')).toStrictEqual(true);
|
|
8
9
|
expect(isValidDate('2025-09-03T13:32:21.582Z')).toStrictEqual(true);
|
|
9
10
|
});
|
|
11
|
+
it('Validates date-only ISO strings in a negative UTC offset', () => {
|
|
12
|
+
vi.stubEnv('TZ', 'America/New_York');
|
|
13
|
+
try {
|
|
14
|
+
expect(new Date('2025-05-21').getDate()).toBe(20);
|
|
15
|
+
expect(isValidDate('2025-05-21')).toBe(true);
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
vi.unstubAllEnvs();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
10
21
|
});
|
|
11
22
|
describe('Invalid values', () => {
|
|
12
23
|
it('Should reject impossible calendar dates', () => {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the provided input is a valid Date instance.
|
|
4
|
+
*/
|
|
5
|
+
export const isValidDateInstance = (input) => {
|
|
6
|
+
if (isNullOrUndefined(input))
|
|
7
|
+
return false;
|
|
8
|
+
if (input instanceof Date && !Number.isNaN(input.getTime()))
|
|
9
|
+
return true;
|
|
10
|
+
return false;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { isValidDateInstance } from './isValidDateInstance.js';
|
|
3
|
+
describe('isValidDateInstance', () => {
|
|
4
|
+
describe('Valid Date instances', () => {
|
|
5
|
+
it('should return true for valid Date instances', () => {
|
|
6
|
+
expect(isValidDateInstance(new Date())).toBe(true);
|
|
7
|
+
expect(isValidDateInstance(new Date('2025-01-15'))).toBe(true);
|
|
8
|
+
expect(isValidDateInstance(new Date('2025-01-15T10:30:00Z'))).toBe(true);
|
|
9
|
+
expect(isValidDateInstance(new Date(1234567890000))).toBe(true);
|
|
10
|
+
});
|
|
11
|
+
it('should return true for Date instances with specific timestamps', () => {
|
|
12
|
+
const specificDate = new Date(2025, 0, 15, 10, 30, 0);
|
|
13
|
+
expect(isValidDateInstance(specificDate)).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
describe('Invalid Date instances', () => {
|
|
17
|
+
it('should return false for invalid Date instances (NaN)', () => {
|
|
18
|
+
expect(isValidDateInstance(new Date('invalid'))).toBe(false);
|
|
19
|
+
expect(isValidDateInstance(new Date(NaN))).toBe(false);
|
|
20
|
+
expect(isValidDateInstance(new Date(''))).toBe(false);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe('Null and undefined values', () => {
|
|
24
|
+
it('should return false for null', () => {
|
|
25
|
+
expect(isValidDateInstance(null)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
it('should return false for undefined', () => {
|
|
28
|
+
expect(isValidDateInstance(undefined)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe('Primitive types', () => {
|
|
32
|
+
it('should return false for strings', () => {
|
|
33
|
+
expect(isValidDateInstance('2025-01-15')).toBe(false);
|
|
34
|
+
expect(isValidDateInstance('2025-01-15T10:30:00Z')).toBe(false);
|
|
35
|
+
expect(isValidDateInstance('invalid date')).toBe(false);
|
|
36
|
+
expect(isValidDateInstance('')).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
it('should return false for numbers', () => {
|
|
39
|
+
expect(isValidDateInstance(1234567890000)).toBe(false);
|
|
40
|
+
expect(isValidDateInstance(0)).toBe(false);
|
|
41
|
+
expect(isValidDateInstance(-1)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
it('should return false for booleans', () => {
|
|
44
|
+
expect(isValidDateInstance(true)).toBe(false);
|
|
45
|
+
expect(isValidDateInstance(false)).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe('Objects and arrays', () => {
|
|
49
|
+
it('should return false for plain objects', () => {
|
|
50
|
+
expect(isValidDateInstance({})).toBe(false);
|
|
51
|
+
expect(isValidDateInstance({ date: new Date() })).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
it('should return false for arrays', () => {
|
|
54
|
+
expect(isValidDateInstance([])).toBe(false);
|
|
55
|
+
expect(isValidDateInstance([new Date()])).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('Functions and symbols', () => {
|
|
59
|
+
it('should return false for functions', () => {
|
|
60
|
+
expect(isValidDateInstance(() => { })).toBe(false);
|
|
61
|
+
expect(isValidDateInstance(function () { })).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
it('should return false for symbols', () => {
|
|
64
|
+
expect(isValidDateInstance(Symbol('date'))).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Checks if a string is a valid foreign phone number.
|
|
3
|
-
* A valid foreign phone number starts with + followed by
|
|
3
|
+
* A valid foreign phone number starts with + followed by one or more digits (spaces are stripped before testing).
|
|
4
4
|
*/
|
|
5
5
|
export declare const isValidForeignPhoneNumber: (phoneNumber: string | null | undefined) => boolean;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { removeWhitespaces } from '../../utils/removeWhitespaces/removeWhitespaces.js';
|
|
2
2
|
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
3
|
-
const FOREIGN_PHONE_REG_EXP = /^\+\d
|
|
3
|
+
const FOREIGN_PHONE_REG_EXP = /^\+\d+$/;
|
|
4
4
|
/**
|
|
5
5
|
* Checks if a string is a valid foreign phone number.
|
|
6
|
-
* A valid foreign phone number starts with + followed by
|
|
6
|
+
* A valid foreign phone number starts with + followed by one or more digits (spaces are stripped before testing).
|
|
7
7
|
*/
|
|
8
8
|
export const isValidForeignPhoneNumber = (phoneNumber) => {
|
|
9
9
|
if (isNullOrUndefined(phoneNumber))
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given MRZ (via ICAO 9303) check digit is valid based on its checksum.
|
|
3
|
+
* The last digit is a checksum calculated using digits and the weights [7, 3, 1].
|
|
4
|
+
* Useful for validating identity card numbers in Machine Readable Zone (MRZ) formats.
|
|
5
|
+
*/
|
|
6
|
+
export declare const isValidMrzCheckDigit: (value: string | number | null | undefined, checkDigit: string | number | null | undefined) => boolean;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isNullOrUndefined } from '../isNullOrUndefined/isNullOrUndefined.js';
|
|
2
|
+
import { isNumericString } from '../isNumericString/isNumericString.js';
|
|
3
|
+
const WEIGHTS = [7, 3, 1];
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the given MRZ (via ICAO 9303) check digit is valid based on its checksum.
|
|
6
|
+
* The last digit is a checksum calculated using digits and the weights [7, 3, 1].
|
|
7
|
+
* Useful for validating identity card numbers in Machine Readable Zone (MRZ) formats.
|
|
8
|
+
*/
|
|
9
|
+
export const isValidMrzCheckDigit = (value, checkDigit) => {
|
|
10
|
+
if (isNullOrUndefined(value))
|
|
11
|
+
return false;
|
|
12
|
+
if (isNullOrUndefined(checkDigit))
|
|
13
|
+
return false;
|
|
14
|
+
if (!isNumericString(value))
|
|
15
|
+
return false;
|
|
16
|
+
if (!isNumericString(checkDigit))
|
|
17
|
+
return false;
|
|
18
|
+
const digits = value.toString().split('').map(Number);
|
|
19
|
+
const checkSum = digits.reduce((sum, digit, index) => {
|
|
20
|
+
const weight = WEIGHTS[index % WEIGHTS.length];
|
|
21
|
+
if (isNullOrUndefined(weight))
|
|
22
|
+
return sum; // typescript guard for nullish value, should not happen in practice
|
|
23
|
+
return sum + digit * weight;
|
|
24
|
+
}, 0);
|
|
25
|
+
const checkModulo = checkSum % 10;
|
|
26
|
+
return checkModulo === Number(checkDigit);
|
|
27
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { isValidMrzCheckDigit } from './isValidMrzCheckDigit.js';
|
|
3
|
+
describe('isValidMrzCheckDigit', () => {
|
|
4
|
+
it('should return true for a valid MRZ check digit', () => {
|
|
5
|
+
expect(isValidMrzCheckDigit('123456789', '7')).toEqual(true);
|
|
6
|
+
expect(isValidMrzCheckDigit('123456789', 7)).toEqual(true);
|
|
7
|
+
expect(isValidMrzCheckDigit(123456789, 7)).toEqual(true);
|
|
8
|
+
expect(isValidMrzCheckDigit(123456789, '7')).toEqual(true);
|
|
9
|
+
expect(isValidMrzCheckDigit('998001351681100882201100', '4')).toEqual(true);
|
|
10
|
+
});
|
|
11
|
+
it('should return false for an invalid MRZ check digit', () => {
|
|
12
|
+
expect(isValidMrzCheckDigit('1234567897', '0')).toEqual(false);
|
|
13
|
+
expect(isValidMrzCheckDigit(1234567897, 0)).toEqual(false);
|
|
14
|
+
expect(isValidMrzCheckDigit('1234567890', '1')).toEqual(false);
|
|
15
|
+
expect(isValidMrzCheckDigit(1234567890, 1)).toEqual(false);
|
|
16
|
+
expect(isValidMrzCheckDigit('', '')).toEqual(false);
|
|
17
|
+
});
|
|
18
|
+
it('should return false for a null or undefined MRZ check digit', () => {
|
|
19
|
+
expect(isValidMrzCheckDigit(null, null)).toEqual(false);
|
|
20
|
+
expect(isValidMrzCheckDigit(undefined, undefined)).toEqual(false);
|
|
21
|
+
expect(isValidMrzCheckDigit(123456789, null)).toEqual(false);
|
|
22
|
+
expect(isValidMrzCheckDigit(123456789, undefined)).toEqual(false);
|
|
23
|
+
expect(isValidMrzCheckDigit(null, 1)).toEqual(false);
|
|
24
|
+
expect(isValidMrzCheckDigit(undefined, 1)).toEqual(false);
|
|
25
|
+
});
|
|
26
|
+
});
|
package/dist/yup/index.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export { isModulo11YupTest } from './isModulo11/isModulo11YupTest.js';
|
|
|
8
8
|
export { isICOYupTest } from './isICO/isICOYupTest.js';
|
|
9
9
|
export { isNumberSameOrGreaterYupWhen } from './isNumberSameOrGreater/isNumberSameOrGreaterYupWhen.js';
|
|
10
10
|
export { isNumberSameOrLowerYupWhen } from './isNumberSameOrLower/isNumberSameOrLowerYupWhen.js';
|
|
11
|
+
export { isDateAtLeastYearsAgoYupTest } from './isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.js';
|
|
11
12
|
export { maxDigitsLengthYupTest } from './maxDigitsLength/maxDigitsLengthYupTest.js';
|
|
12
13
|
export { minDigitsLengthYupTest } from './minDigitsLength/minDigitsLengthYupTest.js';
|
package/dist/yup/index.js
CHANGED
|
@@ -8,5 +8,6 @@ export { isModulo11YupTest } from './isModulo11/isModulo11YupTest.js';
|
|
|
8
8
|
export { isICOYupTest } from './isICO/isICOYupTest.js';
|
|
9
9
|
export { isNumberSameOrGreaterYupWhen } from './isNumberSameOrGreater/isNumberSameOrGreaterYupWhen.js';
|
|
10
10
|
export { isNumberSameOrLowerYupWhen } from './isNumberSameOrLower/isNumberSameOrLowerYupWhen.js';
|
|
11
|
+
export { isDateAtLeastYearsAgoYupTest } from './isDateAtLeastYearsAgo/isDateAtLeastYearsAgoYupTest.js';
|
|
11
12
|
export { maxDigitsLengthYupTest } from './maxDigitsLength/maxDigitsLengthYupTest.js';
|
|
12
13
|
export { minDigitsLengthYupTest } from './minDigitsLength/minDigitsLengthYupTest.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TestConfig } from 'yup';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Yup test configuration for validating if a date string is at least the given number of years in the past.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```
|
|
7
|
+
* const yupSchema = yup.object().shape({
|
|
8
|
+
* 'dateOfBirth': yup.string().test(isDateAtLeastYearsAgoYupTest(18, i18n.t('You must be at least 18 years old')))
|
|
9
|
+
* })
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const isDateAtLeastYearsAgoYupTest: (minimumYears: number, message: string) => TestConfig<string | null | undefined>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { isBefore, isSameDay, subYears } from 'date-fns';
|
|
2
|
+
import { getDateObject } from '../../utils/getDateObject/getDateObject.js';
|
|
3
|
+
import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Yup test configuration for validating if a date string is at least the given number of years in the past.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```
|
|
9
|
+
* const yupSchema = yup.object().shape({
|
|
10
|
+
* 'dateOfBirth': yup.string().test(isDateAtLeastYearsAgoYupTest(18, i18n.t('You must be at least 18 years old')))
|
|
11
|
+
* })
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export const isDateAtLeastYearsAgoYupTest = (minimumYears, message) => {
|
|
15
|
+
return {
|
|
16
|
+
name: 'isDateAtLeastYearsAgoYupTest',
|
|
17
|
+
exclusive: true,
|
|
18
|
+
params: { minimumYears },
|
|
19
|
+
message,
|
|
20
|
+
test: (value) => {
|
|
21
|
+
if (isNullOrUndefined(value))
|
|
22
|
+
return true;
|
|
23
|
+
if (!Number.isFinite(minimumYears) || minimumYears < 0)
|
|
24
|
+
return true;
|
|
25
|
+
const valueDate = getDateObject(value);
|
|
26
|
+
if (isNullOrUndefined(valueDate))
|
|
27
|
+
return false;
|
|
28
|
+
const minimumYearsAgo = subYears(new Date(), minimumYears);
|
|
29
|
+
return isBefore(valueDate, minimumYearsAgo) || isSameDay(valueDate, minimumYearsAgo);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { addDays, format, subYears } from 'date-fns';
|
|
2
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
3
|
+
import { isDateAtLeastYearsAgoYupTest } from './isDateAtLeastYearsAgoYupTest.js';
|
|
4
|
+
describe('isDateAtLeastYearsAgoYupTest', () => {
|
|
5
|
+
const minimumYears = 18;
|
|
6
|
+
const testMessage = 'You must be at least 18 years old';
|
|
7
|
+
const testConfig = isDateAtLeastYearsAgoYupTest(minimumYears, testMessage);
|
|
8
|
+
const mockContext = {};
|
|
9
|
+
let now;
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
now = new Date();
|
|
12
|
+
});
|
|
13
|
+
test('should return a TestConfig object', () => {
|
|
14
|
+
expect(testConfig).toBeDefined();
|
|
15
|
+
expect(testConfig.name).toBe('isDateAtLeastYearsAgoYupTest');
|
|
16
|
+
expect(testConfig.exclusive).toBe(true);
|
|
17
|
+
expect(testConfig.params).toEqual({ minimumYears });
|
|
18
|
+
expect(testConfig.message).toBe(testMessage);
|
|
19
|
+
expect(testConfig.test).toBeDefined();
|
|
20
|
+
});
|
|
21
|
+
test('should return true for null, undefined values', () => {
|
|
22
|
+
expect(testConfig.test.call(mockContext, null, mockContext)).toBe(true);
|
|
23
|
+
expect(testConfig.test.call(mockContext, undefined, mockContext)).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
test('should return false for invalid date strings', () => {
|
|
26
|
+
expect(testConfig.test.call(mockContext, 'invalid-date', mockContext)).toBe(false);
|
|
27
|
+
expect(testConfig.test.call(mockContext, 'not a date', mockContext)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
test('should return true for dates older than 18 years', () => {
|
|
30
|
+
const dateString = format(subYears(now, 25), 'yyyy-MM-dd');
|
|
31
|
+
expect(testConfig.test.call(mockContext, dateString, mockContext)).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
test('should return false for dates newer than 18 years', () => {
|
|
34
|
+
const dateString = format(subYears(now, 17), 'yyyy-MM-dd');
|
|
35
|
+
expect(testConfig.test.call(mockContext, dateString, mockContext)).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
test('should return true for date exactly 18 years ago', () => {
|
|
38
|
+
const dateString = format(subYears(now, 18), 'yyyy-MM-dd');
|
|
39
|
+
expect(testConfig.test.call(mockContext, dateString, mockContext)).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
test('should reject date one day newer in a negative UTC offset', () => {
|
|
42
|
+
vi.stubEnv('TZ', 'America/New_York');
|
|
43
|
+
vi.useFakeTimers();
|
|
44
|
+
vi.setSystemTime(new Date('2026-09-02T03:00:00.000Z'));
|
|
45
|
+
try {
|
|
46
|
+
const dateString = format(addDays(subYears(new Date(), minimumYears), 1), 'yyyy-MM-dd');
|
|
47
|
+
expect(testConfig.test.call(mockContext, dateString, mockContext)).toBe(false);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
vi.useRealTimers();
|
|
51
|
+
vi.unstubAllEnvs();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
test('should accept custom error message', () => {
|
|
55
|
+
const customMessage = 'Custom age validation message';
|
|
56
|
+
const customTestConfig = isDateAtLeastYearsAgoYupTest(minimumYears, customMessage);
|
|
57
|
+
expect(customTestConfig.message).toBe(customMessage);
|
|
58
|
+
});
|
|
59
|
+
test('should return true for invalid minimum years', () => {
|
|
60
|
+
const invalidYearsTestConfig = isDateAtLeastYearsAgoYupTest(-1, testMessage);
|
|
61
|
+
const dateString = format(subYears(now, 5), 'yyyy-MM-dd');
|
|
62
|
+
expect(invalidYearsTestConfig.test.call(mockContext, dateString, mockContext)).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import { isBefore, isEqual,
|
|
1
|
+
import { isBefore, isEqual, startOfDay } from 'date-fns';
|
|
2
|
+
import { getDateObject } from '../../utils/getDateObject/getDateObject.js';
|
|
2
3
|
import { isEmpty } from '../../validators/isEmpty/isEmpty.js';
|
|
3
4
|
import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
|
|
5
|
+
import { isString } from '../../validators/isString/isString.js';
|
|
6
|
+
import { isValidDateInstance } from '../../validators/isValidDateInstance/isValidDateInstance.js';
|
|
7
|
+
const toDate = (input) => {
|
|
8
|
+
if (input instanceof Date)
|
|
9
|
+
return isValidDateInstance(input) ? input : null;
|
|
10
|
+
return isString(input) ? getDateObject(input) : null;
|
|
11
|
+
};
|
|
4
12
|
export const isDateSameOrAfterYupTest = (maxDate, message) => {
|
|
5
13
|
return {
|
|
6
14
|
name: 'isDateSameOrAfter',
|
|
@@ -15,9 +23,9 @@ export const isDateSameOrAfterYupTest = (maxDate, message) => {
|
|
|
15
23
|
const maxValue = maxDate[0];
|
|
16
24
|
if (isNullOrUndefined(value) || isNullOrUndefined(maxValue))
|
|
17
25
|
return true;
|
|
18
|
-
const valueDate =
|
|
19
|
-
const maxDateValue =
|
|
20
|
-
if (
|
|
26
|
+
const valueDate = toDate(value);
|
|
27
|
+
const maxDateValue = toDate(maxValue);
|
|
28
|
+
if (isNullOrUndefined(valueDate) || isNullOrUndefined(maxDateValue))
|
|
21
29
|
return false;
|
|
22
30
|
const maxDateStartOfDay = startOfDay(maxDateValue);
|
|
23
31
|
return isBefore(valueDate, maxDateStartOfDay) || isEqual(valueDate, maxDateStartOfDay);
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import { isAfter, isEqual,
|
|
1
|
+
import { isAfter, isEqual, startOfDay } from 'date-fns';
|
|
2
|
+
import { getDateObject } from '../../utils/getDateObject/getDateObject.js';
|
|
2
3
|
import { isEmpty } from '../../validators/isEmpty/isEmpty.js';
|
|
3
4
|
import { isNullOrUndefined } from '../../validators/isNullOrUndefined/isNullOrUndefined.js';
|
|
5
|
+
import { isString } from '../../validators/isString/isString.js';
|
|
6
|
+
import { isValidDateInstance } from '../../validators/isValidDateInstance/isValidDateInstance.js';
|
|
7
|
+
const toDate = (input) => {
|
|
8
|
+
if (input instanceof Date)
|
|
9
|
+
return isValidDateInstance(input) ? input : null;
|
|
10
|
+
return isString(input) ? getDateObject(input) : null;
|
|
11
|
+
};
|
|
4
12
|
export const isDateSameOrBeforeYupTest = (minDate, message) => {
|
|
5
13
|
return {
|
|
6
14
|
name: 'isDateSameOrBefore',
|
|
@@ -15,9 +23,9 @@ export const isDateSameOrBeforeYupTest = (minDate, message) => {
|
|
|
15
23
|
const minValue = minDate[0];
|
|
16
24
|
if (isNullOrUndefined(value) || isNullOrUndefined(minValue))
|
|
17
25
|
return true;
|
|
18
|
-
const valueDate =
|
|
19
|
-
const minDateValue =
|
|
20
|
-
if (
|
|
26
|
+
const valueDate = toDate(value);
|
|
27
|
+
const minDateValue = toDate(minValue);
|
|
28
|
+
if (isNullOrUndefined(valueDate) || isNullOrUndefined(minDateValue))
|
|
21
29
|
return false;
|
|
22
30
|
const minDateStartOfDay = startOfDay(minDateValue);
|
|
23
31
|
return isAfter(valueDate, minDateStartOfDay) || isEqual(valueDate, minDateStartOfDay);
|
|
@@ -21,4 +21,10 @@ describe('maxDigitsLengthYupTest', () => {
|
|
|
21
21
|
expect(testConfig.test.call(mockContext, 1234, mockContext)).toBe(true);
|
|
22
22
|
expect(testConfig.test.call(mockContext, 123456, mockContext)).toBe(false);
|
|
23
23
|
});
|
|
24
|
+
test('should return false for negative numbers', () => {
|
|
25
|
+
expect(testConfig.test.call(mockContext, -1234, mockContext)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
test('should return false for decimal numbers', () => {
|
|
28
|
+
expect(testConfig.test.call(mockContext, 12.34, mockContext)).toBe(false);
|
|
29
|
+
});
|
|
24
30
|
});
|
|
@@ -21,4 +21,10 @@ describe('minDigitsLengthYupTest', () => {
|
|
|
21
21
|
expect(testConfig.test.call(mockContext, 123456, mockContext)).toBe(true);
|
|
22
22
|
expect(testConfig.test.call(mockContext, 1234, mockContext)).toBe(false);
|
|
23
23
|
});
|
|
24
|
+
test('should return false for negative numbers', () => {
|
|
25
|
+
expect(testConfig.test.call(mockContext, -12345, mockContext)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
test('should return false for decimal numbers', () => {
|
|
28
|
+
expect(testConfig.test.call(mockContext, 12345.6, mockContext)).toBe(false);
|
|
29
|
+
});
|
|
24
30
|
});
|