@mmb-digital/shared-libs-auto 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -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/hooks/useDebounce/useDebounce.test.js +2 -1
- package/dist/hooks/usePreviousValue/usePreviousValue.js +1 -1
- package/dist/hooks/usePreviousValue/usePreviousValue.test.js +1 -1
- package/dist/hooks/useToggle/useToggle.test.js +19 -7
- package/dist/utils/arrayBufferToBase64/arrayBufferToBase64.js +1 -1
- package/dist/utils/assertions/assertNonNullish.d.ts +4 -0
- package/dist/utils/assertions/assertNonNullish.js +10 -0
- package/dist/utils/assertions/assertNonNullish.test.d.ts +1 -0
- package/dist/utils/assertions/assertNonNullish.test.js +19 -0
- package/dist/utils/assertions/assertValue.d.ts +5 -0
- package/dist/utils/assertions/assertValue.js +7 -0
- package/dist/utils/assertions/assertValue.test.d.ts +1 -0
- package/dist/utils/assertions/assertValue.test.js +27 -0
- 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 +1 -1
- 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 +5 -0
- package/dist/utils/index.js +5 -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/openFile/openFile.test.js +1 -1
- 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 +1 -0
- 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 +2 -0
- package/dist/yup/index.js +2 -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/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.d.ts +6 -0
- package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.js +7 -0
- package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.test.d.ts +1 -0
- package/dist/yup/yupNumberEmptyTransform/yupNumberEmptyTransform.test.js +24 -0
- package/package.json +27 -29
|
@@ -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);
|
|
@@ -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
|
};
|
|
@@ -3,6 +3,7 @@ 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);
|
|
@@ -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,7 @@ 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';
|
|
14
|
+
export { yupNumberEmptyTransform } from './yupNumberEmptyTransform/yupNumberEmptyTransform.js';
|
package/dist/yup/index.js
CHANGED
|
@@ -8,5 +8,7 @@ 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';
|
|
14
|
+
export { yupNumberEmptyTransform } from './yupNumberEmptyTransform/yupNumberEmptyTransform.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 {};
|