js

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
const MaxSafeInteger = 1<<53 - 1

MaxSafeInteger is Number.MAX_SAFE_INTEGER.

View Source
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

func ArrayIndex(key string) (uint32, bool)

ArrayIndex reports whether a key is an array index, which JavaScript orders before every other key.

func CivilFromDays

func CivilFromDays(z int64) (int64, int64, int64)

CivilFromDays is the date of a day counted from 1970-01-01: year, month (1 to 12) and day.

func CloneValue

func CloneValue(v any) any

CloneValue is a deep copy of a JSON value.

func DateUTC

func DateUTC(year, month, day, hour, minute, second, ms int64) int64

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

func DaysFromCivil(y, m, d int64) int64

DaysFromCivil is the days since 1970-01-01 of a proleptic Gregorian date, month 1 to 12.

func FloorDiv

func FloorDiv(a, b int64) int64

FloorDiv is Math.floor(a / b) for whole numbers, b > 0.

func FormatNumber

func FormatNumber(n float64) string

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

func FromUnits(u []uint16) string

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 Head16

func Head16(s string, n int) string

Head16 is s.slice(0, n).

func Head16Lone

func Head16Lone(s string, n int) string

Head16Lone is s.slice(0, n), keeping a lone half (Slice16Lone).

func ISOString

func ISOString(ms int64) string

ISOString is new Date(ms).toISOString(): "2026-01-05T09:30:00.000Z", with a signed six-digit year outside 0 to 9999.

func IsInteger

func IsInteger(n float64) bool

IsInteger is Number.isInteger.

func IsSpace

func IsSpace(r rune) bool

IsSpace reports whether JavaScript's \s matches r.

func Length16

func Length16(s string) int

Length16 is a string's .length: its UTF-16 code units.

func Mod

func Mod(a, b int64) int64

Mod is a modulo whose result has the sign of b, as Python's % has it.

func Parse

func Parse(text string) (any, error)

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 Quote

func Quote(s string) string

Quote is JSON.stringify of a string.

func Slice16

func Slice16(s string, start, end int) string

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

func Slice16Lone(s string, start, end int) string

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

func Stringify(v any) string

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

func StringifyLone(v any) string

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 Tail16

func Tail16(s string, n int) string

Tail16 is s.slice(s.length - n): the last n code units.

func Tail16Lone

func Tail16Lone(s string, n int) string

Tail16Lone is s.slice(-n), keeping a lone half (Slice16Lone).

func Trim

func Trim(s string) string

Trim is String.prototype.trim.

func TrimEnd

func TrimEnd(s string) string

TrimEnd is String.prototype.trimEnd.

func Units

func Units(s string) []uint16

Units is the string as UTF-16 code units, as JavaScript holds it.

func WellFormed

func WellFormed(s string) string

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.

func NewObject

func NewObject(pairs ...any) *Object

NewObject is an object holding these key and value pairs, in turn.

func (*Object) Clone

func (o *Object) Clone() *Object

Clone is a deep copy, as JSON.parse(JSON.stringify(o)) makes one.

func (*Object) Delete

func (o *Object) Delete(key string)

Delete removes key.

func (*Object) Get

func (o *Object) Get(key string) (any, bool)

Get is the value at key, and whether the key is there.

func (*Object) Has

func (o *Object) Has(key string) bool

Has reports whether the key is there.

func (*Object) Keys

func (o *Object) Keys() []string

Keys is Object.keys: the keys in JavaScript's order.

func (*Object) Len

func (o *Object) Len() int

Len is how many keys there are.

func (*Object) Set

func (o *Object) Set(key string, value any)

Set gives key the value: a new key takes its place in JavaScript's order, a key already there keeps its place.

type Valuer

type Valuer interface {
	JSValue() any
}

Valuer is a type that writes itself as a JSON value.

Jump to

Keyboard shortcuts

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