-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.go
More file actions
47 lines (39 loc) · 1.33 KB
/
js.go
File metadata and controls
47 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package strlang
import (
"fmt"
)
// Javascript represents a JavaScript code generator that extends the Builder struct.
type Javascript struct {
// Builder is an embedded struct that represents
// the base builder for generating the Javascript code.
*Builder
}
// NewJavascript returns a new instance of Javascript code builder.
func NewJavascript() *Javascript {
return &Javascript{
NewBuilder(),
}
}
// If generates an if statement in the generated code.
func (b *Javascript) If(statement string, inside func()) {
b.Block(fmt.Sprintf("if (%s) {", statement), inside, "}")
}
// Else generates an else statement in the generated code.
func (b *Javascript) Else(inside func()) {
b.TrimRight("\n")
b.Block(" else {", inside, "}")
}
// ElseIf generates an else if statement in the generated code.
func (b *Javascript) ElseIf(statement string, inside func()) {
b.TrimRight("\n")
b.Block(fmt.Sprintf(" else if (%s) {", statement), inside, "}")
}
// Object generates an object block with the provided variable type, name, and inside function.
func (b *Javascript) Object(varType, name string, inside func()) {
b.Block(fmt.Sprintf("%s %s = {", varType, name), inside, "}")
}
// Export appends the "export" keyword and allows to define an exportable object or variable.
func (b *Javascript) Export() *Javascript {
b.WriteString("export ")
return b
}