Add u8aFixLength (#84)
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name u8aFixLength
|
||||
@signature u8aFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string
|
||||
@summary Shifts a Uint8Array to a specific bitLength
|
||||
@description
|
||||
Returns a uint8Array 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.
|
||||
@example
|
||||
import { u8aFixLength } from '@polkadot/util';
|
||||
|
||||
u8aFixLength('0x12') // => 0x12
|
||||
u8aFixLength('0x12', 16) // => 0x0012
|
||||
u8aFixLength('0x1234', 8) // => 0x12
|
||||
*/
|
||||
module.exports = function u8aFixLength (value: Uint8Array, bitLength: number = -1): Uint8Array {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
if (bitLength === -1 || value.length === byteLength) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value.length > byteLength) {
|
||||
return value.subarray(0, byteLength);
|
||||
}
|
||||
|
||||
const result = new Uint8Array(byteLength);
|
||||
|
||||
result.set(value, byteLength - value.length);
|
||||
|
||||
return result;
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { u8aFixLength } = require('./index');
|
||||
|
||||
describe('u8aFixLength', () => {
|
||||
it('returns bitLength === -1 as-is', () => {
|
||||
expect(
|
||||
u8aFixLength(
|
||||
new Uint8Array([0x12, 0x34, 0x56, 0x78])
|
||||
)
|
||||
).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78]));
|
||||
});
|
||||
|
||||
it('does not change when bitlength === length', () => {
|
||||
expect(
|
||||
u8aFixLength(new Uint8Array([0x12, 0x34, 0x56, 0x78]), 32)
|
||||
).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78]));
|
||||
});
|
||||
|
||||
it('trims values when bitLength > length', () => {
|
||||
expect(
|
||||
u8aFixLength(new Uint8Array([0x12, 0x34, 0x56, 0x78]), 16)
|
||||
).toEqual(new Uint8Array([0x12, 0x34]));
|
||||
});
|
||||
|
||||
it('adds zeros when bitLength < length (withPadded)', () => {
|
||||
expect(
|
||||
u8aFixLength(new Uint8Array([0x12, 0x34]), 32)
|
||||
).toEqual(new Uint8Array([0, 0, 0x12, 0x34]));
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,7 @@
|
||||
// @flow
|
||||
|
||||
const u8aConcat = require('./concat');
|
||||
const u8aFixLength = require('./fixLength');
|
||||
const u8aFromBn = require('./fromBn');
|
||||
const u8aFromBuffer = require('./fromBuffer');
|
||||
const u8aFromHex = require('./fromHex');
|
||||
@@ -19,6 +20,7 @@ const u8aToUtf8 = require('./toUtf8');
|
||||
*/
|
||||
module.exports = {
|
||||
u8aConcat,
|
||||
u8aFixLength,
|
||||
u8aFromBn,
|
||||
u8aFromBuffer,
|
||||
u8aFromHex,
|
||||
|
||||
Reference in New Issue
Block a user