Documentation
¶
Overview ¶
Package js holds what the port needs of JavaScript's own behaviour, so that every value the SDK writes, compares or counts is written, compared and counted the same way here: numbers as Number.prototype.toString prints them, JSON.stringify and JSON.parse (objects keep JavaScript's key order), string lengths and cuts in UTF-16 code units, the characters \s matches, and Date's calendar arithmetic.
Index ¶
- Constants
- func ArrayIndex(key string) (uint32, bool)
- func CivilFromDays(z int64) (int64, int64, int64)
- func CloneValue(v any) any
- func DateUTC(year, month, day, hour, minute, second, ms int64) int64
- func DaysFromCivil(y, m, d int64) int64
- func FloorDiv(a, b int64) int64
- func FormatNumber(n float64) string
- func FromUnits(u []uint16) string
- func Head16(s string, n int) string
- func Head16Lone(s string, n int) string
- func ISOString(ms int64) string
- func IsInteger(n float64) bool
- func IsSpace(r rune) bool
- func Length16(s string) int
- func Mod(a, b int64) int64
- func Parse(text string) (any, error)
- func Quote(s string) string
- func Slice16(s string, start, end int) string
- func Slice16Lone(s string, start, end int) string
- func Stringify(v any) string
- func StringifyLone(v any) string
- func Tail16(s string, n int) string
- func Tail16Lone(s string, n int) string
- func Trim(s string) string
- func TrimEnd(s string) string
- func Units(s string) []uint16
- func WellFormed(s string) string
- type Object
- type Valuer
Constants ¶
const MaxSafeInteger = 1<<53 - 1
MaxSafeInteger is Number.MAX_SAFE_INTEGER.
const Whitespace = `\t\n\v\f\r \x{a0}\x{1680}\x{2000}-\x{200a}\x{2028}\x{2029}\x{202f}\x{205f}\x{3000}\x{feff}`
Whitespace is the character class JavaScript's \s matches, and what String.prototype.trim removes: WhiteSpace and LineTerminator, written for a Go character class.
Variables ¶
This section is empty.
Functions ¶
func ArrayIndex ¶
ArrayIndex reports whether a key is an array index, which JavaScript orders before every other key.
func CivilFromDays ¶
CivilFromDays is the date of a day counted from 1970-01-01: year, month (1 to 12) and day.
func DateUTC ¶
DateUTC is Date.UTC(year, month, day, hour, minute, second, ms) with a 0-based month, every field free to overflow into the next, as Date.UTC allows.
func DaysFromCivil ¶
DaysFromCivil is the days since 1970-01-01 of a proleptic Gregorian date, month 1 to 12.
func FormatNumber ¶
FormatNumber is String(n): the shortest digits that read back as n, in plain notation from 1e-7 up to 1e21 and exponential notation outside it, as Number.prototype.toString writes them.
func FromUnits ¶
FromUnits is the text of UTF-16 code units; a lone surrogate becomes U+FFFD, as it does once JavaScript writes it out as UTF-8.
func Head16Lone ¶
Head16Lone is s.slice(0, n), keeping a lone half (Slice16Lone).
func ISOString ¶
ISOString is new Date(ms).toISOString(): "2026-01-05T09:30:00.000Z", with a signed six-digit year outside 0 to 9999.
func Parse ¶
Parse is JSON.parse: objects as *Object in JavaScript's key order (a key given twice keeps its first place and its last value), numbers as float64. A lone surrogate escape (\ud800) becomes U+FFFD.
func Slice16 ¶
Slice16 is s.slice(start, end) in UTF-16 code units, with JavaScript's clamping (a negative index counts from the end). A cut through a surrogate pair keeps the lone half, which is written here as U+FFFD, the character it becomes once written out as UTF-8, so a stored or hashed result is the same bytes.
func Slice16Lone ¶
Slice16Lone is Slice16 keeping the lone half of a surrogate pair the cut goes through, as JavaScript keeps it, for text that goes into a JSON body: the half is held as its three WTF-8 bytes (ED A0..BF 80..BF), which StringifyLone writes as \ud83d, as JSON.stringify does. Anything else that reads the result sees three bytes that are not UTF-8.
func Stringify ¶
Stringify is JSON.stringify: the same bytes for the same value. A number that is not finite is null, as JavaScript writes it.
func StringifyLone ¶
StringifyLone is Stringify for a value whose strings may hold a lone surrogate kept by Slice16Lone: each is written as JSON.stringify writes one (\ud83d), so a body cut through a surrogate pair is the SDK's bytes. Its strings must otherwise be UTF-8 (WellFormed), since the three bytes that hold a lone surrogate are not.
func Tail16Lone ¶
Tail16Lone is s.slice(-n), keeping a lone half (Slice16Lone).
func WellFormed ¶
WellFormed is the text with every byte that is not UTF-8 replaced by U+FFFD, as a JavaScript string decoded from those bytes has it.
Types ¶
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object is a JavaScript object: keys in JavaScript's order, which is every key that is an array index (a canonical whole number below 2^32 - 1) in ascending order, then every other key in the order it was first set. The zero value is an empty object.