Ad bnToBn (convenience) (#85)

* Ad bnToBn (convenience)

* Fix flow
This commit is contained in:
Jaco Greeff
2018-02-12 17:50:57 +01:00
committed by GitHub
parent 44952cdc34
commit 950fa51214
7 changed files with 91 additions and 9 deletions
+21 -1
View File
@@ -3,6 +3,7 @@
Utility methods to convert to and from `BN` objects
- [bnFromHex](#bnfromhex) Creates a BN.js bignumber object from a hex string.
- [bnToBn](#bntobn) Creates a BN value from a BN.js bignumber or number input.
- [bnToHex](#bntohex) Creates a hex value from a BN.js bignumber object.
- [bnToU8a](#bntou8a) Creates a Uint8Array object from a BN.
@@ -18,6 +19,25 @@ bnFromHex (value?: string): BN
## bnToBn
Creates a BN value from a BN.js bignumber or number input.
```js
bnToBn (value?: BN : number): BN
```
`null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation.
```js
import BN from 'bn.js';
import { bnToBn } from '@polkadot/util';
bnToBn(0x1234); // => BN(0x1234)
bnToBn(new BN(0x1234)); // => BN(0x1234)
```
## bnToHex
Creates a hex value from a BN.js bignumber object.
@@ -41,7 +61,7 @@ bnToHex(new BN(0x123456)); // => '0x123456'
Creates a Uint8Array object from a BN.
```js
bnToU8a (value?: BN): Uint8Array
bnToU8a (value?: BN | number, bitLength: number = -1): Uint8Array
```
+2
View File
@@ -4,6 +4,7 @@
// @flow
const bnFromHex = require('./fromHex');
const bnToBn = require('./toBn');
const bnToHex = require('./toHex');
const bnToU8a = require('./toU8a');
@@ -12,6 +13,7 @@ const bnToU8a = require('./toU8a');
*/
module.exports = {
bnFromHex,
bnToBn,
bnToHex,
bnToU8a
};
+31
View File
@@ -0,0 +1,31 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const BN = require('bn.js');
/**
@name bnToBn
@signature bnToBn (value?: BN : number): BN
@summary Creates a BN value from a BN.js bignumber or number input.
@description
`null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation.
@example
import BN from 'bn.js';
import { bnToBn } from '@polkadot/util';
bnToBn(0x1234); // => BN(0x1234)
bnToBn(new BN(0x1234)); // => BN(0x1234)
*/
module.exports = function bnToBn (value?: BN | number): BN {
// flowlint-next-line sketchy-null-number:off
if (!value) {
return new BN(0);
}
return BN.isBN(value)
// $FlowFixMe type is determined
? value
: new BN(value);
};
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const BN = require('bn.js');
const { bnToBn } = require('./index');
describe('bnToBn', () => {
it('converts null values to 0x00', () => {
expect(
bnToBn(null)
).toEqual(new BN(0));
});
it('converts BN values to BN', () => {
expect(
bnToBn(new BN(128))
).toEqual(new BN(128));
});
it('converts number values to BN', () => {
expect(
bnToBn(128)
).toEqual(new BN(128));
});
});
+4 -2
View File
@@ -6,6 +6,7 @@
const BN = require('bn.js');
const hexFixLength = require('../hex/fixLength');
const bnToBn = require('./toBn');
const ZERO_STR = '0x00';
@@ -21,10 +22,11 @@ const ZERO_STR = '0x00';
bnToHex(new BN(0x123456)); // => '0x123456'
*/
module.exports = function bnToHex (value?: BN, bitLength: number = -1): string {
module.exports = function bnToHex (value?: BN | number, bitLength: number = -1): string {
// flowlint-next-line sketchy-null-number:off
if (!value) {
return ZERO_STR;
}
return hexFixLength(value.toString(16), bitLength, true);
return hexFixLength(bnToBn(value).toString(16), bitLength, true);
};
+6 -4
View File
@@ -5,12 +5,13 @@
import type BN from 'bn.js';
const bnToHex = require('./toHex');
const hexToU8a = require('../hex/toU8a');
const bnToBn = require('./toBn');
const bnToHex = require('./toHex');
/**
@name bnToU8a
@signature bnToU8a (value?: BN): Uint8Array
@signature bnToU8a (value?: BN | number, bitLength: number = -1): Uint8Array
@summary Creates a Uint8Array object from a BN.
@description
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`.
@@ -19,12 +20,13 @@ const hexToU8a = require('../hex/toU8a');
bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
*/
module.exports = function bnToU8a (value?: BN, bitLength: number = -1): Uint8Array {
module.exports = function bnToU8a (value?: BN | number, bitLength: number = -1): Uint8Array {
// flowlint-next-line sketchy-null-number:off
if (!value) {
return new Uint8Array([]);
}
return hexToU8a(
bnToHex(value, bitLength)
bnToHex(bnToBn(value), bitLength)
);
};
-2
View File
@@ -24,7 +24,5 @@ module.exports = function hexToBn (value?: string): BN {
return new BN(0);
}
console.log('value', value, hexStripPrefix(value), hexStripPrefix(value) || '0');
return new BN(hexStripPrefix(value) || '0', 16);
};