/** * @file 合并 TypeArray */ import type { TypeArray, TypeArrayConstructor } from '../types/type' type ReplaceGeneric = T extends Uint8Array ? Uint8Array : T extends Int8Array ? Int8Array : T extends Uint16Array ? Uint16Array : T extends Int16Array ? Int16Array : T extends Uint32Array ? Uint32Array : T extends Int32Array ? Int32Array : T extends Float32Array ? Float32Array : T extends Float64Array ? Float64Array : T export default function concatTypeArray( constructor: T, arrays: ReplaceGeneric>[] ): ReplaceGeneric> { if (!arrays.length) { return null } if (arrays.length === 1) { return arrays[0] } let totalLength: number = 0 let array: TypeArray for (array of arrays) { totalLength += array.length } let result = new constructor(totalLength) as ReplaceGeneric> let offset = 0 for (array of arrays) { result.set(array, offset) offset += array.length } return result }