1 | // Copyright 2017-2022 @polkadot/util authors & contributors
|
2 | // SPDX-License-Identifier: Apache-2.0
|
3 | import { compactFromU8a } from "./fromU8a.js";
|
4 | /**
|
5 | * @name compactStripLength
|
6 | * @description Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length
|
7 | * @example
|
8 | * <BR>
|
9 | *
|
10 | * ```javascript
|
11 | * import { compactStripLength } from '@polkadot/util';
|
12 | *
|
13 | * console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
|
14 | * ```
|
15 | */
|
16 |
|
17 | export function compactStripLength(input) {
|
18 | const [offset, length] = compactFromU8a(input);
|
19 | const total = offset + length.toNumber();
|
20 | return [total, input.subarray(offset, total)];
|
21 | } |
\ | No newline at end of file |