From 805d7d5fdd14a6374faab258683939be6fafce10 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 19 Nov 2017 22:48:39 +0100 Subject: [PATCH] Split hex into separate components (#7) --- package.json | 2 +- src/bn/fromHex.js | 4 ++-- src/bn/toHex.js | 4 ++-- src/buffer/fromHex.js | 4 ++-- src/buffer/toHex.js | 4 ++-- src/hex.js | 30 -------------------------- src/hex/addPrefix.js | 12 +++++++++++ src/hex/hasPrefix.js | 8 +++++++ src/hex/index.js | 12 +++++++++++ src/{hex.spec.js => hex/index.spec.js} | 28 ++++++++++++------------ src/hex/stripPrefix.js | 12 +++++++++++ 11 files changed, 67 insertions(+), 53 deletions(-) delete mode 100644 src/hex.js create mode 100644 src/hex/addPrefix.js create mode 100644 src/hex/hasPrefix.js create mode 100644 src/hex/index.js rename src/{hex.spec.js => hex/index.spec.js} (70%) create mode 100644 src/hex/stripPrefix.js diff --git a/package.json b/package.json index 85e56aa1a..fae45e878 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.3.5", + "version": "0.4.0", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ diff --git a/src/bn/fromHex.js b/src/bn/fromHex.js index dbdceed07..a1e6867f9 100644 --- a/src/bn/fromHex.js +++ b/src/bn/fromHex.js @@ -3,7 +3,7 @@ const BN = require('bn.js'); -const { stripHexPrefix } = require('../hex'); +const hexStripPrefix = require('../hex/stripPrefix'); const isHex = require('../is/hex'); const ZERO_BN = new BN(0); @@ -17,5 +17,5 @@ module.exports = function fromHex (value?: string): BN { throw new Error(`Cannot convert from non-hex value '${value}' to BN`); } - return new BN(stripHexPrefix(value), 16); + return new BN(hexStripPrefix(value), 16); }; diff --git a/src/bn/toHex.js b/src/bn/toHex.js index 05591a02a..a6a1b7ec5 100644 --- a/src/bn/toHex.js +++ b/src/bn/toHex.js @@ -3,7 +3,7 @@ const BN = require('bn.js'); -const { addHexPrefix } = require('../hex'); +const hexAddPrefix = require('../hex/addPrefix'); const isBN = require('../is/bn'); const ZERO_STR = '0x'; @@ -17,5 +17,5 @@ module.exports = function toHex (value?: BN): string { throw new Error(`Cannot convert from non-BN value '${value}' to hex`); } - return addHexPrefix(value.toString(16)); + return hexAddPrefix(value.toString(16)); }; diff --git a/src/buffer/fromHex.js b/src/buffer/fromHex.js index ab63e9e9b..c7a08f495 100644 --- a/src/buffer/fromHex.js +++ b/src/buffer/fromHex.js @@ -2,7 +2,7 @@ // @flow const isHex = require('../is/hex'); -const { stripHexPrefix } = require('../hex'); +const hexStripPrefix = require('../hex/stripPrefix'); const EMPTY_BUFFER = Buffer.from([]); @@ -15,5 +15,5 @@ module.exports = function fromHex (value?: string): Buffer { throw new Error(`Cannot convert non-hex value '${value}' to Buffer`); } - return Buffer.from(stripHexPrefix(value), 'hex'); + return Buffer.from(hexStripPrefix(value), 'hex'); }; diff --git a/src/buffer/toHex.js b/src/buffer/toHex.js index f36f8a83b..f9d04942e 100644 --- a/src/buffer/toHex.js +++ b/src/buffer/toHex.js @@ -2,7 +2,7 @@ // @flow const isBuffer = require('../is/buffer'); -const { addHexPrefix } = require('../hex'); +const hexAddPrefix = require('../hex/addPrefix'); const ZERO_HEX = '0x'; @@ -15,7 +15,7 @@ module.exports = function toHex (value?: Buffer): string { throw new Error(`Cannot convert non-buffer to hex`); } - return addHexPrefix( + return hexAddPrefix( value.toString('hex') ); }; diff --git a/src/hex.js b/src/hex.js deleted file mode 100644 index 1ef4856a0..000000000 --- a/src/hex.js +++ /dev/null @@ -1,30 +0,0 @@ -// ISC, Copyright 2017 Jaco Greeff -// @flow - -const isHex = require('./is/hex'); - -function addHexPrefix (value: ?string): string { - if (value && hasHexPrefix(value)) { - return value; - } - - return `0x${value || ''}`; -} - -function hasHexPrefix (value: ?string): boolean { - return !!(value && isHex(value) && value.substr(0, 2) === '0x'); -} - -function stripHexPrefix (value: ?string): string { - if (value && hasHexPrefix(value)) { - return value.substr(2); - } - - return value || ''; -} - -module.exports = { - addHexPrefix, - hasHexPrefix, - stripHexPrefix -}; diff --git a/src/hex/addPrefix.js b/src/hex/addPrefix.js new file mode 100644 index 000000000..3e834ff8e --- /dev/null +++ b/src/hex/addPrefix.js @@ -0,0 +1,12 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const hasPrefix = require('./hasPrefix'); + +module.exports = function addPrefix (value: ?string): string { + if (value && hasPrefix(value)) { + return value; + } + + return `0x${value || ''}`; +}; diff --git a/src/hex/hasPrefix.js b/src/hex/hasPrefix.js new file mode 100644 index 000000000..45469d155 --- /dev/null +++ b/src/hex/hasPrefix.js @@ -0,0 +1,8 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const isHex = require('../is/hex'); + +module.exports = function hasPrefix (value: ?string): boolean { + return !!(value && isHex(value) && value.substr(0, 2) === '0x'); +}; diff --git a/src/hex/index.js b/src/hex/index.js new file mode 100644 index 000000000..ea39c8d5d --- /dev/null +++ b/src/hex/index.js @@ -0,0 +1,12 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const hexAddPrefix = require('./addPrefix'); +const hexHasPrefix = require('./hasPrefix'); +const hexStripPrefix = require('./stripPrefix'); + +module.exports = { + hexAddPrefix, + hexHasPrefix, + hexStripPrefix +}; diff --git a/src/hex.spec.js b/src/hex/index.spec.js similarity index 70% rename from src/hex.spec.js rename to src/hex/index.spec.js index 76dcdd825..7219f8144 100644 --- a/src/hex.spec.js +++ b/src/hex/index.spec.js @@ -2,71 +2,71 @@ /* eslint-disable no-unused-expressions */ -const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('./hex'); +const { hexAddPrefix, hexHasPrefix, hexStripPrefix } = require('./index'); describe('hex', () => { - describe('addHexPrefix', () => { + describe('hexAddPrefix', () => { it('does not add when prefix is available', () => { expect( - addHexPrefix('0x123') + hexAddPrefix('0x123') ).to.equal('0x123'); }); it('adds the prefix when it is not available', () => { expect( - addHexPrefix('123') + hexAddPrefix('123') ).to.equal('0x123'); }); it('returns null as 0x', () => { expect( - addHexPrefix(null) + hexAddPrefix(null) ).to.equal('0x'); }); }); - describe('hasHexPrefix', () => { + describe('hexHasPrefix', () => { it('returns true when hex prefix is found', () => { expect( - hasHexPrefix('0x123') + hexHasPrefix('0x123') ).to.be.true; }); it('returns false when no prefix attached', () => { expect( - hasHexPrefix('123') + hexHasPrefix('123') ).to.be.false; }); it('returns false when null value supplied', () => { expect( - hasHexPrefix(null) + hexHasPrefix(null) ).to.be.false; }); it('returns false when non-string value supplied', () => { expect( - hasHexPrefix(false) + hexHasPrefix(false) ).to.be.false; }); }); - describe('stripHexPrefix', () => { + describe('hexStripPrefix', () => { it('returns the value as-is when no prefix', () => { expect( - stripHexPrefix('01ab') + hexStripPrefix('01ab') ).to.equal('01ab'); }); it('returns an empty string when null value supplied', () => { expect( - stripHexPrefix(null) + hexStripPrefix(null) ).to.equal(''); }); it('strips the prefix from other strings', () => { expect( - stripHexPrefix('0x1223') + hexStripPrefix('0x1223') ).to.equal('1223'); }); }); diff --git a/src/hex/stripPrefix.js b/src/hex/stripPrefix.js new file mode 100644 index 000000000..f71b8077e --- /dev/null +++ b/src/hex/stripPrefix.js @@ -0,0 +1,12 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const hasPrefix = require('./hasPrefix'); + +module.exports = function stripPrefix (value: ?string): string { + if (value && hasPrefix(value)) { + return value.substr(2); + } + + return value || ''; +};