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:
Jaco Greeff
2018-02-15 10:32:05 +01:00
committed by GitHub
parent 06eb66f84e
commit 6f8c54337b
36 changed files with 34 additions and 512 deletions
-14
View File
@@ -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;
-15
View File
@@ -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);
});
});
-4
View File
@@ -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
};
-26
View File
@@ -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');
};
-29
View File
@@ -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);
});
});
+2 -5
View File
@@ -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();
};