mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-23 01:28:01 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.u8aFixLength = u8aFixLength;
|
||||
/**
|
||||
* @name u8aFixLength
|
||||
* @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
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { u8aFixLength } from '@pezkuwi/util';
|
||||
*
|
||||
* u8aFixLength('0x12') // => 0x12
|
||||
* u8aFixLength('0x12', 16) // => 0x0012
|
||||
* u8aFixLength('0x1234', 8) // => 0x12
|
||||
* ```
|
||||
*/
|
||||
function u8aFixLength(value, bitLength = -1, atStart = false) {
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
if (bitLength === -1 || value.length === byteLength) {
|
||||
return value;
|
||||
}
|
||||
else if (value.length > byteLength) {
|
||||
return value.subarray(0, byteLength);
|
||||
}
|
||||
const result = new Uint8Array(byteLength);
|
||||
result.set(value, atStart ? 0 : (byteLength - value.length));
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user