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,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