Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input-number): 使可输入字符检查更严格 #2022

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions js/input-number/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const specialCode = ['-', '.', 'e', 'E', '+'];
* 1.23E+08 就表示 1.23 乘 10 的 8 次方
* 2e3 表示 2 乘 10 的 3 次方
*/
export function canInputNumber(number: string, largeNumber: boolean) {
export function canInputNumber(number: string | undefined | null, largeNumber?: boolean) {
if (['', null, undefined].includes(number)) return true;
// 数字最前方不允许出现连续的两个 0
if (number.slice(0, 2) === '00') return false;
Expand All @@ -218,11 +218,12 @@ export function canInputNumber(number: string, largeNumber: boolean) {
if (number.match(/e/g)?.length > 1) return false;
// 只能出现一个负号(-)或 一个正号(+),并且在第一个位置;但允许 3e+10 这种形式
const tmpNumber = number.slice(1);
if (/(\+|-)/.test(tmpNumber) && !/e+/i.test(tmpNumber)) return false;
const tmpMatched = tmpNumber.match(/(\+|-)/g);
if (tmpMatched && (!/e(\+|-)/i.test(tmpNumber) || tmpMatched.length > 1)) return false;
// 允许输入数字字符
const isNumber = (largeNumber && isInputNumber(number)) || !Number.isNaN(Number(number));
if (!isNumber && !specialCode.includes(number.slice(-1))) return false;
if (/e/i.test(number) && !/\de/i.test(number)) return false;
if (/e/i.test(number) && (!/\de/i.test(number) || /e\./.test(number))) return false;
return true;
}

Expand Down
23 changes: 23 additions & 0 deletions test/unit/input-number/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,40 @@ describe('canInputNumber', () => {
expect(canInputNumber('1.3.')).toBe(false);
expect(canInputNumber('.1.3')).toBe(false);
expect(canInputNumber('.1.3.')).toBe(false);
expect(canInputNumber('1.2e.')).toBe(false);
expect(canInputNumber('1.2E.')).toBe(false);
});

it('normal number: number letters are allowed', () => {
expect(canInputNumber('-')).toBe(true);
expect(canInputNumber('1.3e')).toBe(true);
expect(canInputNumber('1.22+')).toBe(false);
expect(canInputNumber('1.22-')).toBe(false);
expect(canInputNumber('+1.22+')).toBe(false);
expect(canInputNumber('+1.22-')).toBe(false);
expect(canInputNumber('-1.22+')).toBe(false);
expect(canInputNumber('-1.22-')).toBe(false);
expect(canInputNumber('+1.22+++')).toBe(false);
expect(canInputNumber('1.23E')).toBe(true);
expect(canInputNumber('1.23E+')).toBe(true);
expect(canInputNumber('1.23E+08')).toBe(true);
expect(canInputNumber('+1.23E+08')).toBe(true);
expect(canInputNumber('+1.23E-08')).toBe(true);
expect(canInputNumber('1.23E++')).toBe(false);
expect(canInputNumber('+1.23E++')).toBe(false);
expect(canInputNumber('+1.23E--')).toBe(false);
expect(canInputNumber('1.23E++08')).toBe(false);
expect(canInputNumber('1.23E-')).toBe(true);
expect(canInputNumber('1.23E-02')).toBe(true);
expect(canInputNumber('-1.23E+02')).toBe(true);
expect(canInputNumber('-1.23E-02')).toBe(true);
expect(canInputNumber('1.23E--')).toBe(false);
expect(canInputNumber('-1.23E--')).toBe(false);
expect(canInputNumber('-1.23E++')).toBe(false);
expect(canInputNumber('1.23E--02')).toBe(false);
expect(canInputNumber('2e')).toBe(true);
expect(canInputNumber('2e3')).toBe(true);
expect(canInputNumber('2e.')).toBe(false);
expect(canInputNumber('1.')).toBe(true);
expect(canInputNumber('1.2E')).toBe(true);
expect(canInputNumber('--')).toBe(false);
Expand Down
Loading