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
- func Basename(p, ext string) string
- func DateMs(ms float64) float64
- func DateValue(v any) float64
- func DecodeUTF8(b []byte) string
- func ISOString(ms float64) (string, bool)
- func IsSpace(r rune) bool
- func LastIndexOf(s, sub string, from int) int
- func Len(s string) int
- func Number(f float64) string
- func ParseDate(s string) float64
- func ParseInt(s string, radix int) float64
- func Query(raw string) url.Values
- func Slice(s string, n int) string
- func String(v any) string
- func TimeMs(t time.Time) float64
- func Trim(s string) string
- func Truncate(s string, n int) string
- func Truthy(v any) bool
- type Param
- type UTF8Decoder
Constants ¶
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 DateMs ¶
DateMs is the time of the Date Node makes of a file time: the milliseconds rounded (Math.round).
func DateValue ¶
DateValue is new Date(v).getTime() for a value decoded from JSON: NaN for what is not a date.
func DecodeUTF8 ¶
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 ¶
ISOString is Date.prototype.toISOString; ok is false where it throws (an invalid date).
func IsSpace ¶
IsSpace is JavaScript's WhiteSpace or LineTerminator (what \s and String.prototype.trim treat as space).
func LastIndexOf ¶
LastIndexOf is s.lastIndexOf(sub, from) in UTF-16 code units, -1 when absent.
func ParseDate ¶
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 ¶
ParseInt is parseInt(s, radix); radix 0 is parseInt without one (a 0x prefix reads hex). NaN when nothing parses.
func Query ¶
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 ¶
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 ¶
String is String(v) for a JSON scalar; objects and arrays print as JavaScript prints them only roughly.
func TimeMs ¶
TimeMs is a file time as Node's fs.Stats gives it in *Ms: milliseconds with the fraction of the nanoseconds.
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).
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.