Skip to main content
Module

x/codepoint_iterator/mod.ts>asCodePointsArray

Fast uint8array to utf-8 codepoint iterator for streams and array buffers by @okikio & @jonathantneal
Latest
function asCodePointsArray
import { asCodePointsArray } from "https://deno.land/x/codepoint_iterator@v1.1.1/mod.ts";

Converts an iterable of Uint8Array (byte arrays) into an array of Unicode code points. This is particularly useful for processing streams of text data, where each chunk is represented as a Uint8Array, and you want to work with the text's Unicode code points.

Similar to asCodePointsIterator, this function processes the input iterable to extract UTF-8 characters and calculate their corresponding Unicode code points. However, instead of yielding the code points one by one, it stores them in an array and returns the array once the processing is complete.

Examples

Convert an iterable of Uint8Array chunks into an array of Unicode code points.

async function exampleArrayUsage() {
  const utf8Chunks = [new Uint8Array([0x68, 0x65, 0x6C, 0x6C, 0x6F])]; // Represents the string 'hello'
  const codePoints = await asCodePointsArray(utf8Chunks);
  console.log(codePoints.map(cp => String.fromCodePoint(cp)).join('')); // Output: 'hello'
}
exampleArrayUsage();

Type Parameters

T extends Uint8Array

Parameters

iterable: AsyncIterable<T> | Iterable<T>

The source iterable, either synchronous or asynchronous, containing Uint8Array chunks.

Returns

Promise<number[]>

A promise that resolves to an array of Unicode code points.