-
-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
190 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package reducer | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func js(args []string) string { | ||
rs := "\n" | ||
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( | ||
` | ||
x = ( | ||
%v | ||
)(x) | ||
`, code) | ||
|
||
case strings.HasPrefix(a, ".["): | ||
rs += fmt.Sprintf( | ||
` | ||
x = function () | ||
{ return this%v } | ||
.call(x) | ||
`, a[1:]) | ||
|
||
case strings.HasPrefix(a, "."): | ||
rs += fmt.Sprintf( | ||
` | ||
x = function () | ||
{ return this%v } | ||
.call(x) | ||
`, a) | ||
|
||
default: | ||
rs += fmt.Sprintf( | ||
` | ||
let f = function () | ||
{ return %v } | ||
.call(x) | ||
x = typeof f === 'function' ? f(x) : f | ||
`, a) | ||
} | ||
// Generate a beautiful error message. | ||
rs += " } catch (e) {\n" | ||
pre, post, pointer := trace(args, i) | ||
rs += fmt.Sprintf( | ||
" throw `\\n ${%q} ${%q} ${%q}\\n %v\\n\\n${e.stack || e}`\n", | ||
pre, a, post, pointer, | ||
) | ||
rs += " }\n" | ||
} | ||
|
||
fn := `function reduce(input) { | ||
let x = JSON.parse(input) | ||
// Reducers %v | ||
if (typeof x === 'undefined') { | ||
return 'null' | ||
} else { | ||
return JSON.stringify(x) | ||
} | ||
} | ||
` | ||
return fmt.Sprintf(fn, rs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package reducer | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
. "github.com/antonmedv/fx/pkg/json" | ||
. "github.com/antonmedv/fx/pkg/theme" | ||
) | ||
|
||
func echo(object interface{}, theme Theme) { | ||
if s, ok := object.(string); ok { | ||
fmt.Println(s) | ||
} else { | ||
fmt.Println(PrettyPrint(object, 1, theme)) | ||
} | ||
} | ||
|
||
func trace(args []string, i int) (pre, post, pointer string) { | ||
pre = strings.Join(args[:i], " ") | ||
if len(pre) > 20 { | ||
pre = "..." + pre[len(pre)-20:] | ||
} | ||
post = strings.Join(args[i+1:], " ") | ||
if len(post) > 20 { | ||
post = post[:20] + "..." | ||
} | ||
pointer = fmt.Sprintf( | ||
"%v %v %v", | ||
strings.Repeat(" ", len(pre)), | ||
strings.Repeat("^", len(args[i])), | ||
strings.Repeat(" ", len(post)), | ||
) | ||
return | ||
} | ||
|
||
var flatMapRegex = regexp.MustCompile("^(\\.\\w*)+\\[]") | ||
|
||
func fold(s []string) string { | ||
if len(s) == 1 { | ||
return "x => x" + s[0] | ||
} | ||
obj := s[0] | ||
if obj == "." { | ||
obj = "x" | ||
} else { | ||
obj = "x" + obj | ||
} | ||
return fmt.Sprintf("x => Object.values(%v).flatMap(%v)", obj, fold(s[1:])) | ||
} |