Skip to content

Commit

Permalink
Allow base-10 to be passed into BigNumbner.toString and improve error…
Browse files Browse the repository at this point in the history
…s for other radices (#1164).
  • Loading branch information
ricmoo committed Nov 22, 2020
1 parent 29f6c34 commit c8bb77d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/bignumber/src.ts/bignumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export function isBigNumberish(value: any): value is BigNumberish {
);
}

// Only warn about passing 10 into radix once
let _warnedToStringRadix = false;

export class BigNumber implements Hexable {
readonly _hex: string;
readonly _isBigNumber: boolean;
Expand Down Expand Up @@ -188,9 +191,18 @@ export class BigNumber implements Hexable {
}

toString(): string {
// Lots of people expect this, which we do not support, so check
if (arguments.length !== 0) {
logger.throwError("bigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, { });
// Lots of people expect this, which we do not support, so check (See: #889)
if (arguments.length > 0) {
if (arguments[0] === 10) {
if (!_warnedToStringRadix) {
_warnedToStringRadix = true;
logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
}
} else if (arguments[0] === 16) {
logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, { });
} else {
logger.throwError("BigNumber.toString does not accept parametes", Logger.errors.UNEXPECTED_ARGUMENT, { });
}
}
return toBN(this).toString(10);
}
Expand Down

0 comments on commit c8bb77d

Please sign in to comment.