Add u8aBn (#82)
This commit is contained in:
@@ -8,6 +8,7 @@ Utility methods to convert to and from `Uint8Array` objects
|
||||
- [u8aFromHex](#u8afromhex) Creates a Uint8Array value from a hex string.
|
||||
- [u8aFromString](#u8afromstring) Creates a Uint8Array object from a string.
|
||||
- [u8aFromUtf8](#u8afromutf8) Creates a Uint8Array object from a utf-8 string.
|
||||
- [u8aToBn](#u8atobn) Creates a BN from a Uint8Array object.
|
||||
- [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string.
|
||||
- [u8aToHex](#u8atohex) Creates a hex string from a Uint8Array object.
|
||||
- [u8aToUtf8](#u8atoutf8) Creates a utf-8 string from a Uint8Array object.
|
||||
@@ -102,6 +103,23 @@ import { u8aFromUtf8 } from '@polkadot/util';
|
||||
u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
|
||||
```
|
||||
|
||||
## u8aToBn
|
||||
|
||||
Creates a BN from a Uint8Array object.
|
||||
|
||||
```js
|
||||
u8aToHex (value?: Uint8Array): BN
|
||||
```
|
||||
|
||||
|
||||
`UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
|
||||
|
||||
```js
|
||||
import { u8aToBn } from '@polkadot/util';
|
||||
|
||||
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
```
|
||||
|
||||
## u8aToBuffer
|
||||
|
||||
Creates a Buffer object from a hex string.
|
||||
|
||||
@@ -7,8 +7,6 @@ const BN = require('bn.js');
|
||||
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
const ZERO_BN = new BN(0);
|
||||
|
||||
/**
|
||||
@name hexToBn
|
||||
@signature hexToBn (value?: string): BN
|
||||
@@ -23,8 +21,10 @@ const ZERO_BN = new BN(0);
|
||||
module.exports = function hexToBn (value?: string): BN {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return ZERO_BN;
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
return new BN(hexStripPrefix(value), 16);
|
||||
console.log('value', value, hexStripPrefix(value), hexStripPrefix(value) || '0');
|
||||
|
||||
return new BN(hexStripPrefix(value) || '0', 16);
|
||||
};
|
||||
|
||||
@@ -7,12 +7,10 @@ const BN = require('bn.js');
|
||||
const { hexToBn } = require('./index');
|
||||
|
||||
describe('hexToBn', () => {
|
||||
const value = '80';
|
||||
|
||||
it('converts prefixed hex values to BN', () => {
|
||||
expect(
|
||||
hexToBn(`0x${value}`).eq(
|
||||
new BN(value, 16)
|
||||
hexToBn(0x81).eq(
|
||||
new BN(0x81)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
@@ -24,4 +22,12 @@ describe('hexToBn', () => {
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts 0x values to BN(0)', () => {
|
||||
expect(
|
||||
hexToBn('0x').eq(
|
||||
new BN(0)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
isHex('0x1234', 8); // => false
|
||||
*/
|
||||
module.exports = function isHex (value: string, bitLength: number = -1): boolean {
|
||||
const isValidHex = isString(value) && HEX_REGEX.test(value);
|
||||
const isValidHex = value === '0x' || (isString(value) && HEX_REGEX.test(value));
|
||||
|
||||
if (isValidHex && bitLength !== -1) {
|
||||
return value.length === (2 + bitLength / 4);
|
||||
|
||||
@@ -7,6 +7,12 @@ const { isHex } = require('./index');
|
||||
describe('isHex', () => {
|
||||
const test = '123abcd45';
|
||||
|
||||
it('returns true on 0x hex values', () => {
|
||||
expect(
|
||||
isHex('0x')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on hex values', () => {
|
||||
expect(
|
||||
isHex(`0x${test}`)
|
||||
|
||||
@@ -9,6 +9,7 @@ const u8aFromBuffer = require('./fromBuffer');
|
||||
const u8aFromHex = require('./fromHex');
|
||||
const u8aFromUtf8 = require('./fromUtf8');
|
||||
const u8aFromString = require('./fromString');
|
||||
const u8aToBn = require('./toBn');
|
||||
const u8aToBuffer = require('./toBuffer');
|
||||
const u8aToHex = require('./toHex');
|
||||
const u8aToUtf8 = require('./toUtf8');
|
||||
@@ -23,6 +24,7 @@ module.exports = {
|
||||
u8aFromHex,
|
||||
u8aFromString,
|
||||
u8aFromUtf8,
|
||||
u8aToBn,
|
||||
u8aToBuffer,
|
||||
u8aToHex,
|
||||
u8aToUtf8
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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');
|
||||
|
||||
const hexToBn = require('../hex/toBn');
|
||||
const u8aToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@name u8aToBn
|
||||
@signature u8aToHex (value?: Uint8Array): BN
|
||||
@summary Creates a BN from a Uint8Array object.
|
||||
@description
|
||||
`UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
|
||||
@example
|
||||
import { u8aToBn } from '@polkadot/util';
|
||||
|
||||
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
*/
|
||||
module.exports = function u8aToBn (value?: Uint8Array): BN {
|
||||
return hexToBn(
|
||||
u8aToHex(value)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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 { u8aToBn } = require('./index');
|
||||
|
||||
describe('u8aToBn', () => {
|
||||
it('converts values', () => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([0x12, 0x34])
|
||||
).toString(16)
|
||||
).toEqual('1234');
|
||||
});
|
||||
|
||||
it('converts empty', () => {
|
||||
expect(
|
||||
u8aToBn(
|
||||
new Uint8Array([])
|
||||
).toString(16)
|
||||
).toEqual('0');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user