Add hexFixLength documentation (#44)

This commit is contained in:
Jaco Greeff
2017-12-07 21:27:58 +01:00
committed by GitHub
parent 0dafb78bb4
commit f9eeb4ee70
3 changed files with 52 additions and 3 deletions
+19
View File
@@ -3,6 +3,7 @@
Internal utilities to create and test for hex values
- [hexAddPrefix](#hexaddprefix) Adds the `0x` prefix to string values.
- [hexFixLength](#hexfixlength) Pads or shifts a hex string to a specific bitLength
- [hexFromBn](#hexfrombn) Creates a hex value from a BN.js bignumber object.
- [hexFromBn](#hexfrombn) Creates a hex value from a Buffer object.
- [hexFromNumber](#hexfromnumber) Creates a hex value from a number.
@@ -29,6 +30,24 @@ import { hexAddPrefix } from '@polkadot/util';
console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
```
## hexFixLength
Pads or shifts a hex string to a specific bitLength
```js
hexFixLength (value: string, bitLength: number = -1): string
```
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Input values with less bits are zero-padded, values with more bits are trimmed to the specified length.
```js
import { hexFixLength } from '@polkadot/util';
console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12
```
## hexFromBn
Creates a hex value from a BN.js bignumber object. [(alias of bnToHex)](bn.md#bntohex)
+8
View File
@@ -0,0 +1,8 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const ZEROS_256 = '0000000000000000000000000000000000000000000000000000000000000000';
module.exports = {
ZEROS_256
};
+25 -3
View File
@@ -5,9 +5,20 @@ const assert = require('../assert');
const isHex = require('../is/hex');
const hexAddPrefix = require('./addPrefix');
const hexStripPrefix = require('./stripPrefix');
const { ZEROS_256 } = require('./constants');
const ZEROS = '0000000000000000000000000000000000000000000000000000000000000000';
/**
@name hexFixLength
@signature hexFixLength (value: string, bitLength: number = -1): string
@summary Pads or shifts a hex string to a specific bitLength
@description
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Input values with less bits are zero-padded, values with more bits are trimmed to the specified length.
@example
import { hexFixLength } from '@polkadot/util';
console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12
*/
module.exports = function hexFixLength (value: string, bitLength: number = -1): string {
assert(isHex(value), `Expected hex input value, found '${value}' instead`);
@@ -15,9 +26,20 @@ module.exports = function hexFixLength (value: string, bitLength: number = -1):
return value;
}
const byteLength = -1 * (bitLength / 8) * 2;
const strLength = bitLength / 4;
const hexLength = strLength + 2;
if (value.length === hexLength) {
return value;
}
if (value.length > hexLength) {
return hexAddPrefix(
hexStripPrefix(value).slice(-1 * strLength)
);
}
return hexAddPrefix(
`${ZEROS}${hexStripPrefix(value)}`.slice(byteLength)
`${ZEROS_256}${hexStripPrefix(value)}`.slice(-1 * strLength)
);
};