-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat.ts
43 lines (36 loc) · 1.15 KB
/
concat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { ErrorType } from '../../errors/utils.js'
import type { ByteArray, Hex } from '../../types/misc.js'
export type ConcatReturnType<TValue extends Hex | ByteArray> =
TValue extends Hex ? Hex : ByteArray
export type ConcatErrorType =
| ConcatBytesErrorType
| ConcatHexErrorType
| ErrorType
export function concat<TValue extends Hex | ByteArray>(
values: readonly TValue[],
): ConcatReturnType<TValue> {
if (typeof values[0] === 'string')
return concatHex(values as readonly Hex[]) as ConcatReturnType<TValue>
return concatBytes(values as readonly ByteArray[]) as ConcatReturnType<TValue>
}
export type ConcatBytesErrorType = ErrorType
export function concatBytes(values: readonly ByteArray[]): ByteArray {
let length = 0
for (const arr of values) {
length += arr.length
}
const result = new Uint8Array(length)
let offset = 0
for (const arr of values) {
result.set(arr, offset)
offset += arr.length
}
return result
}
export type ConcatHexErrorType = ErrorType
export function concatHex(values: readonly Hex[]): Hex {
return `0x${(values as Hex[]).reduce(
(acc, x) => acc + x.replace('0x', ''),
'',
)}`
}