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

x/iter/mod.ts>create.fromChars

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

Creates an iterable over a string's chars.

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 = iter.create.fromChars(str)[Symbol.iterator]()

console.log(chars.next().value === "\ud83e"); // -> true
console.log(chars.next().value === "\udd80"); // -> true
console.log(chars.next().value === "\ud83d"); // -> true
console.log(chars.next().value === "\udca6"); // -> true
console.log(chars.next().value === "\ud83e"); // -> true
console.log(chars.next().value === "\udd71"); // -> true
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.