diff --git a/package.json b/package.json index 93d411e6..b0a4c637 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "util-helpers", - "version": "3.3.0", + "version": "3.3.1", "description": "一个基于业务场景的工具方法库", "main": "lib/index.js", "scripts": { diff --git a/src/formatMoney.js b/src/formatMoney.js index 49b7a7c5..5184d92a 100644 --- a/src/formatMoney.js +++ b/src/formatMoney.js @@ -1,15 +1,15 @@ -import { checkBoundary, scientificToNumber } from './utils/math.util'; +import { checkBoundary, scientificToNumber, isScientificNumber } from './utils/math.util'; import isNaN from './utils/type/isNaN'; +const reg = /^[+-]?\d*\.?\d*$/; + // 检查数字或数字字符串 function checkNumber(num) { if ( + !(reg.test(num) || isScientificNumber(num)) || isNaN(num) || (typeof num !== 'number' && typeof num !== 'string') || - num === '' || - typeof Number(num) !== 'number' || - num === Infinity || - num === -Infinity + num === '' ) { console.error(`${num} invalid parameter.`); return false; diff --git a/test/processor/formatMoney.test.js b/test/processor/formatMoney.test.js index e35b35c6..23a86f92 100644 --- a/test/processor/formatMoney.test.js +++ b/test/processor/formatMoney.test.js @@ -34,11 +34,8 @@ describe('formatMoney', () => { it('测试溢出最小值 输入-9007199254740992: number, 返回 -9,007,199,254,740,992.00: string', () => { expect(formatMoney('-9007199254740992')).to.be.equal('-9,007,199,254,740,992.00'); }); - it('测试无限大 输入Infinity: number, 返回 \'\' string', () => { - expect(formatMoney(Infinity)).to.be.equal(''); - }); - it('测试无限小 输入-Infinity: number, 返回 \'\' string', () => { - expect(formatMoney(-Infinity)).to.be.equal(''); + it('测试进制数字 输入0x11: string, 返回 \'\' string', () => { + expect(formatMoney('0x11')).to.be.equal(''); }); }); @@ -58,6 +55,15 @@ describe('formatMoney', () => { it('测试科学记数法 输入1.054e+2: number, 返回 1054.20 string', () => { expect(formatMoney(1.054e+2)).to.be.equal('105.40'); }); + it('测试无限大 输入Infinity: number, 返回 \'\' string', () => { + expect(formatMoney(Infinity)).to.be.equal(''); + }); + it('测试无限小 输入-Infinity: number, 返回 \'\' string', () => { + expect(formatMoney(-Infinity)).to.be.equal(''); + }); + it('测试进制数字 输入0x11: number, 返回 17.00 string', () => { + expect(formatMoney(0x11)).to.be.equal('17.00'); + }); }); describe('输入保留位数', () => {