Debian Codegraph @

opts/js.go

raw symbols SCIP
1 package opts
2
3 import (
4 "fmt"
5 "regexp"
6
7 "github.com/go-echarts/go-echarts/v2/types"
8 )
9
10 const funcMarker = "__f__"
11
12 var (
13 newlineTabPat = regexp.MustCompile(`\n|\t`)
14 commentPat = regexp.MustCompile(`(//.*)\n`)
15 )
16
17 type JSFunctions struct {
18 Fns []types.FuncStr
19 }
20
21 // AddJSFuncs adds a new JS function, for back compatibility before v2.4
22 // Plz use the AddJSFuncStrs instead
23 func (f *JSFunctions) AddJSFuncs(fn ...string) {
24 for i := 0; i < len(fn); i++ {
25 f.Fns = append(f.Fns, types.FuncStr(newlineTabPat.ReplaceAllString(fn[i], "")))
26 }
27 }
28
29 // AddJSFuncStrs adds a new JS function string.
30 func (f *JSFunctions) AddJSFuncStrs(fn ...types.FuncStr) {
31 for i := 0; i < len(fn); i++ {
32 f.Fns = append(f.Fns, types.FuncStr(newlineTabPat.ReplaceAllString(string(fn[i]), "")))
33 }
34 }
35
36 // FuncOpts returns a string suitable for options expecting pure JavaScript code insertion.
37 func FuncOpts(fn string) types.FuncStr {
38 return types.FuncStr(replaceJsFuncs(fn))
39 }
40
41 // FuncStripCommentsOpts returns a string suitable for options expecting JavaScript code,
42 // stripping '//' comments.
43 func FuncStripCommentsOpts(fn string) string {
44 fn = commentPat.ReplaceAllString(fn, "")
45 return replaceJsFuncs(fn)
46 }
47
48 // replace and clear up js functions string
49 func replaceJsFuncs(fn string) string {
50 fn = newlineTabPat.ReplaceAllString(fn, "")
51 return fmt.Sprintf("%s%s%s", funcMarker, fn, funcMarker)
52 }