-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #611 from vtex/fix/shouldShowNumberKeyboard-myAccount
Fix: shouldShowNumberKeyboard logic
- Loading branch information
Showing
6 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Removes non-numeric characters from a string, keeping only digits, spaces, and dashes. | ||
* @param {string} string | ||
* @returns {string} | ||
*/ | ||
function removeNonWords(string) { | ||
return (string || '').replace(/[^\d\s-]/g, '') | ||
} | ||
|
||
/** | ||
* Determines whether to show the number keyboard based on the input mask. | ||
* @param {string|number|null|undefined|NaN} [mask] | ||
* @returns {boolean} | ||
*/ | ||
export function shouldShowNumberKeyboard(mask) { | ||
if (mask === undefined || mask === null || mask === '') { | ||
return true | ||
} | ||
|
||
if (Number.isNaN(mask)) { | ||
return false | ||
} | ||
|
||
const maskString = typeof mask === 'number' ? mask.toString() : mask | ||
|
||
const numericString = removeNonWords(maskString) | ||
const isPurelyNumeric = /^[\d\s-]+$/.test(numericString) | ||
|
||
return isPurelyNumeric && !/[a-zA-Z]/.test(maskString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { shouldShowNumberKeyboard } from './shouldShowNumberKeyboard' | ||
|
||
describe('shouldShowNumberKeyboard', () => { | ||
test('Should return true for mask rule undefined', () => { | ||
expect(shouldShowNumberKeyboard(undefined)).toBe(true) | ||
}) | ||
|
||
test('Should return true for mask rule null', () => { | ||
expect(shouldShowNumberKeyboard(null)).toBe(true) | ||
}) | ||
|
||
test('Should return true for mask rule empty string', () => { | ||
expect(shouldShowNumberKeyboard('')).toBe(true) | ||
}) | ||
|
||
test('Should return true for mask rule numeric string without separators', () => { | ||
expect(shouldShowNumberKeyboard('9999')).toBe(true) | ||
}) | ||
|
||
test('Should return true for mask rule numeric string with spaces', () => { | ||
expect(shouldShowNumberKeyboard('999 99')).toBe(true) | ||
}) | ||
|
||
test('Should return true for mask rule numeric string with dashes', () => { | ||
expect(shouldShowNumberKeyboard('9999-99')).toBe(true) | ||
}) | ||
|
||
test('Should return false for mask rulee for string with letters', () => { | ||
expect(shouldShowNumberKeyboard('999AA')).toBe(false) | ||
}) | ||
|
||
test('Should return false for mask rulee for string with mixed letters and spaces', () => { | ||
expect(shouldShowNumberKeyboard('999 AA')).toBe(false) | ||
}) | ||
|
||
test('Should return false for mask rulee for mixed numeric and letter string with dashes', () => { | ||
expect(shouldShowNumberKeyboard('9AA9-99')).toBe(false) | ||
}) | ||
|
||
test('Should return false for mask rulee for NaN', () => { | ||
expect(shouldShowNumberKeyboard(NaN)).toBe(false) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.