jscompat

package
v0.16.521 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jscompat reproduces the few JavaScript behaviours the runtime's answers depend on — string trimming and length in UTF-16 units, parseInt, Date parsing and printing, truthiness of JSON values, Node's query strings and path.basename — so a route moved to Go answers byte for byte as the one it replaces.

Index

Constants

View Source
const SpaceClass = `[\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}]`

SpaceClass is a regexp character class matching what JavaScript's \s matches.

Variables

This section is empty.

Functions

func Basename

func Basename(p, ext string) string

Basename is Node's path.basename(p, ext).

func DateMs

func DateMs(ms float64) float64

DateMs is the time of the Date Node makes of a file time: the milliseconds rounded (Math.round).

func DateValue

func DateValue(v any) float64

DateValue is new Date(v).getTime() for a value decoded from JSON: NaN for what is not a date.

func DecodeUTF8

func DecodeUTF8(b []byte) string

DecodeUTF8 is Buffer#toString('utf8') of bytes: each malformed sequence becomes one U+FFFD, as the WHATWG decoder does (Go's own conversion replaces byte by byte), and a byte order mark stays.

func ISOString

func ISOString(ms float64) (string, bool)

ISOString is Date.prototype.toISOString; ok is false where it throws (an invalid date).

func IsSpace

func IsSpace(r rune) bool

IsSpace is JavaScript's WhiteSpace or LineTerminator (what \s and String.prototype.trim treat as space).

func LastIndexOf

func LastIndexOf(s, sub string, from int) int

LastIndexOf is s.lastIndexOf(sub, from) in UTF-16 code units, -1 when absent.

func Len

func Len(s string) int

Len is a string's length in UTF-16 code units, as JavaScript counts it.

func Number

func Number(f float64) string

Number prints a number as JavaScript does.

func ParseDate

func ParseDate(s string) float64

ParseDate is Date.parse for the forms the runtime meets: ISO 8601 dates and date-times (lower case "t"/"z", a space for "T", "+hhmm" offsets and day overflow tolerated as V8 tolerates them). A date alone is UTC, a date-time without an offset is local time. Anything else is NaN.

func ParseInt

func ParseInt(s string, radix int) float64

ParseInt is parseInt(s, radix); radix 0 is parseInt without one (a 0x prefix reads hex). NaN when nothing parses.

func Query

func Query(raw string) url.Values

Query is Node's querystring.parse of a raw query: pairs split on "&" only, "+" as space, bad escapes kept as they are, every value of a repeated key kept.

func Slice

func Slice(s string, n int) string

Slice is s.slice(0, n) in UTF-16 units. A surrogate pair cut in half leaves U+FFFD, which is what a JSON reader makes of the lone surrogate JavaScript would print.

func String

func String(v any) string

String is String(v) for a JSON scalar; objects and arrays print as JavaScript prints them only roughly.

func TimeMs

func TimeMs(t time.Time) float64

TimeMs is a file time as Node's fs.Stats gives it in *Ms: milliseconds with the fraction of the nanoseconds.

func Trim

func Trim(s string) string

Trim is String.prototype.trim.

func Truncate

func Truncate(s string, n int) string

Truncate is the runtime's preview of a message: at most n units, an ellipsis after a cut.

func Truthy

func Truthy(v any) bool

Truthy is JavaScript truthiness of a value decoded from JSON.

Types

type Param

type Param struct {
	Values []string
}

Param is req.query[name] the way a handler reading it "as string" sees it: absent, one value, or several (an array, which as a string is the values joined by commas).

func (Param) Array

func (q Param) Array() bool

func (Param) Present

func (q Param) Present() bool

func (Param) String

func (q Param) String() string

String is the parameter as String(value) would print it, "" when absent.

func (Param) Truthy

func (q Param) Truthy() bool

Truthy: a present value that is not "" (an array always is).

type UTF8Decoder

type UTF8Decoder struct {
	StripBOM bool
	// contains filtered or unexported fields
}

UTF8Decoder is a TextDecoder('utf-8'): Decode with stream keeps an unfinished sequence for the next call, without it an unfinished sequence ends as U+FFFD. StripBOM drops a byte order mark at the start of the stream, as TextDecoder does by default.

func (*UTF8Decoder) Decode

func (d *UTF8Decoder) Decode(b []byte, stream bool) string

Decode turns the next bytes into text.

Jump to

Keyboard shortcuts

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