Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: now bn accepts null #523

Merged
merged 2 commits into from
Sep 27, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/brown-teachers-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/math": patch
---

now `bn` accepts `null`
6 changes: 4 additions & 2 deletions packages/math/src/bn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,16 @@ describe('Math - BN', () => {
expect(() => bn(over).toHex(4)).toThrow();
});

it('should create bn with number or undefined', () => {
const inputs: { numb: number; str: string; undef?: string } = {
it('should create bn with number or undefined or null', () => {
const inputs: { numb: number; str: string; undef?: string; nil: null } = {
numb: 2,
str: '5',
nil: null,
};

expect(bn().toNumber()).toEqual(0);
expect(bn(inputs?.undef).toNumber()).toEqual(0);
expect(bn(inputs?.nil).toNumber()).toEqual(0);
expect(bn(inputs?.numb).toNumber()).toEqual(2);
expect(bn(inputs?.str).toNumber()).toEqual(5);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/math/src/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface BNHiddenTypes {
type BNInputOverridesKeys = keyof BNInputOverrides;

export class BN extends BnJs implements BNInputOverrides, BNHiddenTypes, BNHelper, BNOverrides {
constructor(value?: BNInput, base?: number | 'hex', endian?: BnJs.Endianness) {
constructor(value?: BNInput | null, base?: number | 'hex', endian?: BnJs.Endianness) {
if (BN.isBN(value)) {
super(value.toArray(), base, endian);
return;
Expand Down Expand Up @@ -251,7 +251,7 @@ export class BN extends BnJs implements BNInputOverrides, BNHiddenTypes, BNHelpe
}

// functional shortcut to create BN
export const bn = (value?: BNInput, base?: number | 'hex', endian?: BnJs.Endianness) =>
export const bn = (value?: BNInput | null, base?: number | 'hex', endian?: BnJs.Endianness) =>
Copy link
Member

Choose a reason for hiding this comment

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

Should we change the type BNInput?

Copy link
Contributor Author

@LuizAsFight LuizAsFight Sep 27, 2022

Choose a reason for hiding this comment

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

I think no. a lot of helper methods use BNInput but shouldn't accept a null/undefined as values.

for example:
bn(10).mul() should not work. It's needed to explicitly say the value I want to multiply to mul method, which has BNInput as input typing

new BN(value, base, endian);

bn.parseUnits = (value: string, units: number = DECIMAL_UNITS): BN => {
Expand Down