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. // 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") log.Info().Interface("args", call.Arguments).Msg("print")
return goja.Undefined() 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") log.Warn().Interface("args", call.Arguments).Msg("alert")
return goja.Undefined() return goja.Undefined()
} }
// JSFormatTimestampLocal returns the timestamp formatted as local time in a way that's compatible with filenames. // jsFormatTimestampLocal returns the timestamp formatted as local time in a way that's compatible with filenames.
func JSFormatTimestampLocal(timestamp time.Time) string { func jsFormatTimestampLocal(timestamp time.Time) string {
return timestamp.Local().Format("2006-01-02_150405") return timestamp.Local().Format("2006-01-02_150405")
} }
@ -87,14 +87,14 @@ const (
chunkBlender = ".." 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. // The returned ranges will be at most `chunkSize` frames long.
// //
// Supports "regular" and "blender" notation, resp. "A-Z" and "A..Z". Returned // Supports "regular" and "blender" notation, resp. "A-Z" and "A..Z". Returned
// chunks will always be in "regular" notation because they're more compatible // chunks will always be in "regular" notation because they're more compatible
// with embedding in filenames. // with embedding in filenames.
func JSFrameChunker(frameRange string, chunkSize int) ([]string, error) { func jsFrameChunker(frameRange string, chunkSize int) ([]string, error) {
frameRange = strings.TrimSpace(frameRange) frameRange = strings.TrimSpace(frameRange)
if len(frameRange) == 0 { if len(frameRange) == 0 {
return nil, errInvalidRange(frameRange, "empty range") return nil, errInvalidRange(frameRange, "empty range")

View File

@ -27,35 +27,35 @@ import (
) )
func TestFrameChunkerHappyBlenderStyle(t *testing.T) { 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.Nil(t, err)
assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks) assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks)
} }
func TestFrameChunkerHappySmallInput(t *testing.T) { func TestFrameChunkerHappySmallInput(t *testing.T) {
// No frames, should be an error // No frames, should be an error
chunks, err := JSFrameChunker(" ", 4) chunks, err := jsFrameChunker(" ", 4)
assert.ErrorIs(t, err, ErrInvalidRange{Message: "empty range"}) assert.ErrorIs(t, err, ErrInvalidRange{Message: "empty range"})
// Just one frame. // Just one frame.
chunks, err = JSFrameChunker("47", 4) chunks, err = jsFrameChunker("47", 4)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, []string{"47"}, chunks) assert.Equal(t, []string{"47"}, chunks)
// Just one range of exactly one chunk. // Just one range of exactly one chunk.
chunks, err = JSFrameChunker("1-3", 3) chunks, err = jsFrameChunker("1-3", 3)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, []string{"1-3"}, chunks) assert.Equal(t, []string{"1-3"}, chunks)
} }
func TestFrameChunkerHappyRegularStyle(t *testing.T) { 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.Nil(t, err)
assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks) assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks)
} }
func TestFrameChunkerHappyExtraWhitespace(t *testing.T) { 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.Nil(t, err)
assert.Equal(t, []string{"1-4", "5-8", "9,10,20,21", "22-25", "40"}, chunks) 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. // Set some global functions.
mustSet("print", JSPrint) mustSet("print", jsPrint)
mustSet("alert", JSAlert) mustSet("alert", jsAlert)
mustSet("frameChunker", JSFrameChunker) mustSet("frameChunker", jsFrameChunker)
mustSet("formatTimestampLocal", JSFormatTimestampLocal) mustSet("formatTimestampLocal", jsFormatTimestampLocal)
// Pre-import some useful modules. // Pre-import some useful modules.
s.registry.Enable(vm) s.registry.Enable(vm)