From 9f61125653f76ba087adee6f3d059123594d5036 Mon Sep 17 00:00:00 2001 From: sight <26325820+Sight-wcg@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:04:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(input-number):=20=E4=BD=BF=E5=8F=AF?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=AD=97=E7=AC=A6=E6=A3=80=E6=9F=A5=E6=9B=B4?= =?UTF-8?q?=E4=B8=A5=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复 `E/e` 字符之后,可输入多个正负号的问题 2. 修复 `E/e` 字符之后,可输入多个 `.` 字符的问题 3. 修复 `canInputNumber` 参数类型错误 --- js/input-number/number.ts | 7 ++++--- test/unit/input-number/number.test.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/js/input-number/number.ts b/js/input-number/number.ts index a59369e3ce..068da808cb 100644 --- a/js/input-number/number.ts +++ b/js/input-number/number.ts @@ -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; @@ -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; } diff --git a/test/unit/input-number/number.test.js b/test/unit/input-number/number.test.js index 5fe0770ebe..60faee4bc1 100644 --- a/test/unit/input-number/number.test.js +++ b/test/unit/input-number/number.test.js @@ -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);