jscompat

package
v0.0.1-beta.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package jscompat gives Go code what JavaScript does differently from Go's standard library: string offsets in UTF-16 code units, Math.round's rounding, and, in a js build, the engine's Math functions, Unicode normalization and regular expressions (RegExp).

The functions of this file are plain Go and build everywhere. With goesm, the others call the engine directly through //goesm:import; with Go's own js/wasm port, through syscall/js.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteOffset

func ByteOffset(s string, u int) int

ByteOffset returns the byte offset in s of the offset u in UTF-16 code units. An offset between the two halves of a surrogate pair, which Go cannot represent, is that of the character's start; one past the end is len(s).

func Round

func Round(x float64) float64

Round returns x rounded to the nearest integer as JavaScript's Math.round does: halves round toward positive infinity, so Round(-2.5) is -2, where math.Round(-2.5) is -3.

func UTF16Len

func UTF16Len(s string) int

UTF16Len returns the length of s in UTF-16 code units, which is JavaScript's length of the same text. An invalid byte counts as one unit, as the U+FFFD it becomes.

func UTF16Offset

func UTF16Offset(s string, i int) int

UTF16Offset returns the offset in UTF-16 code units, as JavaScript's string indices and the selection of an input element count, of the byte offset i of s. An offset inside a character is that of the character's start.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL