Skip to content

Commit

Permalink
fix: the situation of
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitusun committed May 9, 2024
1 parent 2b98798 commit be39893
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions __tests__/toPercent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
6 changes: 6 additions & 0 deletions docs/CHANGELOG/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/toFixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion src/toPercent.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/toSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/toUnit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import orange, { Lang } from './orange';
import toFixed, { ToFixedOption } from './toFixed';
import { toNumber } from './utils';

export interface Unit {
label: string;
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Reason: number(null) = 0
export const toNumber = (target: any): number => {
return target === null ? NaN : Number(target);
};

0 comments on commit be39893

Please sign in to comment.