Remove {from,to}Buffer methods (#89)
* Remove all {from,to}Buffer utility methods
* Fix check & test
* Updsate docs
* Bump minor
This commit is contained in:
+1
-1
@@ -10,5 +10,5 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "0.14.11"
|
||||
"version": "0.15.0"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "0.14.11",
|
||||
"version": "0.15.0",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { KeypairType } from '../types';
|
||||
const nacl = require('tweetnacl');
|
||||
const hexToU8a = require('@polkadot/util/hex/toU8a');
|
||||
const isHex = require('@polkadot/util/is/hex');
|
||||
const isUint8Array = require('@polkadot/util/is/uint8Array');
|
||||
const isU8a = require('@polkadot/util/is/u8a');
|
||||
const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,7 @@ const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
naclKeypairFromSeed(...) // => { secretKey: [...], publicKey: [...] }
|
||||
*/
|
||||
module.exports = function naclKeypairFromSeed (seed: Uint8Array | string): KeypairType {
|
||||
if (isUint8Array(seed)) {
|
||||
if (isU8a(seed)) {
|
||||
// $FlowFixMe type has been determined
|
||||
return nacl.sign.keyPair.fromSeed(seed);
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isUint8Array = require('@polkadot/util/is/uint8Array');
|
||||
const isU8a = require('@polkadot/util/is/u8a');
|
||||
|
||||
const { randomAsU8a } = require('./index');
|
||||
|
||||
describe('randomAsU8a', () => {
|
||||
it('generates a Uint8Array', () => {
|
||||
expect(
|
||||
isUint8Array(randomAsU8a())
|
||||
isU8a(randomAsU8a())
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-rlp",
|
||||
"version": "0.14.11",
|
||||
"version": "0.15.0",
|
||||
"description": "RLP encoding and decoding",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -29,7 +29,7 @@
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/util": "^0.14.11",
|
||||
"@polkadot/util": "^0.15.0",
|
||||
"babel-runtime": "^6.26.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-triehash",
|
||||
"version": "0.14.11",
|
||||
"version": "0.15.0",
|
||||
"description": "Creation of triehash values",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -29,9 +29,9 @@
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/util": "^0.14.11",
|
||||
"@polkadot/util-crypto": "^0.14.11",
|
||||
"@polkadot/util-rlp": "^0.14.11",
|
||||
"@polkadot/util": "^0.15.0",
|
||||
"@polkadot/util-crypto": "^0.15.0",
|
||||
"@polkadot/util-rlp": "^0.15.0",
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,37 +2,9 @@
|
||||
|
||||
Utility methods to convert to and from `Buffer` objects
|
||||
|
||||
- [bufferFromHex](#bufferfromhex) Creates a Buffer object from a hex string.
|
||||
- [bufferFromNumber](#bufferfromnumber) Creates a Buffer object from a number.
|
||||
- [bufferFromU8a](#bufferfromu8a) Creates a Buffer object from a Uint8Array.
|
||||
- [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. [(alias of hexToBuffer)](hex.md#hextobuffer)
|
||||
|
||||
```js
|
||||
bufferFromHex (value?: string): Buffer
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## bufferFromNumber
|
||||
|
||||
Creates a Buffer object from a number. [(alias of numberToBuffer)](number.md#numbertobuffer)
|
||||
|
||||
```js
|
||||
bufferFromNumber (value?: number): Buffer
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## bufferFromU8a
|
||||
|
||||
Creates a Buffer object from a Uint8Array. [(alias of u8aToBuffer)](u8a.md#u8atobuffer)
|
||||
@@ -45,40 +17,6 @@ bufferFromU8a (value?: string): Buffer
|
||||
|
||||
|
||||
|
||||
## bufferToHex
|
||||
|
||||
Creates a hex value from a Buffer object.
|
||||
|
||||
```js
|
||||
bufferToHex (value?: Buffer): string
|
||||
```
|
||||
|
||||
|
||||
`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error.
|
||||
|
||||
```js
|
||||
import { bufferToHex } from '@polkadot/util';
|
||||
|
||||
bufferToHex(Buffer.from([1, 2, 3]); // => 0x010203
|
||||
```
|
||||
|
||||
## 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.
|
||||
|
||||
@@ -5,12 +5,10 @@ Internal utilities to create and test for hex values
|
||||
- [hexAddPrefix](#hexaddprefix) Adds the `0x` prefix to string values.
|
||||
- [hexFixLength](#hexfixlength) Shifts a hex string to a specific bitLength
|
||||
- [hexFromBn](#hexfrombn) Creates a hex value from a BN.js bignumber object.
|
||||
- [hexFromBuffer](#hexfrombuffer) Creates a hex value from a Buffer object.
|
||||
- [hexFromNumber](#hexfromnumber) Creates a hex value from a number.
|
||||
- [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.
|
||||
- [hexToU8a](#hextou8a) Creates a Buffer object from a hex string.
|
||||
|
||||
@@ -62,18 +60,6 @@ hexFromBn (value?: BN): string
|
||||
|
||||
|
||||
|
||||
## hexFromBuffer
|
||||
|
||||
Creates a hex value from a Buffer object. [(alias of bufferToHex)](buffer.md#buffertohex)
|
||||
|
||||
```js
|
||||
hexFromBuffer (value?: Buffer): string
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## hexFromNumber
|
||||
|
||||
Creates a hex value from a number. [(alias of numberToHex)](number.md#numbertohex)
|
||||
@@ -137,23 +123,6 @@ import { hexToBn } from '@polkadot/util';
|
||||
hexToBn('0x123480001f'); // => BN(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';
|
||||
|
||||
hexToBuffer('0x123480001f'); // Buffer([0x12, 0x34, 0x80, 0x00, 0x1f])
|
||||
```
|
||||
|
||||
## hexToNumber
|
||||
|
||||
Creates a Number value from a Buffer object.
|
||||
|
||||
@@ -14,7 +14,7 @@ Type checking utilities
|
||||
- [isNumber](#isnumber) Tests for a JavaScript number.
|
||||
- [isObject](#isobject) Tests for an `object`.
|
||||
- [isString](#isstring) Tests for a string.
|
||||
- [isUint8Array](#isuint8array) Tests for a `Uint8Array` object instance.
|
||||
- [isU8a](#isu8a) Tests for a `Uint8Array` object instance.
|
||||
- [isUndefined](#isundefined) Tests for a `undefined` values.
|
||||
|
||||
## isBN
|
||||
@@ -227,12 +227,12 @@ import { isString } from '@polkadot/util';
|
||||
console.log('isString', isString('test')); // => true
|
||||
```
|
||||
|
||||
## isUint8Array
|
||||
## isU8a
|
||||
|
||||
Tests for a `Uint8Array` object instance.
|
||||
|
||||
```js
|
||||
isUint8Array (value: mixed): boolean
|
||||
isU8a (value: mixed): boolean
|
||||
```
|
||||
|
||||
|
||||
@@ -241,7 +241,7 @@ Checks to see if the input object is an instance of `Uint8Array`.
|
||||
```js
|
||||
import { isUint8Array } from '@polkadot/util';
|
||||
|
||||
console.log('isUint8Array', isUint8Array([])); // => false
|
||||
console.log('isU8a', isU8a([])); // => false
|
||||
```
|
||||
|
||||
## isUndefined
|
||||
|
||||
@@ -2,24 +2,10 @@
|
||||
|
||||
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.
|
||||
- [numberToBuffer](#numbertobuffer) Creates a Buffer object from a number.
|
||||
- [numberToHex](#numbertohex) Creates a hex value from a number.
|
||||
- [numberToU8a](#numbertou8a) Creates a Uint8Array object from a number.
|
||||
|
||||
## numberFromBuffer
|
||||
|
||||
Creates a Number value from a Buffer object. [(alias of bufferToNumber)](buffer.md#buffertonumber)
|
||||
|
||||
```js
|
||||
numberFromBuffer (value?: Buffer): number
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## numberFromHex
|
||||
|
||||
Creates a Number value from a Buffer object. [(alias of hexToNumber)](hex.md#hextonumber)
|
||||
@@ -32,23 +18,6 @@ numberFromHex (value?: Hex): number
|
||||
|
||||
|
||||
|
||||
## numberToBuffer
|
||||
|
||||
Creates a Buffer object from a number.
|
||||
|
||||
```js
|
||||
numberToBuffer (value?: number): Buffer
|
||||
```
|
||||
|
||||
|
||||
`null`/`undefined`/`NaN` inputs returns an empty `Buffer` result. `number` input values return the actual bytes value converted to a `Buffer`.
|
||||
|
||||
```js
|
||||
import { numberToBuffer } from '@polkadot/util';
|
||||
|
||||
const buffer = numberToBuffer(0x1234); // => Buffer.from([0x12, 0x34])
|
||||
```
|
||||
|
||||
## numberToHex
|
||||
|
||||
Creates a hex value from a number.
|
||||
@@ -72,7 +41,7 @@ numberToHex(0x1234, 32) // => 0x00001234
|
||||
Creates a Uint8Array object from a number.
|
||||
|
||||
```js
|
||||
numberToBuffer (value?: number, bitLenght: number = -1): Uint8Array
|
||||
numberToU8a (value?: number, bitLenght: number = -1): Uint8Array
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "0.14.11",
|
||||
"version": "0.15.0",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
// 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 bufferFromHex = require('../hex/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromHex
|
||||
@signature bufferFromHex (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a hex string.
|
||||
@alias hex/toBuffer
|
||||
*/
|
||||
module.exports = bufferFromHex;
|
||||
@@ -1,15 +0,0 @@
|
||||
// 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 isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromHex } = require('./index');
|
||||
|
||||
describe('bufferFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,14 +0,0 @@
|
||||
// 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 bufferFromNumber = require('../number/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromNumber
|
||||
@signature bufferFromNumber (value?: number): Buffer
|
||||
@summary Creates a Buffer object from a number.
|
||||
@alias number/toBuffer
|
||||
*/
|
||||
module.exports = bufferFromNumber;
|
||||
@@ -1,15 +0,0 @@
|
||||
// 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 isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromNumber } = require('./index');
|
||||
|
||||
describe('bufferFromNumber', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromNumber)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromHex } = require('./index');
|
||||
const { bufferFromU8a } = require('./index');
|
||||
|
||||
describe('bufferFromHex', () => {
|
||||
describe('bufferFromU8a', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromHex)
|
||||
isFunction(bufferFromU8a)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,21 +3,13 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @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,
|
||||
bufferFromNumber,
|
||||
bufferFromU8a,
|
||||
bufferToHex,
|
||||
bufferToNumber,
|
||||
bufferToU8a
|
||||
};
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
// 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 hexAddPrefix = require('../hex/addPrefix');
|
||||
|
||||
const ZERO_HEX = '0x00';
|
||||
|
||||
/**
|
||||
@name bufferToHex
|
||||
@signature bufferToHex (value?: Buffer): string
|
||||
@summary Creates a hex value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error.
|
||||
@example
|
||||
import { bufferToHex } from '@polkadot/util';
|
||||
|
||||
bufferToHex(Buffer.from([1, 2, 3]); // => 0x010203
|
||||
*/
|
||||
module.exports = function bufferToHex (value?: Buffer): string {
|
||||
if (!value) {
|
||||
return ZERO_HEX;
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
value.toString('hex')
|
||||
);
|
||||
};
|
||||
@@ -1,19 +0,0 @@
|
||||
// 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 { bufferToHex } = require('./index');
|
||||
|
||||
describe('bufferToHex', () => {
|
||||
it('returns 0x00 when the buffer is null', () => {
|
||||
expect(
|
||||
bufferToHex(null)
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('returns the hex value for the buffer', () => {
|
||||
expect(
|
||||
bufferToHex(Buffer.from([128, 0, 10]))
|
||||
).toEqual('0x80000a');
|
||||
});
|
||||
});
|
||||
@@ -1,30 +0,0 @@
|
||||
// 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
|
||||
|
||||
/**
|
||||
@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;
|
||||
};
|
||||
@@ -1,25 +0,0 @@
|
||||
// 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 { 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);
|
||||
});
|
||||
});
|
||||
@@ -1,14 +0,0 @@
|
||||
// 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 bufferToHex = require('../buffer/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBuffer
|
||||
@signature hexFromBuffer (value?: Buffer): string
|
||||
@summary Creates a hex value from a Buffer object.
|
||||
@alias buffer/toHex
|
||||
*/
|
||||
module.exports = bufferToHex;
|
||||
@@ -1,15 +0,0 @@
|
||||
// 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 isFunction = require('../is/function');
|
||||
|
||||
const { hexFromBuffer } = require('./index');
|
||||
|
||||
describe('hexFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -6,12 +6,10 @@
|
||||
const hexAddPrefix = require('./addPrefix');
|
||||
const hexFixLength = require('./fixLength');
|
||||
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');
|
||||
const hexToU8a = require('./toU8a');
|
||||
|
||||
@@ -22,12 +20,10 @@ module.exports = {
|
||||
hexAddPrefix,
|
||||
hexFixLength,
|
||||
hexFromBn,
|
||||
hexFromBuffer,
|
||||
hexFromNumber,
|
||||
hexHasPrefix,
|
||||
hexStripPrefix,
|
||||
hexToBn,
|
||||
hexToBuffer,
|
||||
hexToNumber,
|
||||
hexToU8a
|
||||
};
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
// 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 hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
/**
|
||||
@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';
|
||||
|
||||
hexToBuffer('0x123480001f'); // Buffer([0x12, 0x34, 0x80, 0x00, 0x1f])
|
||||
*/
|
||||
module.exports = function hexToBuffer (value?: string): Buffer {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return Buffer.from(hexStripPrefix(value), 'hex');
|
||||
};
|
||||
@@ -1,29 +0,0 @@
|
||||
// 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 { hexToBuffer } = require('./index');
|
||||
|
||||
describe('hexToBuffer', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
hexToBuffer(null)
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('returns a buffer with the correct values', () => {
|
||||
expect(
|
||||
Buffer.from([128, 0, 10]).equals(
|
||||
hexToBuffer('0x80000a')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns a buffer with the correct values (no prefix)', () => {
|
||||
expect(
|
||||
Buffer.from([128, 0, 10]).equals(
|
||||
hexToBuffer('80000a')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -3,8 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferToNumber = require('../buffer/toNumber');
|
||||
const hexToBuffer = require('../hex/toBuffer');
|
||||
const hexToBn = require('./toBn');
|
||||
|
||||
/**
|
||||
@name hexToNumber
|
||||
@@ -23,7 +22,5 @@ module.exports = function hexToNumber (value?: string): number {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return bufferToNumber(
|
||||
hexToBuffer(value)
|
||||
);
|
||||
return hexToBn(value).toNumber();
|
||||
};
|
||||
|
||||
@@ -15,7 +15,7 @@ const isNull = require('./null');
|
||||
const isNumber = require('./number');
|
||||
const isObject = require('./object');
|
||||
const isString = require('./string');
|
||||
const isUint8Array = require('./uint8Array');
|
||||
const isU8a = require('./u8a');
|
||||
const isUndefined = require('./undefined');
|
||||
|
||||
/**
|
||||
@@ -34,6 +34,6 @@ module.exports = {
|
||||
isNull,
|
||||
isNumber,
|
||||
isString,
|
||||
isUint8Array,
|
||||
isU8a,
|
||||
isUndefined
|
||||
};
|
||||
|
||||
@@ -6,16 +6,16 @@
|
||||
const isInstanceOf = require('./instanceOf');
|
||||
|
||||
/**
|
||||
@name isUint8Array
|
||||
@signature isUint8Array (value: mixed): boolean
|
||||
@name isU8a
|
||||
@signature isU8a (value: mixed): boolean
|
||||
@summary Tests for a `Uint8Array` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `Uint8Array`.
|
||||
@example
|
||||
import { isUint8Array } from '@polkadot/util';
|
||||
|
||||
console.log('isUint8Array', isUint8Array([])); // => false
|
||||
console.log('isU8a', isU8a([])); // => false
|
||||
*/
|
||||
module.exports = function isUint8Array (value: mixed): boolean {
|
||||
module.exports = function isU8a (value: mixed): boolean {
|
||||
return isInstanceOf(value, Uint8Array);
|
||||
};
|
||||
@@ -2,24 +2,24 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isUint8Array } = require('./index');
|
||||
const { isU8a } = require('./index');
|
||||
|
||||
describe('isUint8Array', () => {
|
||||
it('returns false on undefined values', () => {
|
||||
expect(
|
||||
isUint8Array()
|
||||
isU8a()
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on Array values', () => {
|
||||
expect(
|
||||
isUint8Array([])
|
||||
isU8a([])
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on Uint8Array values', () => {
|
||||
expect(
|
||||
isUint8Array(new Uint8Array([]))
|
||||
isU8a(new Uint8Array([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -1,14 +0,0 @@
|
||||
// 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 numberFromBuffer = require('../buffer/toNumber');
|
||||
|
||||
/**
|
||||
@name numberFromBuffer
|
||||
@signature numberFromBuffer (value?: Buffer): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@alias buffer/toNumber
|
||||
*/
|
||||
module.exports = numberFromBuffer;
|
||||
@@ -1,15 +0,0 @@
|
||||
// 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 isFunction = require('../is/function');
|
||||
|
||||
const { numberFromBuffer } = require('./index');
|
||||
|
||||
describe('numberFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(numberFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -3,9 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const numberFromBuffer = require('./fromBuffer');
|
||||
const numberFromHex = require('./fromHex');
|
||||
const numberToBuffer = require('./toBuffer');
|
||||
const numberToHex = require('./toHex');
|
||||
const numberToU8a = require('./toU8a');
|
||||
|
||||
@@ -13,9 +11,7 @@ const numberToU8a = require('./toU8a');
|
||||
@summary Utility methods to convert to and from `number` values
|
||||
*/
|
||||
module.exports = {
|
||||
numberFromBuffer,
|
||||
numberFromHex,
|
||||
numberToBuffer,
|
||||
numberToHex,
|
||||
numberToU8a
|
||||
};
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
// 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 hexToBuffer = require('../hex/toBuffer');
|
||||
const isNull = require('../is/null');
|
||||
const isUndefined = require('../is/undefined');
|
||||
const numberToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@name numberToBuffer
|
||||
@signature numberToBuffer (value?: number): Buffer
|
||||
@summary Creates a Buffer object from a number.
|
||||
@description
|
||||
`null`/`undefined`/`NaN` inputs returns an empty `Buffer` result. `number` input values return the actual bytes value converted to a `Buffer`.
|
||||
@example
|
||||
import { numberToBuffer } from '@polkadot/util';
|
||||
|
||||
const buffer = numberToBuffer(0x1234); // => Buffer.from([0x12, 0x34])
|
||||
*/
|
||||
module.exports = function numberToBuffer (value?: number): Buffer {
|
||||
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return hexToBuffer(
|
||||
numberToHex(value)
|
||||
);
|
||||
};
|
||||
@@ -1,31 +0,0 @@
|
||||
// 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 { 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]));
|
||||
});
|
||||
});
|
||||
@@ -8,7 +8,7 @@ const numberToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@name numberToU8a
|
||||
@signature numberToBuffer (value?: number, bitLenght: number = -1): Uint8Array
|
||||
@signature numberToU8a (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`. With `bitLength`, it converts the value to the equivalent size.
|
||||
|
||||
Reference in New Issue
Block a user