Hone logo
Hone
Problems

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:

  1. byteLength(array: ArrayLike<number>): number: Returns the byte length of the Typed Array. This is equivalent to array.byteLength but provides a more consistent API.
  2. subarray(array: ArrayLike<number>, start?: number, end?: number): ArrayLike<number>: Creates a view (subarray) of the Typed Array. This should behave identically to array.subarray(), but again, provides a more consistent API. start and end are optional and should default to 0 and array.length respectively if not provided.
  3. set(array: ArrayLike<number>, offset: number, source: ArrayLike<number>): void: Copies elements from source to array starting at offset. This should behave identically to array.set().
  4. fill(array: ArrayLike<number>, value: number, start?: number, end?: number): void: Fills the Typed Array with a given value from start to end. This should behave identically to array.fill(). start and end are optional and should default to 0 and array.length respectively if not provided.
  5. 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 subarray function should handle optional start and end parameters correctly, defaulting to 0 and array.length respectively.
  • The fill function should handle optional start and end parameters correctly, defaulting to 0 and array.length respectively.
  • The functions should not modify the original Typed Array unless explicitly specified (e.g., fill, set).
  • The toArray function 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 a length property 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 start or end values in subarray and fill. For example, start and end should 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.
Loading editor...
typescript