Alias to <-> from (#31)
* bn <-> hex aliasses * Bump minor * Split tests into singular files * to <-> from alias * Add Codeclimate badge
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||

|
||||
[](https://github.com/Flet/semistandard)
|
||||
[](https://www.npmjs.com/package/@polkadot/util)
|
||||
[](https://travis-ci.org/polkadot-js/util)
|
||||
[](https://codeclimate.com/github/polkadot-js/util/maintainability)
|
||||
[](https://coveralls.io/github/polkadot-js/util?branch=master)
|
||||
[](https://greenkeeper.io/)
|
||||
[](https://david-dm.org/polkadot-js/util)
|
||||
|
||||
+2
-7
@@ -2,25 +2,20 @@
|
||||
|
||||
Utility methods to convert to and from `BN` objects
|
||||
|
||||
- [bnFromHex](#bnfromhex) Creates a BN.js bignumber object from a hex string.
|
||||
- [bnFromHex](#bnfromhex) Creates a BN.js bignumber object from a hex string. [hex.md#hextobn](alias hexToBn)
|
||||
- [bnToHex](#bntohex) Creates a hex value from a BN.js bignumber object.
|
||||
|
||||
## bnFromHex
|
||||
|
||||
Creates a BN.js bignumber object from a hex string.
|
||||
Creates a BN.js bignumber object from a hex string. [hex.md#hextobn](alias hexToBn)
|
||||
|
||||
```js
|
||||
bnFromHex (value?: string): BN
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
|
||||
```js
|
||||
import { bnFromHex } from '@polkadot/util';
|
||||
|
||||
console.log('BN object', bnFromHex('0x123480001f'));
|
||||
```
|
||||
|
||||
## bnToHex
|
||||
|
||||
|
||||
+63
-6
@@ -2,26 +2,49 @@
|
||||
|
||||
Utility methods to convert to and from `Buffer` objects
|
||||
|
||||
- [bufferFromHex](#bufferfromhex) Creates a Buffer object from a hex string.
|
||||
- [bufferFromHex](#bufferfromhex) Creates a Buffer object from a hex string. [hex.md#hextobuffer](alias hexToBuffer)
|
||||
- [bufferFromNumber](#bufferfromnumber) Creates a Buffer object from a number. [number.md#numbertobuffer](alias numberToBuffer)
|
||||
- [bufferFromU8a](#bufferfromu8a) Creates a Buffer object from a Uint8Array. [u8a.md#u8atobuffer](alias u8aToBuffer)
|
||||
- [bufferToHex](#buffertohex) Creates a hex value from a Buffer object.
|
||||
- [bufferToNumber](#buffertonumber) Creates a Number value from a Buffer object.
|
||||
- [bufferToU8a](#buffertou8a) Creates a Uint8Array value from a Buffer object.
|
||||
|
||||
## bufferFromHex
|
||||
|
||||
Creates a Buffer object from a hex string.
|
||||
Creates a Buffer object from a hex string. [hex.md#hextobuffer](alias hexToBuffer)
|
||||
|
||||
```js
|
||||
bufferFromHex (value?: string): Buffer
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
|
||||
|
||||
|
||||
## bufferFromNumber
|
||||
|
||||
Creates a Buffer object from a number. [number.md#numbertobuffer](alias numberToBuffer)
|
||||
|
||||
```js
|
||||
import { bufferFromHex } from '@polkadot/util';
|
||||
|
||||
console.log('Buffer object', bufferFromHex('0x123480001f'));
|
||||
bufferFromNumber (value?: number): Buffer
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## bufferFromU8a
|
||||
|
||||
Creates a Buffer object from a Uint8Array. [u8a.md#u8atobuffer](alias u8aToBuffer)
|
||||
|
||||
```js
|
||||
bufferFromU8a (value?: string): Buffer
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## bufferToHex
|
||||
|
||||
Creates a hex value from a Buffer object.
|
||||
@@ -37,4 +60,38 @@ bufferToHex (value?: Buffer): string
|
||||
import { bufferToHex } from '@polkadot/util';
|
||||
|
||||
console.log('Hex value', bufferToHex(Buffer.from([1, 2, 3]));
|
||||
```
|
||||
|
||||
## bufferToNumber
|
||||
|
||||
Creates a Number value from a Buffer object.
|
||||
|
||||
```js
|
||||
bufferToNumber (value?: Buffer): number
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
|
||||
|
||||
```js
|
||||
import { bufferToNumber } from '@polkadot/util';
|
||||
|
||||
bufferToNumber(Buffer.from([12, 34, 56])); // => 0x123456
|
||||
```
|
||||
|
||||
## bufferToU8a
|
||||
|
||||
Creates a Uint8Array value from a Buffer object.
|
||||
|
||||
```js
|
||||
bufferToU8a (value?: Buffer): string
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
|
||||
|
||||
```js
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
bufferToU8a(Buffer.from([1, 2, 3]));
|
||||
```
|
||||
+93
@@ -3,8 +3,14 @@
|
||||
Internal utilities to create and test for hex values
|
||||
|
||||
- [hexAddPrefix](#hexaddprefix) Adds the `0x` prefix to string values.
|
||||
- [hexFromBn](#hexfrombn) Creates a hex value from a BN.js bignumber object. [bn.md#bntohex](alias bnToHex)
|
||||
- [hexFromBn](#hexfrombn) Creates a hex value from a Buffer object. [buffer.md#buffertohex](alias bufferToHex)
|
||||
- [hexFromNumber](#hexfromnumber) Creates a hex value from a number. [number.md#numbertohex](alias numberToHex)
|
||||
- [hexHasPrefix](#hexhasprefix) Tests for the existence of a `0x` prefix.
|
||||
- [hexStripPrefix](#hexstripprefix) Strips any leading `0x` prefix.
|
||||
- [hexToBn](#hextobn) Creates a BN.js bignumber object from a hex string.
|
||||
- [hexToBuffer](#hextobuffer) Creates a Buffer object from a hex string.
|
||||
- [hexToNumber](#hextonumber) Creates a Number value from a Buffer object.
|
||||
|
||||
## hexAddPrefix
|
||||
|
||||
@@ -23,6 +29,42 @@ import { hexAddPrefix } from '@polkadot/util';
|
||||
console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
|
||||
```
|
||||
|
||||
## hexFromBn
|
||||
|
||||
Creates a hex value from a BN.js bignumber object. [bn.md#bntohex](alias bnToHex)
|
||||
|
||||
```js
|
||||
hexFromBn (value?: BN): string
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## hexFromBn
|
||||
|
||||
Creates a hex value from a Buffer object. [buffer.md#buffertohex](alias bufferToHex)
|
||||
|
||||
```js
|
||||
hexFromBuffer (value?: Buffer): string
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## hexFromNumber
|
||||
|
||||
Creates a hex value from a number. [number.md#numbertohex](alias numberToHex)
|
||||
|
||||
```js
|
||||
hexFromNumber (value?: number): string
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## hexHasPrefix
|
||||
|
||||
Tests for the existence of a `0x` prefix.
|
||||
@@ -55,4 +97,55 @@ Tests for the existence of a `0x` prefix, and returns the value without the pref
|
||||
import { hexStripPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
```
|
||||
|
||||
## hexToBn
|
||||
|
||||
Creates a BN.js bignumber object from a hex string.
|
||||
|
||||
```js
|
||||
hexToBn (value?: string): BN
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
|
||||
```js
|
||||
import { hexToBn } from '@polkadot/util';
|
||||
|
||||
console.log('BN object', hexToBn('0x123480001f'));
|
||||
```
|
||||
|
||||
## hexToBuffer
|
||||
|
||||
Creates a Buffer object from a hex string.
|
||||
|
||||
```js
|
||||
hexToBuffer (value?: string): Buffer
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
|
||||
```js
|
||||
import { hexToBuffer } from '@polkadot/util';
|
||||
|
||||
console.log('Buffer object', hexToBuffer('0x123480001f'));
|
||||
```
|
||||
|
||||
## hexToNumber
|
||||
|
||||
Creates a Number value from a Buffer object.
|
||||
|
||||
```js
|
||||
hexToNumber (value?: Hex): number
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
|
||||
```js
|
||||
import { hexToNumber } from '@polkadot/util';
|
||||
|
||||
hexToNumber('0x1234'); // => 0x1234
|
||||
```
|
||||
+4
-14
@@ -2,44 +2,34 @@
|
||||
|
||||
Utility methods to convert to and from `number` values
|
||||
|
||||
- [numberFromBuffer](#numberfrombuffer) Creates a Number value from a Buffer object.
|
||||
- [numberFromHex](#numberfromhex) Creates a Number value from a Buffer object.
|
||||
- [numberFromBuffer](#numberfrombuffer) Creates a Number value from a Buffer object. [buffer.md#buffertonumber](alias bufferToNumber)
|
||||
- [numberFromHex](#numberfromhex) Creates a Number value from a Buffer object. [buffer.md#hextonumber](alias hexToNumber)
|
||||
- [numberToBuffer](#numbertobuffer) Creates a Buffer object from a number.
|
||||
- [numberToHex](#numbertohex) Creates a hex value from a number.
|
||||
|
||||
## numberFromBuffer
|
||||
|
||||
Creates a Number value from a Buffer object.
|
||||
Creates a Number value from a Buffer object. [buffer.md#buffertonumber](alias bufferToNumber)
|
||||
|
||||
```js
|
||||
numberFromBuffer (value?: Buffer): number
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
|
||||
|
||||
```js
|
||||
import { numberFromBuffer } from '@polkadot/util';
|
||||
|
||||
numberFromBuffer(Buffer.from([12, 34, 56])); // => 0x123456
|
||||
```
|
||||
|
||||
## numberFromHex
|
||||
|
||||
Creates a Number value from a Buffer object.
|
||||
Creates a Number value from a Buffer object. [buffer.md#hextonumber](alias hexToNumber)
|
||||
|
||||
```js
|
||||
numberFromHex (value?: Hex): number
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an Nan result, `hex` values return the actual value as a `Number`.
|
||||
|
||||
```js
|
||||
import { numberFromHex } from '@polkadot/util';
|
||||
|
||||
numberFromBuffer('0x1234'); // => 0x1234
|
||||
```
|
||||
|
||||
## numberToBuffer
|
||||
|
||||
|
||||
+3
-8
@@ -2,25 +2,20 @@
|
||||
|
||||
Utility methods to convert to and from `Uint8Array` objects
|
||||
|
||||
- [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object.
|
||||
- [u8aFromBuffer](#u8afrombuffer) Creates a Uint8Array value from a Buffer object. [buffer.md#buffertou8a](alias bufferToU8a)
|
||||
- [u8aToBuffer](#u8atobuffer) Creates a Buffer object from a hex string.
|
||||
|
||||
## u8aFromBuffer
|
||||
|
||||
Creates a Uint8Array value from a Buffer object.
|
||||
Creates a Uint8Array value from a Buffer object. [buffer.md#buffertou8a](alias bufferToU8a)
|
||||
|
||||
```js
|
||||
u8aFromBuffer (value?: Buffer): string
|
||||
u8aFromBuffer (value?: Buffer): number
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
|
||||
|
||||
```js
|
||||
import { u8aFromBuffer } from '@polkadot/util';
|
||||
|
||||
u8aFromBuffer(Buffer.from([1, 2, 3]));
|
||||
```
|
||||
|
||||
## u8aToBuffer
|
||||
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "0.5.20",
|
||||
"version": "0.6.0",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -30,7 +30,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/api-jsonrpc": "^0.5.5",
|
||||
"@polkadot/dev": "^0.9.9"
|
||||
"@polkadot/dev": "^0.10.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.26.0",
|
||||
|
||||
+3
-24
@@ -1,32 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const hexStripPrefix = require('../hex/stripPrefix');
|
||||
const isHex = require('../is/hex');
|
||||
|
||||
const ZERO_BN = new BN(0);
|
||||
const bnFromHex = require('../hex/toBn');
|
||||
|
||||
/**
|
||||
@name bnFromHex
|
||||
@signature bnFromHex (value?: string): BN
|
||||
@summary Creates a BN.js bignumber object from a hex string.
|
||||
@description
|
||||
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { bnFromHex } from '@polkadot/util';
|
||||
|
||||
console.log('BN object', bnFromHex('0x123480001f'));
|
||||
@summary Creates a BN.js bignumber object from a hex string. [hex.md#hextobn](alias hexToBn)
|
||||
*/
|
||||
module.exports = function bnFromHex (value?: string): BN {
|
||||
if (!value) {
|
||||
return ZERO_BN;
|
||||
}
|
||||
|
||||
if (!isHex(value)) {
|
||||
throw new Error(`Cannot convert from non-hex value '${value}' to BN`);
|
||||
}
|
||||
|
||||
return new BN(hexStripPrefix(value), 16);
|
||||
};
|
||||
module.exports = bnFromHex;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bnFromHex } = require('./index');
|
||||
|
||||
describe('bnFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bnFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,53 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { bnFromHex, bnToHex } = require('./index');
|
||||
|
||||
describe('bn', () => {
|
||||
describe('bnFromHex', () => {
|
||||
const value = '80';
|
||||
|
||||
it('converts prefixed hex values to BN', () => {
|
||||
expect(
|
||||
bnFromHex(`0x${value}`).eq(
|
||||
new BN(value, 16)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts null values to BN(0)', () => {
|
||||
expect(
|
||||
bnFromHex(null).eq(
|
||||
new BN(0)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('throws when trying to convert non-BN values', () => {
|
||||
expect(
|
||||
() => bnFromHex('noAHex')
|
||||
).toThrow(/Cannot convert from non-hex/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bnToHex', () => {
|
||||
it('converts BN values to a prefixed hex representation', () => {
|
||||
expect(
|
||||
bnToHex(new BN(128))
|
||||
).toEqual('0x80');
|
||||
});
|
||||
|
||||
it('converts null values to 0x', () => {
|
||||
expect(
|
||||
bnToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('throws when trying to convert non-BN values', () => {
|
||||
expect(
|
||||
() => bnToHex('notABn')
|
||||
).toThrow(/Cannot convert from non-BN/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { bnToHex } = require('./index');
|
||||
|
||||
describe('bnToHex', () => {
|
||||
it('converts BN values to a prefixed hex representation', () => {
|
||||
expect(
|
||||
bnToHex(new BN(128))
|
||||
).toEqual('0x80');
|
||||
});
|
||||
|
||||
it('converts null values to 0x', () => {
|
||||
expect(
|
||||
bnToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('throws when trying to convert non-BN values', () => {
|
||||
expect(
|
||||
() => bnToHex('notABn')
|
||||
).toThrow(/Cannot convert from non-BN/);
|
||||
});
|
||||
});
|
||||
+3
-22
@@ -1,30 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isHex = require('../is/hex');
|
||||
const hexStripPrefix = require('../hex/stripPrefix');
|
||||
|
||||
const EMPTY_BUFFER = Buffer.from([]);
|
||||
const bufferFromHex = require('../hex/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromHex
|
||||
@signature bufferFromHex (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a hex string.
|
||||
@description
|
||||
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { bufferFromHex } from '@polkadot/util';
|
||||
|
||||
console.log('Buffer object', bufferFromHex('0x123480001f'));
|
||||
@summary Creates a Buffer object from a hex string. [hex.md#hextobuffer](alias hexToBuffer)
|
||||
*/
|
||||
module.exports = function bufferFromHex (value?: string): Buffer {
|
||||
if (!value) {
|
||||
return EMPTY_BUFFER;
|
||||
}
|
||||
|
||||
if (!isHex(value)) {
|
||||
throw new Error(`Cannot convert non-hex value '${value}' to Buffer`);
|
||||
}
|
||||
|
||||
return Buffer.from(hexStripPrefix(value), 'hex');
|
||||
};
|
||||
module.exports = bufferFromHex;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromHex } = require('./index');
|
||||
|
||||
describe('bufferFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const bufferFromNumber = require('../number/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromNumber
|
||||
@signature bufferFromNumber (value?: number): Buffer
|
||||
@summary Creates a Buffer object from a number. [number.md#numbertobuffer](alias numberToBuffer)
|
||||
*/
|
||||
module.exports = bufferFromNumber;
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromNumber } = require('./index');
|
||||
|
||||
describe('bufferFromNumber', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromNumber)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const bufferFromU8a = require('../u8a/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromU8a
|
||||
@signature bufferFromU8a (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a Uint8Array. [u8a.md#u8atobuffer](alias u8aToBuffer)
|
||||
*/
|
||||
module.exports = bufferFromU8a;
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromHex } = require('./index');
|
||||
|
||||
describe('bufferFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
+9
-1
@@ -2,12 +2,20 @@
|
||||
// @flow
|
||||
|
||||
const bufferFromHex = require('./fromHex');
|
||||
const bufferFromNumber = require('./fromNumber');
|
||||
const bufferFromU8a = require('./fromU8a');
|
||||
const bufferToHex = require('./toHex');
|
||||
const bufferToNumber = require('./toNumber');
|
||||
const bufferToU8a = require('./toU8a');
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to and from `Buffer` objects
|
||||
*/
|
||||
module.exports = {
|
||||
bufferFromHex,
|
||||
bufferToHex
|
||||
bufferFromNumber,
|
||||
bufferFromU8a,
|
||||
bufferToHex,
|
||||
bufferToNumber,
|
||||
bufferToU8a
|
||||
};
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { bufferFromHex, bufferToHex } = require('./');
|
||||
|
||||
describe('buffer', () => {
|
||||
describe('bufferFromHex', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
bufferFromHex(null).length
|
||||
).toEqual(0);
|
||||
});
|
||||
|
||||
it('returns a buffer with the correct values', () => {
|
||||
expect(
|
||||
Buffer.from([128, 0, 10]).equals(
|
||||
bufferFromHex('0x80000a')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('throws error on non-hex inputs', () => {
|
||||
expect(
|
||||
() => bufferFromHex('notAHex')
|
||||
).toThrow(/Cannot convert non-hex value/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bufferToHex', () => {
|
||||
it('returns 0x when the buffer is null', () => {
|
||||
expect(
|
||||
bufferToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('returns the hex value for the buffer', () => {
|
||||
expect(
|
||||
bufferToHex(Buffer.from([128, 0, 10]))
|
||||
).toEqual('0x80000a');
|
||||
});
|
||||
|
||||
it('throws when non-buffer value is supplied', () => {
|
||||
expect(
|
||||
() => bufferToHex('noABuffer')
|
||||
).toThrow(/Cannot convert non-buffer/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { bufferToHex } = require('./index');
|
||||
|
||||
describe('bufferToHex', () => {
|
||||
it('returns 0x when the buffer is null', () => {
|
||||
expect(
|
||||
bufferToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('returns the hex value for the buffer', () => {
|
||||
expect(
|
||||
bufferToHex(Buffer.from([128, 0, 10]))
|
||||
).toEqual('0x80000a');
|
||||
});
|
||||
|
||||
it('throws when non-buffer value is supplied', () => {
|
||||
expect(
|
||||
() => bufferToHex('noABuffer')
|
||||
).toThrow(/Cannot convert non-buffer/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name bufferToNumber
|
||||
@signature bufferToNumber (value?: Buffer): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { bufferToNumber } from '@polkadot/util';
|
||||
|
||||
bufferToNumber(Buffer.from([12, 34, 56])); // => 0x123456
|
||||
*/
|
||||
module.exports = function bufferToNumber (buffer?: Buffer): number {
|
||||
if (!buffer || !buffer.length) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const length = buffer.length;
|
||||
let value = 0;
|
||||
|
||||
for (let index = 0; index < length; ++index) {
|
||||
value = (value * 256) + buffer[index];
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { bufferToNumber } = require('./index');
|
||||
|
||||
describe('bufferToNumber', () => {
|
||||
it('converts nothing to NaN', () => {
|
||||
expect(
|
||||
bufferToNumber()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts empty buffer to NaN', () => {
|
||||
expect(
|
||||
bufferToNumber(Buffer.from([]))
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts a buffer to a value', () => {
|
||||
expect(
|
||||
bufferToNumber(Buffer.from([0x12, 0x34]))
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isBuffer = require('../is/buffer');
|
||||
|
||||
/**
|
||||
@name bufferToU8a
|
||||
@signature bufferToU8a (value?: Buffer): string
|
||||
@summary Creates a Uint8Array value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
|
||||
@example
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
bufferToU8a(Buffer.from([1, 2, 3]));
|
||||
*/
|
||||
module.exports = function bufferToU8a (buffer?: Buffer): Uint8Array {
|
||||
if (!buffer) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
if (!isBuffer(buffer)) {
|
||||
throw new Error('Cannot convert non-buffer to Uint8Array');
|
||||
}
|
||||
|
||||
const array = new Uint8Array(buffer.length);
|
||||
|
||||
for (let index = 0; index < buffer.length; ++index) {
|
||||
array[index] = buffer[index];
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { bufferToU8a } = require('./index');
|
||||
|
||||
describe('bufferToU8a', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
bufferToU8a(null)
|
||||
).toEqual(new Uint8Array([]));
|
||||
});
|
||||
|
||||
it('returns a Uint8Buffer with the correct values', () => {
|
||||
expect(
|
||||
bufferToU8a(Buffer.from([128, 0, 10]))
|
||||
).toEqual(new Uint8Array([128, 0, 10]));
|
||||
});
|
||||
|
||||
it('throws error on non-Buffer inputs', () => {
|
||||
expect(
|
||||
() => bufferToU8a('notBuffer')
|
||||
).toThrow(/Cannot convert non-/);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,9 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const ExtError = require('./error');
|
||||
const isError = require('../is/error');
|
||||
const { ExtError } = require('./index');
|
||||
|
||||
describe('Error', () => {
|
||||
describe('ExtError', () => {
|
||||
describe('constructor', () => {
|
||||
it('constructs an Error that is still an Error', () => {
|
||||
expect(
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { hexAddPrefix } = require('./index');
|
||||
|
||||
describe('hexAddPrefix', () => {
|
||||
it('does not add when prefix is available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0x0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds the prefix when it is not available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds extra 0 when length % 2 === 1', () => {
|
||||
expect(
|
||||
hexAddPrefix('123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('returns null as 0x', () => {
|
||||
expect(
|
||||
hexAddPrefix(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const hexFromBn = require('../bn/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBn
|
||||
@signature hexFromBn (value?: BN): string
|
||||
@summary Creates a hex value from a BN.js bignumber object. [bn.md#bntohex](alias bnToHex)
|
||||
*/
|
||||
module.exports = hexFromBn;
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { hexFromBn } = require('./index');
|
||||
|
||||
describe('hexFromBn', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBn)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const hexFromBuffer = require('../bn/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBn
|
||||
@signature hexFromBuffer (value?: Buffer): string
|
||||
@summary Creates a hex value from a Buffer object. [buffer.md#buffertohex](alias bufferToHex)
|
||||
*/
|
||||
module.exports = hexFromBuffer;
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { hexFromBuffer } = require('./index');
|
||||
|
||||
describe('hexFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const hexFromNumber = require('../number/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromNumber
|
||||
@signature hexFromNumber (value?: number): string
|
||||
@summary Creates a hex value from a number. [number.md#numbertohex](alias numberToHex)
|
||||
*/
|
||||
module.exports = hexFromNumber;
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { hexFromNumber } = require('./index');
|
||||
|
||||
describe('hexFromNumber', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromNumber)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { hexHasPrefix } = require('./index');
|
||||
|
||||
describe('hexHasPrefix', () => {
|
||||
it('returns true when hex prefix is found', () => {
|
||||
expect(
|
||||
hexHasPrefix('0x123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false when no prefix attached', () => {
|
||||
expect(
|
||||
hexHasPrefix('123')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when null value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when non-string value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
+13
-1
@@ -2,14 +2,26 @@
|
||||
// @flow
|
||||
|
||||
const hexAddPrefix = require('./addPrefix');
|
||||
const hexFromBn = require('./fromBn');
|
||||
const hexFromBuffer = require('./fromBuffer');
|
||||
const hexFromNumber = require('./fromNumber');
|
||||
const hexHasPrefix = require('./hasPrefix');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
const hexToBn = require('./toBn');
|
||||
const hexToBuffer = require('./toBuffer');
|
||||
const hexToNumber = require('./toNumber');
|
||||
|
||||
/**
|
||||
@summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
module.exports = {
|
||||
hexAddPrefix,
|
||||
hexFromBn,
|
||||
hexFromBuffer,
|
||||
hexFromNumber,
|
||||
hexHasPrefix,
|
||||
hexStripPrefix
|
||||
hexStripPrefix,
|
||||
hexToBn,
|
||||
hexToBuffer,
|
||||
hexToNumber
|
||||
};
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { hexAddPrefix, hexHasPrefix, hexStripPrefix } = require('./index');
|
||||
|
||||
describe('hex', () => {
|
||||
describe('hexAddPrefix', () => {
|
||||
it('does not add when prefix is available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0x0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds the prefix when it is not available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds extra 0 when length % 2 === 1', () => {
|
||||
expect(
|
||||
hexAddPrefix('123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('returns null as 0x', () => {
|
||||
expect(
|
||||
hexAddPrefix(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
});
|
||||
|
||||
describe('hexHasPrefix', () => {
|
||||
it('returns true when hex prefix is found', () => {
|
||||
expect(
|
||||
hexHasPrefix('0x123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false when no prefix attached', () => {
|
||||
expect(
|
||||
hexHasPrefix('123')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when null value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when non-string value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hexStripPrefix', () => {
|
||||
it('returns the value as-is when no prefix', () => {
|
||||
expect(
|
||||
hexStripPrefix('01ab')
|
||||
).toEqual('01ab');
|
||||
});
|
||||
|
||||
it('returns an empty string when null value supplied', () => {
|
||||
expect(
|
||||
hexStripPrefix(null)
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('strips the prefix from other strings', () => {
|
||||
expect(
|
||||
hexStripPrefix('0x1223')
|
||||
).toEqual('1223');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { hexStripPrefix } = require('./index');
|
||||
|
||||
describe('hexStripPrefix', () => {
|
||||
it('returns the value as-is when no prefix', () => {
|
||||
expect(
|
||||
hexStripPrefix('01ab')
|
||||
).toEqual('01ab');
|
||||
});
|
||||
|
||||
it('returns an empty string when null value supplied', () => {
|
||||
expect(
|
||||
hexStripPrefix(null)
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('strips the prefix from other strings', () => {
|
||||
expect(
|
||||
hexStripPrefix('0x1223')
|
||||
).toEqual('1223');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
const isHex = require('../is/hex');
|
||||
|
||||
const ZERO_BN = new BN(0);
|
||||
|
||||
/**
|
||||
@name hexToBn
|
||||
@signature hexToBn (value?: string): BN
|
||||
@summary Creates a BN.js bignumber object from a hex string.
|
||||
@description
|
||||
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { hexToBn } from '@polkadot/util';
|
||||
|
||||
console.log('BN object', hexToBn('0x123480001f'));
|
||||
*/
|
||||
module.exports = function hexToBn (value?: string): BN {
|
||||
if (!value) {
|
||||
return ZERO_BN;
|
||||
}
|
||||
|
||||
if (!isHex(value)) {
|
||||
throw new Error(`Cannot convert from non-hex value '${value}' to BN`);
|
||||
}
|
||||
|
||||
return new BN(hexStripPrefix(value), 16);
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
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)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts null values to BN(0)', () => {
|
||||
expect(
|
||||
hexToBn(null).eq(
|
||||
new BN(0)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('throws when trying to convert non-BN values', () => {
|
||||
expect(
|
||||
() => hexToBn('noAHex')
|
||||
).toThrow(/Cannot convert from non-hex/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isHex = require('../is/hex');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
const EMPTY_BUFFER = Buffer.from([]);
|
||||
|
||||
/**
|
||||
@name hexToBuffer
|
||||
@signature hexToBuffer (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a hex string.
|
||||
@description
|
||||
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { hexToBuffer } from '@polkadot/util';
|
||||
|
||||
console.log('Buffer object', hexToBuffer('0x123480001f'));
|
||||
*/
|
||||
module.exports = function hexToBuffer (value?: string): Buffer {
|
||||
if (!value) {
|
||||
return EMPTY_BUFFER;
|
||||
}
|
||||
|
||||
if (!isHex(value)) {
|
||||
throw new Error(`Cannot convert non-hex value '${value}' to Buffer`);
|
||||
}
|
||||
|
||||
return Buffer.from(hexStripPrefix(value), 'hex');
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { hexToBuffer } = require('./index');
|
||||
|
||||
describe('hexToBuffer', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
hexToBuffer(null).length
|
||||
).toEqual(0);
|
||||
});
|
||||
|
||||
it('returns a buffer with the correct values', () => {
|
||||
expect(
|
||||
Buffer.from([128, 0, 10]).equals(
|
||||
hexToBuffer('0x80000a')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('throws error on non-hex inputs', () => {
|
||||
expect(
|
||||
() => hexToBuffer('notAHex')
|
||||
).toThrow(/Cannot convert non-hex value/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const bufferToNumber = require('../buffer/toNumber');
|
||||
const hexToBuffer = require('../hex/toBuffer');
|
||||
|
||||
/**
|
||||
@name hexToNumber
|
||||
@signature hexToNumber (value?: Hex): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { hexToNumber } from '@polkadot/util';
|
||||
|
||||
hexToNumber('0x1234'); // => 0x1234
|
||||
*/
|
||||
module.exports = function hexToNumber (value?: string): number {
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return bufferToNumber(
|
||||
hexToBuffer(value)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { hexToNumber } = require('./index');
|
||||
|
||||
describe('hexToNumber', () => {
|
||||
it('converts an empty to NaN', () => {
|
||||
expect(
|
||||
hexToNumber()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts to a number from hex', () => {
|
||||
expect(
|
||||
hexToNumber('0x1234')
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
+1
-1
@@ -15,5 +15,5 @@ const u8a = require('./u8a');
|
||||
@summary Utility methods for this package are split into groups
|
||||
*/
|
||||
module.exports = Object.assign(
|
||||
{}, bn, buffer, ext, hex, is, jsonrpc, keccak, logger, u8a
|
||||
{}, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, u8a
|
||||
);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { isBN } = require('./index');
|
||||
|
||||
describe('isBN', () => {
|
||||
it('returns true when a BN value', () => {
|
||||
expect(
|
||||
isBN(new BN(123))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BN values', () => {
|
||||
expect(
|
||||
isBN(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isBuffer } = require('./index');
|
||||
|
||||
describe('isBuffer', () => {
|
||||
it('returns true when a Buffer value', () => {
|
||||
expect(
|
||||
isBuffer(Buffer.from([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values', () => {
|
||||
expect(
|
||||
isBuffer(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isError } = require('./index');
|
||||
|
||||
describe('isError', () => {
|
||||
it('returns true when an Error value', () => {
|
||||
expect(
|
||||
isError(new Error('testing'))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Error values', () => {
|
||||
expect(
|
||||
isError(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isFunction } = require('./index');
|
||||
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid functions', () => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isHex } = require('./index');
|
||||
|
||||
describe('isHex', () => {
|
||||
const test = '123abcd45';
|
||||
|
||||
it('returns true on hex values', () => {
|
||||
expect(
|
||||
isHex(`0x${test}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on uppercase values', () => {
|
||||
expect(
|
||||
isHex(`0x${test.toUpperCase()}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('return false on hex values unprefixed', () => {
|
||||
expect(
|
||||
isHex(test)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on non-string values', () => {
|
||||
expect(
|
||||
isHex(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -10,6 +10,7 @@ const isInstanceOf = require('./instanceOf');
|
||||
const isNull = require('./null');
|
||||
const isNumber = require('./number');
|
||||
const isString = require('./string');
|
||||
const isUint8Array = require('./uint8Array');
|
||||
const isUndefined = require('./undefined');
|
||||
|
||||
/**
|
||||
@@ -25,5 +26,6 @@ module.exports = {
|
||||
isNull,
|
||||
isNumber,
|
||||
isString,
|
||||
isUint8Array,
|
||||
isUndefined
|
||||
};
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { isBN, isBuffer, isError, isFunction, isHex, isInstanceOf, isNumber, isString, isUndefined } = require('./index');
|
||||
|
||||
describe('is', () => {
|
||||
describe('isBN', () => {
|
||||
it('returns true when a BN value', () => {
|
||||
expect(
|
||||
isBN(new BN(123))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BN values', () => {
|
||||
expect(
|
||||
isBN(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isBuffer', () => {
|
||||
it('returns true when a Buffer value', () => {
|
||||
expect(
|
||||
isBuffer(Buffer.from([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values', () => {
|
||||
expect(
|
||||
isBuffer(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isError', () => {
|
||||
it('returns true when an Error value', () => {
|
||||
expect(
|
||||
isError(new Error('testing'))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Error values', () => {
|
||||
expect(
|
||||
isError(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid functions', () => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isHex', () => {
|
||||
const test = '123abcd45';
|
||||
|
||||
it('returns true on hex values', () => {
|
||||
expect(
|
||||
isHex(`0x${test}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on uppercase values', () => {
|
||||
expect(
|
||||
isHex(`0x${test.toUpperCase()}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('return false on hex values unprefixed', () => {
|
||||
expect(
|
||||
isHex(test)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on non-string values', () => {
|
||||
expect(
|
||||
isHex(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isInstanceOf', () => {
|
||||
it('returns true on real instances', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', () => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-instances', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when class not specified', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on wrong class type', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), String)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isNumber', () => {
|
||||
it('returns true on valid numbers', () => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isString', () => {
|
||||
it('returns true on valid strings', () => {
|
||||
expect(
|
||||
isString('123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on empty strings', () => {
|
||||
expect(
|
||||
isString('')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isString(2)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUndefined', () => {
|
||||
it('returns true on undefined values', () => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on defined values', () => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isInstanceOf } = require('./index');
|
||||
|
||||
describe('isInstanceOf', () => {
|
||||
it('returns true on real instances', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', () => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-instances', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when class not specified', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on wrong class type', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), String)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isNull } = require('./index');
|
||||
|
||||
describe('isNull', () => {
|
||||
it('returns true when a null value', () => {
|
||||
expect(
|
||||
isNull(null)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-null values', () => {
|
||||
expect(
|
||||
isNull()
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isNumber } = require('./index');
|
||||
|
||||
describe('isNumber', () => {
|
||||
it('returns true on valid numbers', () => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isString } = require('./index');
|
||||
|
||||
describe('isString', () => {
|
||||
it('returns true on valid strings', () => {
|
||||
expect(
|
||||
isString('123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on empty strings', () => {
|
||||
expect(
|
||||
isString('')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isString(2)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isUint8Array } = require('./index');
|
||||
|
||||
describe('isUint8Array', () => {
|
||||
it('returns false on undefined values', () => {
|
||||
expect(
|
||||
isUint8Array()
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on Array values', () => {
|
||||
expect(
|
||||
isUint8Array([])
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on Uint8Array values', () => {
|
||||
expect(
|
||||
isUint8Array(new Uint8Array([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { isUndefined } = require('./index');
|
||||
|
||||
describe('isUndefined', () => {
|
||||
it('returns true on undefined values', () => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on defined values', () => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -1,32 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { jsonrpcSignature } = require('./index');
|
||||
|
||||
describe('jsonrpc', () => {
|
||||
describe('jsonrpcSignature', () => {
|
||||
it('formats the signature according to the specification', () => {
|
||||
expect(
|
||||
jsonrpcSignature(
|
||||
'test_method',
|
||||
[
|
||||
{ name: 'b', type: 'A' },
|
||||
{ name: 'a', type: 'B' },
|
||||
{ type: 'C' },
|
||||
{ type: 'D' }
|
||||
],
|
||||
{ type: 'A' }
|
||||
)
|
||||
).toEqual('test_method (b: A, a: B, C, D): A');
|
||||
});
|
||||
|
||||
it('empty inputs format correctly', () => {
|
||||
expect(
|
||||
jsonrpcSignature(
|
||||
'test_method',
|
||||
[],
|
||||
{ type: 'A' }
|
||||
)
|
||||
).toEqual('test_method (): A');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { jsonrpcSignature } = require('./index');
|
||||
|
||||
describe('jsonrpcSignature', () => {
|
||||
it('formats the signature according to the specification', () => {
|
||||
expect(
|
||||
jsonrpcSignature(
|
||||
'test_method',
|
||||
[
|
||||
{ name: 'b', type: 'A' },
|
||||
{ name: 'a', type: 'B' },
|
||||
{ type: 'C' },
|
||||
{ type: 'D' }
|
||||
],
|
||||
{ type: 'A' }
|
||||
)
|
||||
).toEqual('test_method (b: A, a: B, C, D): A');
|
||||
});
|
||||
|
||||
it('empty inputs format correctly', () => {
|
||||
expect(
|
||||
jsonrpcSignature(
|
||||
'test_method',
|
||||
[],
|
||||
{ type: 'A' }
|
||||
)
|
||||
).toEqual('test_method (): A');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { keccakAsBuffer } = require('./index');
|
||||
|
||||
const value = 'test value';
|
||||
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
|
||||
|
||||
describe('keccakAsBuffer', () => {
|
||||
it('returns an hex representation', () => {
|
||||
expect(
|
||||
keccakAsBuffer(value).equals(
|
||||
Buffer.from(result, 'hex')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { keccakAsHex } = require('./index');
|
||||
|
||||
const value = 'test value';
|
||||
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
|
||||
|
||||
describe('keccakAsHex', () => {
|
||||
it('returns a prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsHex(value)
|
||||
).toEqual(`0x${result}`);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { keccakAsString } = require('./index');
|
||||
|
||||
const value = 'test value';
|
||||
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
|
||||
|
||||
describe('keccakAsString', () => {
|
||||
it('returns an un-prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsString(value)
|
||||
).toEqual(result);
|
||||
});
|
||||
});
|
||||
@@ -1,36 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { keccakAsBuffer, keccakAsHex, keccakAsString } = require('./index');
|
||||
|
||||
describe('keccak', () => {
|
||||
const value = 'test value';
|
||||
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
|
||||
|
||||
describe('keccakAsBuffer', () => {
|
||||
it('returns an hex representation', () => {
|
||||
expect(
|
||||
keccakAsBuffer(value).equals(
|
||||
Buffer.from(result, 'hex')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('keccakAsHex', () => {
|
||||
it('returns a prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsHex(value)
|
||||
).toEqual(`0x${result}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('keccakAsString', () => {
|
||||
it('returns an un-prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsString(value)
|
||||
).toEqual(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
/* global jest */
|
||||
|
||||
const logger = require('./logger');
|
||||
const { logger } = require('./index');
|
||||
|
||||
describe('logger', () => {
|
||||
let l;
|
||||
|
||||
@@ -1,28 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const numberFromBuffer = require('../buffer/toNumber');
|
||||
|
||||
/**
|
||||
@name numberFromBuffer
|
||||
@signature numberFromBuffer (value?: Buffer): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { numberFromBuffer } from '@polkadot/util';
|
||||
|
||||
numberFromBuffer(Buffer.from([12, 34, 56])); // => 0x123456
|
||||
@summary Creates a Number value from a Buffer object. [buffer.md#buffertonumber](alias bufferToNumber)
|
||||
*/
|
||||
module.exports = function numberFromBuffer (buffer?: Buffer): number {
|
||||
if (!buffer || !buffer.length) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const length = buffer.length;
|
||||
let value = 0;
|
||||
|
||||
for (let index = 0; index < length; ++index) {
|
||||
value = (value * 256) + buffer[index];
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
module.exports = numberFromBuffer;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { numberFromBuffer } = require('./index');
|
||||
|
||||
describe('numberFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(numberFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
+3
-18
@@ -1,26 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const bufferFromHex = require('../buffer/fromHex');
|
||||
const numberFromBuffer = require('./fromBuffer');
|
||||
const numberFromHex = require('../hex/toNumber');
|
||||
|
||||
/**
|
||||
@name numberFromHex
|
||||
@signature numberFromHex (value?: Hex): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an Nan result, `hex` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { numberFromHex } from '@polkadot/util';
|
||||
|
||||
numberFromBuffer('0x1234'); // => 0x1234
|
||||
@summary Creates a Number value from a Buffer object. [buffer.md#hextonumber](alias hexToNumber)
|
||||
*/
|
||||
module.exports = function numberFromHex (value?: string): number {
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return numberFromBuffer(
|
||||
bufferFromHex(value)
|
||||
);
|
||||
};
|
||||
module.exports = numberFromHex;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { numberFromHex } = require('./index');
|
||||
|
||||
describe('numberFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(numberFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,100 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const numberFromBuffer = require('./fromBuffer');
|
||||
const numberFromHex = require('./fromHex');
|
||||
const numberToBuffer = require('./toBuffer');
|
||||
const numberToHex = require('./toHex');
|
||||
|
||||
describe('number', () => {
|
||||
describe('numberFromBuffer', () => {
|
||||
it('converts nothing to NaN', () => {
|
||||
expect(
|
||||
numberFromBuffer()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts empty buffer to NaN', () => {
|
||||
expect(
|
||||
numberFromBuffer(Buffer.from([]))
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts a buffer to a value', () => {
|
||||
expect(
|
||||
numberFromBuffer(Buffer.from([0x12, 0x34]))
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
|
||||
describe('numberToBuffer', () => {
|
||||
it('converts undefined to empty', () => {
|
||||
expect(
|
||||
numberToBuffer()
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts null to empty', () => {
|
||||
expect(
|
||||
numberToBuffer(null)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts NaN to empty', () => {
|
||||
expect(
|
||||
numberToBuffer(NaN)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts values to the buffer', () => {
|
||||
expect(
|
||||
numberToBuffer(0x3456)
|
||||
).toEqual(Buffer.from([0x34, 0x56]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('numberFromHex', () => {
|
||||
it('converts an empty to NaN', () => {
|
||||
expect(
|
||||
numberFromHex()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts to a number from hex', () => {
|
||||
expect(
|
||||
numberFromHex('0x1234')
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
|
||||
describe('numberToHex', () => {
|
||||
it('converts undefined to 0x', () => {
|
||||
expect(
|
||||
numberToHex()
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts null to 0x', () => {
|
||||
expect(
|
||||
numberToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts Nan to 0x', () => {
|
||||
expect(
|
||||
numberToHex(NaN)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts 0 to 0x00', () => {
|
||||
expect(
|
||||
numberToHex(0)
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('converts number to hex', () => {
|
||||
expect(
|
||||
numberToHex(0x1245)
|
||||
).toEqual('0x1245');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const bufferFromHex = require('../buffer/fromHex');
|
||||
const hexToBuffer = require('../hex/toBuffer');
|
||||
const isNull = require('../is/null');
|
||||
const isUndefined = require('../is/undefined');
|
||||
const numberToHex = require('./toHex');
|
||||
@@ -22,7 +22,7 @@ module.exports = function numberToBuffer (value?: number): Buffer {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return bufferFromHex(
|
||||
return hexToBuffer(
|
||||
numberToHex(value)
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { numberToBuffer } = require('./index');
|
||||
|
||||
describe('numberToBuffer', () => {
|
||||
it('converts undefined to empty', () => {
|
||||
expect(
|
||||
numberToBuffer()
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts null to empty', () => {
|
||||
expect(
|
||||
numberToBuffer(null)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts NaN to empty', () => {
|
||||
expect(
|
||||
numberToBuffer(NaN)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts values to the buffer', () => {
|
||||
expect(
|
||||
numberToBuffer(0x3456)
|
||||
).toEqual(Buffer.from([0x34, 0x56]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { numberToHex } = require('./index');
|
||||
|
||||
describe('numberToHex', () => {
|
||||
it('converts undefined to 0x', () => {
|
||||
expect(
|
||||
numberToHex()
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts null to 0x', () => {
|
||||
expect(
|
||||
numberToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts Nan to 0x', () => {
|
||||
expect(
|
||||
numberToHex(NaN)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts 0 to 0x00', () => {
|
||||
expect(
|
||||
numberToHex(0)
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('converts number to hex', () => {
|
||||
expect(
|
||||
numberToHex(0x1245)
|
||||
).toEqual('0x1245');
|
||||
});
|
||||
});
|
||||
+4
-26
@@ -1,33 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isBuffer = require('../is/buffer');
|
||||
const u8aFromBuffer = require('../buffer/toU8a');
|
||||
|
||||
/**
|
||||
@name u8aFromBuffer
|
||||
@signature u8aFromBuffer (value?: Buffer): string
|
||||
@summary Creates a Uint8Array value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
|
||||
@example
|
||||
import { u8aFromBuffer } from '@polkadot/util';
|
||||
|
||||
u8aFromBuffer(Buffer.from([1, 2, 3]));
|
||||
@signature u8aFromBuffer (value?: Buffer): number
|
||||
@summary Creates a Uint8Array value from a Buffer object. [buffer.md#buffertou8a](alias bufferToU8a)
|
||||
*/
|
||||
module.exports = function u8aFromBuffer (buffer?: Buffer): Uint8Array {
|
||||
if (!buffer) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
if (!isBuffer(buffer)) {
|
||||
throw new Error('Cannot convert non-buffer to Uint8Array');
|
||||
}
|
||||
|
||||
const array = new Uint8Array(buffer.length);
|
||||
|
||||
for (let index = 0; index < buffer.length; ++index) {
|
||||
array[index] = buffer[index];
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
module.exports = u8aFromBuffer;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { u8aFromBuffer } = require('./index');
|
||||
|
||||
describe('u8aFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(u8aFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { u8aFromBuffer, u8aToBuffer } = require('./');
|
||||
|
||||
describe('u8a', () => {
|
||||
describe('u8aFromBuffer', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
u8aFromBuffer(null)
|
||||
).toEqual(new Uint8Array([]));
|
||||
});
|
||||
|
||||
it('returns a Uint8Buffer with the correct values', () => {
|
||||
expect(
|
||||
u8aFromBuffer(Buffer.from([128, 0, 10]))
|
||||
).toEqual(new Uint8Array([128, 0, 10]));
|
||||
});
|
||||
|
||||
it('throws error on non-Buffer inputs', () => {
|
||||
expect(
|
||||
() => u8aFromBuffer('notBuffer')
|
||||
).toThrow(/Cannot convert non-/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('u8aToBuffer', () => {
|
||||
it('returns [] when the buffer is null', () => {
|
||||
expect(
|
||||
u8aToBuffer(null)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('returns the Buffer value for the Uint8Array', () => {
|
||||
expect(
|
||||
u8aToBuffer(new Uint8Array([128, 0, 10]))
|
||||
).toEqual(Buffer.from([128, 0, 10]));
|
||||
});
|
||||
|
||||
it('throws when non-Uint8Array value is supplied', () => {
|
||||
expect(
|
||||
() => u8aToBuffer('noU8A')
|
||||
).toThrow(/Cannot convert non-Uint8Array/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { u8aToBuffer } = require('./index');
|
||||
|
||||
describe('u8aToBuffer', () => {
|
||||
it('returns [] when the buffer is null', () => {
|
||||
expect(
|
||||
u8aToBuffer(null)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('returns the Buffer value for the Uint8Array', () => {
|
||||
expect(
|
||||
u8aToBuffer(new Uint8Array([128, 0, 10]))
|
||||
).toEqual(Buffer.from([128, 0, 10]));
|
||||
});
|
||||
|
||||
it('throws when non-Uint8Array value is supplied', () => {
|
||||
expect(
|
||||
() => u8aToBuffer('noU8A')
|
||||
).toThrow(/Cannot convert non-Uint8Array/);
|
||||
});
|
||||
});
|
||||
@@ -60,9 +60,9 @@
|
||||
dependencies:
|
||||
babel-runtime "^6.26.0"
|
||||
|
||||
"@polkadot/dev@^0.9.9":
|
||||
version "0.9.9"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.9.9.tgz#b302ba6728a5068c7bffe069fd64290c227d317c"
|
||||
"@polkadot/dev@^0.10.1":
|
||||
version "0.10.1"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.10.1.tgz#4c57180413320f0194c340177725f8b8c0ecbffa"
|
||||
dependencies:
|
||||
babel-cli "^6.26.0"
|
||||
babel-core "^6.26.0"
|
||||
@@ -79,6 +79,7 @@
|
||||
eslint "^4.11.0"
|
||||
eslint-config-semistandard "^11.0.0"
|
||||
eslint-config-standard "^10.2.1"
|
||||
eslint-config-standard-jsx "^4.0.2"
|
||||
eslint-config-standard-react "^5.0.0"
|
||||
eslint-plugin-flowtype "^2.39.1"
|
||||
eslint-plugin-import "^2.8.0"
|
||||
@@ -1614,7 +1615,7 @@ eslint-config-semistandard@^11.0.0:
|
||||
version "11.0.0"
|
||||
resolved "https://registry.yarnpkg.com/eslint-config-semistandard/-/eslint-config-semistandard-11.0.0.tgz#44eef7cfdfd47219e3a7b81b91b540e880bb2615"
|
||||
|
||||
eslint-config-standard-jsx@^4.0.0:
|
||||
eslint-config-standard-jsx@^4.0.0, eslint-config-standard-jsx@^4.0.2:
|
||||
version "4.0.2"
|
||||
resolved "https://registry.yarnpkg.com/eslint-config-standard-jsx/-/eslint-config-standard-jsx-4.0.2.tgz#009e53c4ddb1e9ee70b4650ffe63a7f39f8836e1"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user