Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/BigIntDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import {
validateNumber,
} from './numberUtil';

export default class BigIntDecimal implements DecimalClass {
origin: string = '';
negative: boolean;
integer: bigint;
decimal: bigint;
/** BigInt will convert `0009` to `9`. We need record the len of decimal */
decimalLen: number;
empty: boolean;
nan: boolean;

class BigIntDecimal implements DecimalClass {
constructor(value: string | number) {
this.origin = '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure type safety and prevent potential runtime errors, it's best to initialize all class properties at the beginning of the constructor. The BigIntDecimal interface defines properties like negative, integer, etc., as non-optional. However, if an empty value is passed to the constructor, these properties are never initialized, leaving them as undefined, which violates the interface contract. Initializing them with default values makes the class more robust.

Suggested change
this.origin = '';
this.origin = '';
this.negative = false;
this.integer = BigInt(0);
this.decimal = BigInt(0);
this.decimalLen = 0;
this.empty = false;
this.nan = false;


if (isEmpty(value)) {
this.empty = true;
return;
Expand Down Expand Up @@ -164,7 +157,7 @@ export default class BigIntDecimal implements DecimalClass {
}

equals(target: DecimalClass) {
return this.toString() === target?.toString();
return this.toString() === (target ? target.toString() : undefined);
}

lessEquals(target: DecimalClass) {
Expand Down Expand Up @@ -192,3 +185,16 @@ export default class BigIntDecimal implements DecimalClass {
).fullStr;
}
}

interface BigIntDecimal {
origin: string;
negative: boolean;
integer: bigint;
decimal: bigint;
/** BigInt will convert `0009` to `9`. We need record the len of decimal */
decimalLen: number;
empty: boolean;
nan: boolean;
}

export default BigIntDecimal;
18 changes: 12 additions & 6 deletions src/NumberDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { getNumberPrecision, isE, isEmpty, num2str } from './numberUtil';
/**
* We can remove this when IE not support anymore
*/
export default class NumberDecimal implements DecimalClass {
origin: string = '';
number: number;
empty: boolean;

class NumberDecimal implements DecimalClass {
constructor(value: ValueType) {
this.origin = '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure type safety and prevent potential runtime errors, it's best to initialize all class properties at the beginning of the constructor. The NumberDecimal interface defines number and empty as non-optional properties. However, if an empty value is passed to the constructor, number is not initialized, leaving it as undefined, which violates the interface contract. Initializing all properties with default values makes the class more robust.

    this.origin = '';
    this.number = 0;
    this.empty = false;


if (isEmpty(value)) {
this.empty = true;
return;
Expand Down Expand Up @@ -90,7 +88,7 @@ export default class NumberDecimal implements DecimalClass {
}

equals(target: DecimalClass) {
return this.toNumber() === target?.toNumber();
return this.toNumber() === (target ? target.toNumber() : undefined);
}

lessEquals(target: DecimalClass) {
Expand All @@ -117,3 +115,11 @@ export default class NumberDecimal implements DecimalClass {
return num2str(this.number);
}
}

interface NumberDecimal {
origin: string;
number: number;
empty: boolean;
}

export default NumberDecimal;
Loading