Split hex into separate components (#7)

This commit is contained in:
Jaco Greeff
2017-11-19 22:48:39 +01:00
committed by GitHub
parent 14234649af
commit 805d7d5fdd
11 changed files with 67 additions and 53 deletions
+1 -1
View File
@@ -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": [
+2 -2
View File
@@ -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);
};
+2 -2
View File
@@ -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));
};
+2 -2
View File
@@ -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');
};
+2 -2
View File
@@ -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')
);
};
-30
View File
@@ -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
};
+12
View File
@@ -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 || ''}`;
};
+8
View File
@@ -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');
};
+12
View File
@@ -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
};
+14 -14
View File
@@ -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');
});
});
+12
View File
@@ -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 || '';
};