diff --git a/main.go b/main.go index bf27934..2c9b17a 100644 --- a/main.go +++ b/main.go @@ -135,6 +135,12 @@ func main() { return } if lang == "js" { + simplePath, ok := SplitSimplePath(args) + if ok { + output := GetBySimplePath(object, simplePath) + Echo(output, theme) + os.Exit(0) + } vm, fn, err := CreateJS(args, fxrc) if err != nil { fmt.Println(err) diff --git a/pkg/reducer/js.go b/pkg/reducer/js.go index 169ddd6..7051f85 100644 --- a/pkg/reducer/js.go +++ b/pkg/reducer/js.go @@ -19,13 +19,6 @@ func js(args []string, fxrc string) string { for i, a := range args { rs += " try {" switch { - case a == ".": - rs += ` - x = function () - { return this } - .call(x) -` - case flatMapRegex.MatchString(a): code := fold(strings.Split(a, "[]")) rs += fmt.Sprintf( @@ -100,6 +93,6 @@ func ReduceJS(vm *goja.Runtime, reduce goja.Callable, input interface{}, theme T fmt.Print(output) return 0 } - echo(object, theme) + Echo(object, theme) return 0 } diff --git a/pkg/reducer/node.go b/pkg/reducer/node.go index 3773e4c..9113715 100644 --- a/pkg/reducer/node.go +++ b/pkg/reducer/node.go @@ -32,13 +32,6 @@ func nodejs(args []string, fxrc string) string { for i, a := range args { rs += " try {" switch { - case a == ".": - rs += ` - x = function () - { return this } - .call(x) -` - case flatMapRegex.MatchString(a): code := fold(strings.Split(a, "[]")) rs += fmt.Sprintf( diff --git a/pkg/reducer/python.go b/pkg/reducer/python.go index 888ca61..6b965d0 100644 --- a/pkg/reducer/python.go +++ b/pkg/reducer/python.go @@ -17,20 +17,12 @@ var templatePython string func python(args []string) string { rs := "\n" for i, a := range args { - rs += "try:" - switch { - case a == ".": - rs += ` - x = x -` - - default: - rs += fmt.Sprintf( - ` + rs += fmt.Sprintf( + `try: f = (lambda x: (%v))(x) x = f(x) if callable(f) else f `, a) - } + // Generate a beautiful error message. rs += "except Exception as e:\n" pre, post, pointer := trace(args, i) diff --git a/pkg/reducer/reduce.go b/pkg/reducer/reduce.go index 5fd142c..736243a 100644 --- a/pkg/reducer/reduce.go +++ b/pkg/reducer/reduce.go @@ -28,10 +28,10 @@ func GenerateCode(lang string, args []string, fxrc string) string { } func Reduce(input interface{}, lang string, args []string, theme Theme, fxrc string) int { - path, ok := splitPath(args) + path, ok := SplitSimplePath(args) if ok { - output := getByPath(input, path) - echo(output, theme) + output := GetBySimplePath(input, path) + Echo(output, theme) return 0 } var cmd *exec.Cmd @@ -68,7 +68,7 @@ func Reduce(input interface{}, lang string, args []string, theme Theme, fxrc str fmt.Print(string(output)) return 0 } - echo(object, theme) + Echo(object, theme) if dec.InputOffset() < int64(len(output)) { fmt.Print(string(output[dec.InputOffset():])) } diff --git a/pkg/reducer/ruby.go b/pkg/reducer/ruby.go index 82684be..1b7aa22 100644 --- a/pkg/reducer/ruby.go +++ b/pkg/reducer/ruby.go @@ -17,19 +17,10 @@ var templateRuby string func ruby(args []string) string { rs := "\n" for i, a := range args { - rs += "begin" - switch { - case a == ".": - rs += ` - x = x -` - - default: - rs += fmt.Sprintf( - ` + rs += fmt.Sprintf( + `begin x = lambda {|x| %v }.call(x) `, a) - } // Generate a beautiful error message. rs += "rescue Exception => e\n" pre, post, pointer := trace(args, i) diff --git a/pkg/reducer/simple.go b/pkg/reducer/simple.go index 2d575d1..c4a7e36 100644 --- a/pkg/reducer/simple.go +++ b/pkg/reducer/simple.go @@ -24,7 +24,7 @@ const ( singleQuoteEscape ) -func splitPath(args []string) ([]interface{}, bool) { +func SplitSimplePath(args []string) ([]interface{}, bool) { path := make([]interface{}, 0) for _, arg := range args { s := "" @@ -180,7 +180,7 @@ func isProp(ch rune) bool { return unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '_' || ch == '$' } -func getByPath(object interface{}, path []interface{}) interface{} { +func GetBySimplePath(object interface{}, path []interface{}) interface{} { for _, get := range path { switch get := get.(type) { case string: diff --git a/pkg/reducer/simple_test.go b/pkg/reducer/simple_test.go index e9828d0..66a87e8 100644 --- a/pkg/reducer/simple_test.go +++ b/pkg/reducer/simple_test.go @@ -83,7 +83,7 @@ func Test_splitPath(t *testing.T) { } for _, tt := range tests { t.Run(strings.Join(tt.args, " "), func(t *testing.T) { - path, ok := splitPath(tt.args) + path, ok := SplitSimplePath(tt.args) require.Equal(t, tt.want, path) require.True(t, ok) }) @@ -130,7 +130,7 @@ func Test_splitPath_negative(t *testing.T) { } for _, tt := range tests { t.Run(strings.Join(tt.args, " "), func(t *testing.T) { - path, ok := splitPath(tt.args) + path, ok := SplitSimplePath(tt.args) require.False(t, ok, path) }) } diff --git a/pkg/reducer/utils.go b/pkg/reducer/utils.go index b73de15..46edf8a 100644 --- a/pkg/reducer/utils.go +++ b/pkg/reducer/utils.go @@ -9,7 +9,7 @@ import ( . "github.com/antonmedv/fx/pkg/theme" ) -func echo(object interface{}, theme Theme) { +func Echo(object interface{}, theme Theme) { if s, ok := object.(string); ok { fmt.Println(s) } else {