Don't pad fixLength by default (#51)

* Don't pad fixLength by default

* Update docs
This commit is contained in:
Jaco Greeff
2017-12-15 20:59:27 +01:00
committed by GitHub
parent 25be6e55df
commit 366965371f
3 changed files with 37 additions and 25 deletions
+6 -5
View File
@@ -3,7 +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
- [hexFixLength](#hexfixlength) 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.
@@ -32,19 +32,20 @@ console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
## hexFixLength
Pads or shifts a hex string to a specific bitLength
Shifts a hex string to a specific bitLength
```js
hexFixLength (value: string, bitLength: number = -1): string
hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): 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.
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). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`.
```js
import { hexFixLength } from '@polkadot/util';
console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012
console.log('fixed', hexFixLength('0x12', 16)) // => 0x12
console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12
```
+10 -11
View File
@@ -9,28 +9,27 @@ const { ZEROS_256 } = require('./constants');
/**
@name hexFixLength
@signature hexFixLength (value: string, bitLength: number = -1): string
@summary Pads or shifts a hex string to a specific bitLength
@signature hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string
@summary 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.
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). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`.
@example
import { hexFixLength } from '@polkadot/util';
console.log('fixed', hexFixLength('0x12', 16)) // => 0x0012
console.log('fixed', hexFixLength('0x12', 16)) // => 0x12
console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12
*/
module.exports = function hexFixLength (value: string, bitLength: number = -1): string {
module.exports = function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string {
assert(isHex(value), `Expected hex input value, found '${value}' instead`);
if (bitLength === -1) {
return value;
}
const strLength = bitLength / 4;
const hexLength = strLength + 2;
if (value.length === hexLength) {
return value;
if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) {
return hexAddPrefix(
hexStripPrefix(value)
);
}
if (value.length > hexLength) {
+21 -9
View File
@@ -15,16 +15,10 @@ describe('hexFixLength', () => {
).toEqual('0x12345678');
});
it('trims values when bitLength > length', () => {
it('returns bitLength === -1 as-is (adding missing 0)', () => {
expect(
hexFixLength('0x12345678', 16)
).toEqual('0x5678');
});
it('adds zeros when bitLength < length', () => {
expect(
hexFixLength('0x1234', 32)
).toEqual('0x00001234');
hexFixLength('0x1234567')
).toEqual('0x01234567');
});
it('does not change when bitlength === length', () => {
@@ -32,4 +26,22 @@ describe('hexFixLength', () => {
hexFixLength('0x12345678', 32)
).toEqual('0x12345678');
});
it('trims values when bitLength > length', () => {
expect(
hexFixLength('0x12345678', 16)
).toEqual('0x5678');
});
it('returns as-is when bitLength < length', () => {
expect(
hexFixLength('0x1234', 32)
).toEqual('0x1234');
});
it('adds zeros when bitLength < length (withPadded)', () => {
expect(
hexFixLength('0x1234', 32, true)
).toEqual('0x00001234');
});
});