JSXxxjsXxx functions

This commit is contained in:
Sybren A. Stüvel 2022-01-13 16:35:31 +01:00
parent 0629728ce9
commit c36bc3ebbd
3 changed files with 17 additions and 17 deletions

View File

@ -32,22 +32,22 @@ import (
)
// ----------------------------------------------------------
// Functions that start with `JS` are exposed to JavaScript.
// Functions that start with `js` are exposed to JavaScript.
// See newGojaVM() for the actual expose-as-globals code.
// ----------------------------------------------------------
func JSPrint(call goja.FunctionCall) goja.Value {
func jsPrint(call goja.FunctionCall) goja.Value {
log.Info().Interface("args", call.Arguments).Msg("print")
return goja.Undefined()
}
func JSAlert(call goja.FunctionCall) goja.Value {
func jsAlert(call goja.FunctionCall) goja.Value {
log.Warn().Interface("args", call.Arguments).Msg("alert")
return goja.Undefined()
}
// JSFormatTimestampLocal returns the timestamp formatted as local time in a way that's compatible with filenames.
func JSFormatTimestampLocal(timestamp time.Time) string {
// jsFormatTimestampLocal returns the timestamp formatted as local time in a way that's compatible with filenames.
func jsFormatTimestampLocal(timestamp time.Time) string {
return timestamp.Local().Format("2006-01-02_150405")
}
@ -87,14 +87,14 @@ const (
chunkBlender = ".."
)
// JSFrameChunker takes a range like "1..10,20..25,40" and returns chunked ranges.
// jsFrameChunker takes a range like "1..10,20..25,40" and returns chunked ranges.
//
// The returned ranges will be at most `chunkSize` frames long.
//
// Supports "regular" and "blender" notation, resp. "A-Z" and "A..Z". Returned
// chunks will always be in "regular" notation because they're more compatible
// with embedding in filenames.
func JSFrameChunker(frameRange string, chunkSize int) ([]string, error) {
func jsFrameChunker(frameRange string, chunkSize int) ([]string, error) {
frameRange = strings.TrimSpace(frameRange)
if len(frameRange) == 0 {
return nil, errInvalidRange(frameRange, "empty range")

View File

@ -27,35 +27,35 @@ import (
)
func TestFrameChunkerHappyBlenderStyle(t *testing.T) {
chunks, err := JSFrameChunker("1..10,20..25,40,3..8", 4)
chunks, err := jsFrameChunker("1..10,20..25,40,3..8", 4)
assert.Nil(t, err)
assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks)
}
func TestFrameChunkerHappySmallInput(t *testing.T) {
// No frames, should be an error
chunks, err := JSFrameChunker(" ", 4)
chunks, err := jsFrameChunker(" ", 4)
assert.ErrorIs(t, err, ErrInvalidRange{Message: "empty range"})
// Just one frame.
chunks, err = JSFrameChunker("47", 4)
chunks, err = jsFrameChunker("47", 4)
assert.Nil(t, err)
assert.Equal(t, []string{"47"}, chunks)
// Just one range of exactly one chunk.
chunks, err = JSFrameChunker("1-3", 3)
chunks, err = jsFrameChunker("1-3", 3)
assert.Nil(t, err)
assert.Equal(t, []string{"1-3"}, chunks)
}
func TestFrameChunkerHappyRegularStyle(t *testing.T) {
chunks, err := JSFrameChunker("1-10,20-25,40", 4)
chunks, err := jsFrameChunker("1-10,20-25,40", 4)
assert.Nil(t, err)
assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks)
}
func TestFrameChunkerHappyExtraWhitespace(t *testing.T) {
chunks, err := JSFrameChunker(" 1 .. 10,\t20..25\n,40 ", 4)
chunks, err := jsFrameChunker(" 1 .. 10,\t20..25\n,40 ", 4)
assert.Nil(t, err)
assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks)
}

View File

@ -97,10 +97,10 @@ func (s *Service) newGojaVM() *goja.Runtime {
}
// Set some global functions.
mustSet("print", JSPrint)
mustSet("alert", JSAlert)
mustSet("frameChunker", JSFrameChunker)
mustSet("formatTimestampLocal", JSFormatTimestampLocal)
mustSet("print", jsPrint)
mustSet("alert", jsAlert)
mustSet("frameChunker", jsFrameChunker)
mustSet("formatTimestampLocal", jsFormatTimestampLocal)
// Pre-import some useful modules.
s.registry.Enable(vm)