Skip to content
heff

browser.js

JavaScript’s strings: JSString, text as JavaScript has it, UTF-16, beside a String, which is bytes (UTF-8 as a literal writes it, but any byte may be in one). A string is made one by toJS, and one made a string by fromJS; there is nothing else to do with one.

It is for where a program meets JavaScript as it is:

  • a field of an effect’s operation of type JSString is given to its provider, and taken from it, as a JavaScript string, through no helper (a String field is bytes, which a provider gives to the host by $toJs or $toBytes); a provider handing much text from one host API to another (what is in a <textarea>, to another element) keeps it a JSString, made a String only where the program reads it. The library’s own effects (browser.dom, std.io, …) take and give Strings, their providers turning them into what the host has;
  • an ES module (--backend=esm) gives and takes what it is given as it is: a function for JavaScript to call takes and gives JSStrings, greet : JSString -> JSString, and is a String’s inside, by fromJS and toJS (a String it gives is bytes, each a code unit, which JavaScript must decode).

In JavaScript (esm, page) a JavaScript string; in the interpreter a string of UTF-8, valid, which is all a toJS makes — so fromJS (toJS s) is R.toValid s on each. A C program does not import it (E0351), as it does none of the browser’s modules.

JSString

data JSString

Text as JavaScript has it, a JavaScript string: made only by toJS (no value is written of it), and read only by fromJS.

strToJS

strToJS : String -> JSString

strFromJS

strFromJS : JSString -> String

toJS

toJS : String -> JSString

A string’s text as JavaScript’s: its UTF-8 read, what is not UTF-8 in it U+FFFD (each longest start of a rune’s bytes one, as TextDecoder reads them, a byte order mark kept).

fromJS

fromJS : JSString -> String

JavaScript’s text as a string, UTF-8: a surrogate not in a pair U+FFFD (as toWellFormed makes it). Back from toJS, a string is what it was where it was UTF-8.

>>> List.map (\s -> fromJS (toJS s)) ["", "héllo 😀", "a\xffb", "\xed\xa0\x80"]
["", "héllo 😀", "a\u{FFFD}b", "\u{FFFD}\u{FFFD}\u{FFFD}"]