diff --git a/package-lock.json b/package-lock.json index 213677f..2f2a447 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@satyam-seth/otp-field", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@satyam-seth/otp-field", - "version": "1.0.4", + "version": "1.0.5", "license": "ISC", "devDependencies": { "@types/chai": "^4.3.6", diff --git a/package.json b/package.json index ef2daea..80740d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@satyam-seth/otp-field", - "version": "1.0.4", + "version": "1.0.5", "description": "A configurable OTP field built using TypeScript and SCSS", "repository": { "type": "git", diff --git a/src/ts/utils/regex.ts b/src/ts/utils/regex.ts index 05f8599..25199f8 100644 --- a/src/ts/utils/regex.ts +++ b/src/ts/utils/regex.ts @@ -30,7 +30,7 @@ export function getOTPRegexForValueType(valueType: OTPValueType): RegExp { return /[^a-z0-9]/g; // Match anything except alphanumeric lower characters case OTPValueType.ALPHANUMERIC_UPPER: - return /[^A-Za-z0-9]/g; // Match anything except alphanumeric upper characters + return /[^A-Z0-9]/g; // Match anything except alphanumeric upper characters // throw error for invalid type default: diff --git a/tests/utils/regex.ts b/tests/utils/regex.ts new file mode 100644 index 0000000..2ba3eb6 --- /dev/null +++ b/tests/utils/regex.ts @@ -0,0 +1,46 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { getOTPRegexForValueType, OTPValueType } from '../../src/ts/main'; + +describe('Test getOTPRegexForValueType function', () => { + it('should return valid regex for valid value type', () => { + const numericRegex = getOTPRegexForValueType(OTPValueType.NUMERIC); + expect(numericRegex.toString()).to.equal('/[^0-9]/g'); + + const alphabeticRegex = getOTPRegexForValueType(OTPValueType.ALPHABETIC); + expect(alphabeticRegex.toString()).to.equal('/[^A-Za-z]/g'); + + const alphabeticLowerRegex = getOTPRegexForValueType( + OTPValueType.ALPHABETIC_LOWER + ); + expect(alphabeticLowerRegex.toString()).to.equal('/[^a-z]/g'); + + const alphabeticUpperRegex = getOTPRegexForValueType( + OTPValueType.ALPHABETIC_UPPER + ); + expect(alphabeticUpperRegex.toString()).to.equal('/[^A-Z]/g'); + + const alphanumericRegex = getOTPRegexForValueType( + OTPValueType.ALPHANUMERIC + ); + expect(alphanumericRegex.toString()).to.equal('/[^A-Za-z0-9]/g'); + + const alphanumericLowerRegex = getOTPRegexForValueType( + OTPValueType.ALPHANUMERIC_LOWER + ); + expect(alphanumericLowerRegex.toString()).to.equal('/[^a-z0-9]/g'); + + const alphanumericUpperRegex = getOTPRegexForValueType( + OTPValueType.ALPHANUMERIC_UPPER + ); + expect(alphanumericUpperRegex.toString()).to.equal('/[^A-Z0-9]/g'); + }); + + it('should throw error for invalid value type', () => { + // @ts-ignore + expect(() => getOTPRegexForValueType('invalid')).to.throw( + Error, + 'Invalid OTP field value type' + ); + }); +});