Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/iter/lib/generators.ts>fromCharCodes

A bunch of utilities for working with iterables, many inspired by the native array methods.
Latest
function fromCharCodes
import { fromCharCodes } from "https://deno.land/x/iter@v3.2.3/lib/generators.ts";

Creates an iterable over a string's char codes.

Note that, in contrast to String.prototype[@@iterator], this does not treat astral codepoints as single characters, but rather as the constituent surrogate pair. Each char code is therefore between 0x0000 and 0xffff = 2¹⁶ - 1. See the example below.

Examples

Example 1

import * as iter from "https://deno.land/x/iter/mod.ts";

const str = "🦀💦🥱";
const chars = Uint16Array.from(iter.create.fromCharCodes(str));

console.log(chars.length); // -> 6
console.log(chars[0].toString(16)); // -> d83e
console.log(chars[1].toString(16)); // -> dd80
console.log(chars[2].toString(16)); // -> d83d
console.log(chars[3].toString(16)); // -> dca6
console.log(chars[4].toString(16)); // -> d83e
console.log(chars[5].tostring(16)); // -> dd71
console.log("\ud83e\udd80\ud83d\udca6\ud83e\udd71"); // -> 🦀💦🥱

Parameters

str: string
  • A string to extract char codes from.

Returns

An iterable over the char codes.