@dioxide-js/web3.js
Version:
dioxide javascript API
43 lines (36 loc) • 1.05 kB
text/typescript
export function concat(...args: ArrayBuffer[]) {
let length = 0
const units = args.map((arg) => {
return new Uint8Array(arg)
})
// Get the total length of all arrays.
units.forEach((item) => {
length += item.length
})
// Create a new array with total length and merge all source arrays.
const mergedArray = new Uint8Array(length)
let offset = 0
units.forEach((item) => {
mergedArray.set(item, offset)
offset += item.length
})
// Should print an array with length 90788 (5x 16384 + 8868 your source arrays)
return mergedArray
}
export function stringToUint8Array(message: string): Uint8Array {
return new TextEncoder().encode(message)
}
export function uint8ArrayToString(content: Uint8Array) {
return new TextDecoder('utf-8').decode(content)
}
export function areUint8ArraysEqual(arr1: Uint8Array, arr2: Uint8Array) {
if (arr1.length !== arr2.length) {
return false
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false
}
}
return true
}