Scala.js - How to convert ArrayBuffer to Array[Byte]?

90 Views Asked by At

DOM API returns ArrayBuffer but what I need is Array[Byte]. How can I do a conversion?

import scala.scalajs.js.typedarray.ArrayBuffer

def toScalaArray(input: ArrayBuffer): Array[Byte] = {
  // code in question
}

1

There are 1 best solutions below

0
sjrd On BEST ANSWER

Wrap it in an Int8Array, then call the extension method .toArray defined in the typedarray package:

import scala.scalajs.js.typedarray._

def toScalaArray(input: ArrayBuffer): Array[Byte] =
  new Int8Array(input).toArray