Skip to content

Commit

Permalink
修复formatMoney进制数字的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
蔡金锋 committed Jan 12, 2020
1 parent 6fee3a4 commit 0bfad85
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "util-helpers",
"version": "3.3.0",
"version": "3.3.1",
"description": "一个基于业务场景的工具方法库",
"main": "lib/index.js",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions src/formatMoney.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 11 additions & 5 deletions test/processor/formatMoney.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});
});

Expand All @@ -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('输入保留位数', () => {
Expand Down

0 comments on commit 0bfad85

Please sign in to comment.