Ad bitLength to number & BN conversions (#81)

* Typo in docs

* Add bitLength to number & WS conversions

* Update docs
This commit is contained in:
Jaco Greeff
2018-02-12 12:05:36 +01:00
committed by GitHub
parent cc8eece9fe
commit b9f83dd422
12 changed files with 65 additions and 33 deletions
+2 -2
View File
@@ -23,11 +23,11 @@ bnFromHex (value?: string): BN
Creates a hex value from a BN.js bignumber object.
```js
bnToHex (value?: BN): string
bnToHex (value?: BN, bitLength: number = -1): string
```
`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error.
`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. With `bitLength` set, it fixes the number to the specified length.
```js
import BN from 'bn.js';
+1 -1
View File
@@ -40,7 +40,7 @@ hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = fals
```
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`.
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter values are padded with `0`.
```js
import { hexFixLength } from '@polkadot/util';
+5 -4
View File
@@ -58,12 +58,13 @@ numberToHex (value?: number): string
```
`null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`.
`null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`. With `bitLength` set, it converts the number to the equivalent size.
```js
import { numberToHex } from '@polkadot/util';
const hex = numberToHex(0x1234); // => '0x1234'
numberToHex(0x1234); // => '0x1234'
numberToHex(0x1234, 32) // => 0x00001234
```
## numberToU8a
@@ -71,11 +72,11 @@ const hex = numberToHex(0x1234); // => '0x1234'
Creates a Uint8Array object from a number.
```js
numberToBuffer (value?: number): Uint8Array
numberToBuffer (value?: number, bitLenght: number = -1): Uint8Array
```
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`.
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`. With `bitLength`, it converts the value to the equivalent size.
```js
import { numberToU8a } from '@polkadot/util';
+5 -5
View File
@@ -5,26 +5,26 @@
const BN = require('bn.js');
const hexAddPrefix = require('../hex/addPrefix');
const hexFixLength = require('../hex/fixLength');
const ZERO_STR = '0x00';
/**
@name bnToHex
@signature bnToHex (value?: BN): string
@signature bnToHex (value?: BN, bitLength: number = -1): string
@summary Creates a hex value from a BN.js bignumber object.
@description
`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error.
`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. With `bitLength` set, it fixes the number to the specified length.
@example
import BN from 'bn.js';
import { bnToHex } from '@polkadot/util';
bnToHex(new BN(0x123456)); // => '0x123456'
*/
module.exports = function bnToHex (value?: BN): string {
module.exports = function bnToHex (value?: BN, bitLength: number = -1): string {
if (!value) {
return ZERO_STR;
}
return hexAddPrefix(value.toString(16));
return hexFixLength(value.toString(16), bitLength, true);
};
+9 -3
View File
@@ -7,15 +7,21 @@ const BN = require('bn.js');
const { bnToHex } = require('./index');
describe('bnToHex', () => {
it('converts null values to 0x00', () => {
expect(
bnToHex(null)
).toEqual('0x00');
});
it('converts BN values to a prefixed hex representation', () => {
expect(
bnToHex(new BN(128))
).toEqual('0x80');
});
it('converts null values to 0x00', () => {
it('converts BN values to a prefixed hex representation (bitLength)', () => {
expect(
bnToHex(null)
).toEqual('0x00');
bnToHex(new BN(128), 16)
).toEqual('0x0080');
});
});
+7 -3
View File
@@ -5,6 +5,9 @@
import type BN from 'bn.js';
const bnToHex = require('./toHex');
const hexToU8a = require('../hex/toU8a');
/**
@name bnToU8a
@signature bnToU8a (value?: BN): Uint8Array
@@ -16,11 +19,12 @@ import type BN from 'bn.js';
bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
*/
module.exports = function bnToU8a (value?: BN): Uint8Array {
module.exports = function bnToU8a (value?: BN, bitLength: number = -1): Uint8Array {
if (!value) {
return new Uint8Array([]);
}
// $FlowFixMe toArray doesn't need params
return new Uint8Array(value.toArray());
return hexToU8a(
bnToHex(value, bitLength)
);
};
+9 -3
View File
@@ -7,15 +7,21 @@ const BN = require('bn.js');
const { bnToU8a } = require('./index');
describe('bnToU8a', () => {
it('converts null values to 0x00', () => {
expect(
bnToU8a(null)
).toEqual(new Uint8Array([]));
});
it('converts BN values to a prefixed hex representation', () => {
expect(
bnToU8a(new BN(0x123456))
).toEqual(new Uint8Array([0x12, 0x34, 0x56]));
});
it('converts null values to 0x00', () => {
it('converts BN values to a prefixed hex representation (bitLength)', () => {
expect(
bnToU8a(null)
).toEqual(new Uint8Array([]));
bnToU8a(new BN(0x123456), 32)
).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56]));
});
});
+1 -1
View File
@@ -12,7 +12,7 @@ const { ZEROS_256 } = require('./constants');
@signature hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string
@summary Shifts a hex string to a specific bitLength
@description
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`.
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter values are padded with `0`.
@example
import { hexFixLength } from '@polkadot/util';
+9 -6
View File
@@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
const hexAddPrefix = require('../hex/addPrefix');
const hexFixLength = require('../hex/fixLength');
const isNull = require('../is/null');
const isUndefined = require('../is/undefined');
@@ -12,18 +12,21 @@ const isUndefined = require('../is/undefined');
@signature numberToHex (value?: number): string
@summary Creates a hex value from a number.
@description
`null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`.
`null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`. With `bitLength` set, it converts the number to the equivalent size.
@example
import { numberToHex } from '@polkadot/util';
const hex = numberToHex(0x1234); // => '0x1234'
numberToHex(0x1234); // => '0x1234'
numberToHex(0x1234, 32) // => 0x00001234
*/
module.exports = function numberToHex (value?: number): string {
module.exports = function numberToHex (value?: number, bitLength: number = -1): string {
if (isUndefined(value) || isNull(value) || isNaN(value)) {
return '0x';
}
return hexAddPrefix(
(value || 0).toString(16)
return hexFixLength(
(value || 0).toString(16),
bitLength,
true
);
};
+6
View File
@@ -34,4 +34,10 @@ describe('numberToHex', () => {
numberToHex(0x1245)
).toEqual('0x1245');
});
it('converts number to hex (specfified bitLength)', () => {
expect(
numberToHex(0x1245, 32)
).toEqual('0x00001245');
});
});
+4 -4
View File
@@ -8,22 +8,22 @@ const numberToHex = require('./toHex');
/**
@name numberToU8a
@signature numberToBuffer (value?: number): Uint8Array
@signature numberToBuffer (value?: number, bitLenght: number = -1): Uint8Array
@summary Creates a Uint8Array object from a number.
@description
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`.
`null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`. With `bitLength`, it converts the value to the equivalent size.
@example
import { numberToU8a } from '@polkadot/util';
numberToU8a(0x1234); // => [0x12, 0x34]
*/
module.exports = function numberToU8a (value?: number): Uint8Array {
module.exports = function numberToU8a (value?: number, bitLength: number = -1): Uint8Array {
// flowlint-next-line sketchy-null-number:off
if (!value || isNaN(value)) {
return new Uint8Array([]);
}
return hexToU8a(
numberToHex(value)
numberToHex(value, bitLength)
);
};
+7 -1
View File
@@ -29,9 +29,15 @@ describe('numberToU8a', () => {
).toEqual(new Uint8Array([]));
});
it('converts values to the buffer', () => {
it('converts values to the u8a', () => {
expect(
numberToU8a(0x3456)
).toEqual(new Uint8Array([0x34, 0x56]));
});
it('converts values to the u8a (bitLength)', () => {
expect(
numberToU8a(0x3456, 32)
).toEqual(new Uint8Array([0x00, 0x00, 0x34, 0x56]));
});
});