From 3fdc0937ebfee96dd06607674986dacae5e1ad70 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 21 Jan 2020 17:37:04 +0100 Subject: [PATCH] internal/jsre: fix pretty-printing of floats --- internal/jsre/pretty.go | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/internal/jsre/pretty.go b/internal/jsre/pretty.go index 57bfe03bc1..4171e00906 100644 --- a/internal/jsre/pretty.go +++ b/internal/jsre/pretty.go @@ -84,30 +84,23 @@ func (ctx ppctx) indent(level int) string { } func (ctx ppctx) printValue(v goja.Value, level int, inArray bool) { + if goja.IsNull(v) || goja.IsUndefined(v) { + fmt.Fprint(ctx.w, SpecialColor(v.String())) + return + } + kind := v.ExportType().Kind() switch { - case goja.IsNull(v): - fmt.Fprint(ctx.w, SpecialColor("null")) - case goja.IsUndefined(v): - fmt.Fprint(ctx.w, SpecialColor("undefined")) - case goja.IsNaN(v): - fmt.Fprint(ctx.w, NumberColor("NaN")) + case kind == reflect.Bool: + fmt.Fprint(ctx.w, SpecialColor("%t", v.ToBoolean())) + case kind == reflect.String: + fmt.Fprint(ctx.w, StringColor("%q", v.String())) + case kind >= reflect.Int && kind <= reflect.Complex128: + fmt.Fprint(ctx.w, NumberColor("%s", v.String())) default: - switch v.ExportType().Kind() { - case reflect.String: - s := v.ToString().String() - fmt.Fprint(ctx.w, StringColor("%q", s)) - case reflect.Bool: - b := v.ToBoolean() - fmt.Fprint(ctx.w, SpecialColor("%t", b)) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - s := v.ToString().String() - fmt.Fprint(ctx.w, NumberColor("%s", s)) - default: - if obj, ok := v.(*goja.Object); ok { - ctx.printObject(obj, level, inArray) - } else { - fmt.Fprint(ctx.w, "") - } + if obj, ok := v.(*goja.Object); ok { + ctx.printObject(obj, level, inArray) + } else { + fmt.Fprintf(ctx.w, "", v) } } }