Typed Array Utility Functions in TypeScript
This challenge focuses on building a suite of utility functions for working with Typed Arrays in TypeScript. Typed Arrays provide a way to work with raw binary data in JavaScript, and are essential for performance-critical operations like image processing, audio manipulation, and game development. Creating reusable utility functions will improve code clarity and reduce redundancy when dealing with these arrays.
Problem Description
You are tasked with creating a module containing several utility functions for Typed Arrays. These functions should be generic, accepting any Typed Array type (e.g., Int8Array, Uint32Array, Float64Array) as a parameter. The module should include the following functions:
byteLength(array: ArrayLike<number>): number: Returns the byte length of the Typed Array. This is equivalent toarray.byteLengthbut provides a more consistent API.subarray(array: ArrayLike<number>, start?: number, end?: number): ArrayLike<number>: Creates a view (subarray) of the Typed Array. This should behave identically toarray.subarray(), but again, provides a more consistent API.startandendare optional and should default to 0 andarray.lengthrespectively if not provided.set(array: ArrayLike<number>, offset: number, source: ArrayLike<number>): void: Copies elements fromsourcetoarraystarting atoffset. This should behave identically toarray.set().fill(array: ArrayLike<number>, value: number, start?: number, end?: number): void: Fills the Typed Array with a givenvaluefromstarttoend. This should behave identically toarray.fill().startandendare optional and should default to 0 andarray.lengthrespectively if not provided.toArray(array: ArrayLike<number>): number[]: Converts a Typed Array to a standard JavaScript array of numbers.
Examples
Example 1:
Input:
const uint8Array = new Uint8Array([1, 2, 3, 4, 5]);
Output:
4
Explanation:
byteLength(uint8Array) returns the byte length of the Uint8Array, which is 5 bytes.
Example 2:
Input:
const int16Array = new Int16Array([1, 2, 3, 4, 5]);
const subarray = subarray(int16Array, 1, 4);
Output:
Int16Array [2, 3, 4]
Explanation:
subarray(int16Array, 1, 4) creates a subarray starting at index 1 and ending at index 4 (exclusive), resulting in [2, 3, 4].
Example 3:
Input:
const uint32Array = new Uint32Array([1, 2, 3, 4]);
const sourceArray = [5, 6, 7];
set(uint32Array, 1, sourceArray);
Output:
Uint32Array [1, 5, 6, 7]
Explanation:
set(uint32Array, 1, sourceArray) copies the elements of sourceArray (5, 6, 7) into uint32Array starting at index 1, overwriting the existing values.
Example 4:
Input:
const float32Array = new Float32Array([1.0, 2.0, 3.0, 4.0]);
fill(float32Array, 0.0);
Output:
Float32Array [0, 0, 0, 0]
Explanation:
fill(float32Array, 0.0) fills the entire array with the value 0.0.
Example 5:
Input:
const int8Array = new Int8Array([10, 20, 30, 40]);
const jsArray = toArray(int8Array);
Output:
[10, 20, 30, 40]
Explanation:
toArray(int8Array) converts the Int8Array to a standard JavaScript array.
Constraints
- All functions must be generic and accept any
ArrayLike<number>as input. - The
subarrayfunction should handle optionalstartandendparameters correctly, defaulting to 0 andarray.lengthrespectively. - The
fillfunction should handle optionalstartandendparameters correctly, defaulting to 0 andarray.lengthrespectively. - The functions should not modify the original Typed Array unless explicitly specified (e.g.,
fill,set). - The
toArrayfunction should return a new JavaScript array, not a view of the Typed Array. - Performance should be considered; avoid unnecessary copying where possible.
Notes
ArrayLike<number>is a useful type to ensure that the input has alengthproperty and elements that are numbers.- Consider using the built-in Typed Array methods where appropriate to avoid re-implementing core functionality. The goal is to provide a more consistent and potentially easier-to-use API.
- Think about how to handle edge cases, such as invalid
startorendvalues insubarrayandfill. For example,startandendshould be clamped to the valid range of indices. - This challenge is designed to test your understanding of TypeScript generics, Typed Arrays, and functional programming principles.