Skip to content

Instantly share code, notes, and snippets.

@chiro-hiro
Last active May 19, 2023 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiro-hiro/7346d910313e78633ac5185aeaba04c3 to your computer and use it in GitHub Desktop.
Save chiro-hiro/7346d910313e78633ac5185aeaba04c3 to your computer and use it in GitHub Desktop.
Concat Uint8Array

Searching on the Internet to find a proper way to concat multiple arrays of Uint8Array but the result made me disapointed. "Why you guys so fucking lazy?", I asked.

So, I wrote mine so you can steal it genterly under the witness of MIT License. If you are end up here you are fucking lazy, you should do something about your life. Btw, don't just steal it leave me a damn star, Jesus!!.

TypeScript

function concatUint8Array(input: Uint8Array[], size?: number): Uint8Array {
  let concatedSize = 0;
  if (typeof size === 'undefined') {
    for (let i = 0; i < input.length; i += 1) {
      concatedSize += input[i].length;
    }
  } else {
    concatedSize = size;
  }
  const newArray = new Uint8Array(concatedSize);
  let pointer = 0;
  for (let i = 0; i < input.length; i += 1) {
    newArray.set(input[i], pointer);
    pointer += input[i].length;
  }
  return newArray;
}

JavaScript

function concatUint8Array(input, size) {
    let concatedSize = 0;
    if (typeof size === 'undefined') {
        for (let i = 0; i < input.length; i += 1) {
            concatedSize += input[i].length;
        }
    }
    else {
        concatedSize = size;
    }
    const newArray = new Uint8Array(concatedSize);
    let pointer = 0;
    for (let i = 0; i < input.length; i += 1) {
        newArray.set(input[i], pointer);
        pointer += input[i].length;
    }
    return newArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment