From be398934d66695ffc43ad0d9e9ca0c2bc302ca95 Mon Sep 17 00:00:00 2001 From: Roc Date: Thu, 9 May 2024 16:10:15 +0800 Subject: [PATCH] fix: the situation of --- __tests__/toPercent.test.js | 1 + docs/CHANGELOG/changelog.md | 6 ++++++ package.json | 2 +- src/toFixed.ts | 3 ++- src/toPercent.ts | 4 +++- src/toSlice.ts | 3 ++- src/toUnit.ts | 3 ++- src/utils/index.ts | 4 ++++ 8 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/utils/index.ts diff --git a/__tests__/toPercent.test.js b/__tests__/toPercent.test.js index 1326119..9c69d1d 100644 --- a/__tests__/toPercent.test.js +++ b/__tests__/toPercent.test.js @@ -5,5 +5,6 @@ test('toPercent', () => { expect(toPercent(65, { precision: 2 })).toBe('65.00%'); expect(toPercent(0.651525, { multiply: 100, precision: 3 })).toBe('65.152%'); expect(toPercent(NaN)).toBe('--'); + expect(toPercent(null)).toBe('--'); expect(toPercent(NaN, { placeholder: 'xxxx' })).toBe('xxxx'); }) \ No newline at end of file diff --git a/docs/CHANGELOG/changelog.md b/docs/CHANGELOG/changelog.md index c2a417c..491bf67 100644 --- a/docs/CHANGELOG/changelog.md +++ b/docs/CHANGELOG/changelog.md @@ -6,6 +6,12 @@ nav: ## v4 +### v4.2.1 + +`2024-05-09` + +- fix: the situation of `Number(null) = 0` + ### v4.2.0 `2024-05-09` diff --git a/package.json b/package.json index abb6c23..b653241 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dz-web/o-orange", - "version": "4.2.0", + "version": "4.2.1", "description": "A javascript utility library", "main": "./dist/esm/index.js", "types": "dist/esm/index.d.ts", diff --git a/src/toFixed.ts b/src/toFixed.ts index 6d5b73b..3791693 100644 --- a/src/toFixed.ts +++ b/src/toFixed.ts @@ -2,6 +2,7 @@ import Big from 'big.js'; import isTrue from './isTrue'; import { stringNumber } from './types/common/type'; import orange, { RoundingMode } from './orange'; +import { toNumber } from './utils'; export interface ToFixedOption { placeholder?: string; @@ -25,7 +26,7 @@ function toFixed(num: stringNumber, option: ToFixedOption = {}): string { RM = orange.RM, } = option; if (!isTrue(num)) return placeholder; - const pureNum = Number(num); + const pureNum = toNumber(num); if (isNaN(pureNum) || !isFinite(pureNum)) return placeholder; diff --git a/src/toPercent.ts b/src/toPercent.ts index d035b90..c29c514 100644 --- a/src/toPercent.ts +++ b/src/toPercent.ts @@ -1,6 +1,8 @@ import orange from './orange'; import toFixed from './toFixed'; import { ToFixedOption } from './toFixed'; +import { toNumber } from './utils'; + interface toPercentOption extends ToFixedOption { multiply?: number; } @@ -15,7 +17,7 @@ interface toPercentOption extends ToFixedOption { function toPercent(num: number, option: toPercentOption = {}): string { const { multiply = 1, ...rest } = option; const { placeholder = orange.placeholder } = rest; - const pureNum: number = Number(num); + const pureNum = toNumber(num); if (isNaN(pureNum)) return placeholder; diff --git a/src/toSlice.ts b/src/toSlice.ts index 9ea4293..c66847a 100644 --- a/src/toSlice.ts +++ b/src/toSlice.ts @@ -2,6 +2,7 @@ import Big from 'big.js'; import toFixed from './toFixed'; import isTrue from './isTrue'; import orange, { RoundingMode } from './orange'; +import { toNumber } from './utils'; interface IOption { placeholder?: string; @@ -21,7 +22,7 @@ function toSlice( ): string { const { placeholder = orange.placeholder, precision = orange.precision, ignoreIntegerPrecision = false } = option; if (!isTrue(num)) return placeholder; - const pureNum = Number(num); + const pureNum = toNumber(num); if (isNaN(pureNum)) return placeholder; diff --git a/src/toUnit.ts b/src/toUnit.ts index b5634fd..10aa992 100644 --- a/src/toUnit.ts +++ b/src/toUnit.ts @@ -1,5 +1,6 @@ import orange, { Lang } from './orange'; import toFixed, { ToFixedOption } from './toFixed'; +import { toNumber } from './utils'; export interface Unit { label: string; @@ -44,7 +45,7 @@ function toUnit(num: number | string, options: toUnitOptions = {}): string { ignoreIntegerPrecision = true, } = rest; - const pureNum: number = Number(num); + const pureNum: number = toNumber(num); if (isNaN(pureNum)) return placeholder; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..9e0b41e --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,4 @@ +// Reason: number(null) = 0 +export const toNumber = (target: any): number => { + return target === null ? NaN : Number(target); +};