From e6cd09a02a10d0bb557ba861a23d2a9e56b0261f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 6 Dec 2017 22:05:10 +0100 Subject: [PATCH] hexFixLength (#42) * hexFixLength * Update @polkadot/api-jsonrpc --- package.json | 4 ++-- src/hex/fixLength.js | 23 +++++++++++++++++++++++ src/hex/fixLength.spec.js | 35 +++++++++++++++++++++++++++++++++++ src/hex/index.js | 2 ++ yarn.lock | 6 +++--- 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/hex/fixLength.js create mode 100644 src/hex/fixLength.spec.js diff --git a/package.json b/package.json index 1792e8a7f..730970044 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,8 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/api-jsonrpc": "^0.5.11", - "@polkadot/dev": "^0.12.6" + "@polkadot/api-jsonrpc": "^0.6.1", + "@polkadot/dev": "^0.13.1" }, "dependencies": { "babel-runtime": "^6.26.0", diff --git a/src/hex/fixLength.js b/src/hex/fixLength.js new file mode 100644 index 000000000..683cfa278 --- /dev/null +++ b/src/hex/fixLength.js @@ -0,0 +1,23 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const assert = require('../assert'); +const isHex = require('../is/hex'); +const hexAddPrefix = require('./addPrefix'); +const hexStripPrefix = require('./stripPrefix'); + +const ZEROS = '0000000000000000000000000000000000000000000000000000000000000000'; + +module.exports = function hexFixLength (value: string, bitLength: number = -1): string { + assert(isHex(value), `Expected hex input value, found '${value}' instead`); + + if (bitLength === -1) { + return value; + } + + const byteLength = -1 * (bitLength / 8) * 2; + + return hexAddPrefix( + `${ZEROS}${hexStripPrefix(value)}`.slice(byteLength) + ); +}; diff --git a/src/hex/fixLength.spec.js b/src/hex/fixLength.spec.js new file mode 100644 index 000000000..1b21bee72 --- /dev/null +++ b/src/hex/fixLength.spec.js @@ -0,0 +1,35 @@ +// ISC, Copyright 2017 Jaco Greeff + +const { hexFixLength } = require('./index'); + +describe('hexFixLength', () => { + it('throws an exception on non-hex inputs', () => { + expect( + () => hexFixLength('notAHex') + ).toThrow(/Expected hex input value/); + }); + + it('returns bitLength === -1 as-is', () => { + expect( + hexFixLength('0x12345678') + ).toEqual('0x12345678'); + }); + + it('trims values when bitLength > length', () => { + expect( + hexFixLength('0x12345678', 16) + ).toEqual('0x5678'); + }); + + it('adds zeros when bitLength < length', () => { + expect( + hexFixLength('0x1234', 32) + ).toEqual('0x00001234'); + }); + + it('does not change when bitlength === length', () => { + expect( + hexFixLength('0x12345678', 32) + ).toEqual('0x12345678'); + }); +}); diff --git a/src/hex/index.js b/src/hex/index.js index 806f66861..42ef0ad37 100644 --- a/src/hex/index.js +++ b/src/hex/index.js @@ -2,6 +2,7 @@ // @flow const hexAddPrefix = require('./addPrefix'); +const hexFixLength = require('./fixLength'); const hexFromBn = require('./fromBn'); const hexFromBuffer = require('./fromBuffer'); const hexFromNumber = require('./fromNumber'); @@ -16,6 +17,7 @@ const hexToNumber = require('./toNumber'); */ module.exports = { hexAddPrefix, + hexFixLength, hexFromBn, hexFromBuffer, hexFromNumber, diff --git a/yarn.lock b/yarn.lock index 8861b84bb..9e79e470a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -60,9 +60,9 @@ dependencies: babel-runtime "^6.26.0" -"@polkadot/dev@^0.12.6": - version "0.12.6" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.12.6.tgz#1b2ffec13b3ebfc0a0e58253ee5eb8495482d91d" +"@polkadot/dev@^0.13.1": + version "0.13.1" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.13.1.tgz#7fd7bb6f5d343b0bbfa277abe24c46de909e854c" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0"