Skip to content

Commit a76cd86

Browse files
committed
New: Format.SimplifyJson
1 parent bb4bc1a commit a76cd86

2 files changed

Lines changed: 80 additions & 10 deletions

File tree

source/Errors/Err.ExpandErrorRecord.pq

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,39 @@ let
66
Err.ExpandErrorRecord.pq - start here, this is a lot simpler to use
77
Err.ExpandErrorRecord.Verbose.pq - when you want an excessive amount of details
88
*/
9-
// when you want extra details on an exception, mainly for interactive use.
9+
1010
Err.ExpandErrorRecord = (
1111
exception as any,
1212
optional options as nullable record
1313
) => [
1414
Json = (source as any) as text => Text.FromBinary( Json.FromValue( source ) ),
15+
// future: auto coerce exceptions and error records, so that either can be passed
16+
// isUncaught = (try exception)[HasError],
17+
// exception =
18+
// if not isUncaught
19+
// then exception
20+
// else try exception,
21+
22+
// exception = try exception catch (e) => e,
1523
return = [
1624
// Er_Type = Value.Type( exception ), // is alway a record?
17-
Err = exception,
25+
Err = exception,
1826
Err.Meta = Value.Metadata( Err ),
1927
ErTypeMeta = Value.Metadata( Value.Type( Err ) ),
2028
ErMeta_KeyNames = Text.Combine( Record.FieldNames( Err.Meta ), ", " ),
2129

22-
Reason = Err[Reason]? ?? null,
23-
Message = Err[Message]? ?? null,
24-
Detail = Err[Detail]? ?? null,
25-
Message.Format = Err[Message.Format]? ?? null,
26-
Message.Parameters = Err[Message.Parameters]? ?? null,
30+
Reason = Err[Reason]? ?? null,
31+
Message = Err[Message]? ?? null,
32+
Detail = Err[Detail]? ?? null,
33+
Message.Format = Err[Message.Format]? ?? null,
34+
Message.Parameters = Err[Message.Parameters]? ?? null,
35+
Message.Parameters_Json =
36+
Json( Message.Parameters ),
37+
2738

28-
Expression.Stack = Err.Meta[Expression.Stack]? ?? null,
29-
Expression.Stack.Json = Json( Expression.Stack ),
30-
Expression.Stack.JsonLines =
39+
Expression.Stack = Err.Meta[Expression.Stack]? ?? null,
40+
Expression.Stack.Json = Json( Expression.Stack ),
41+
Expression.Stack.JsonLines =
3142
Text.Combine(
3243
List.Transform(
3344
Expression.Stack,

source/Text/Format.SimplifyJson.pq

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
let
2+
/*
3+
About:
4+
I wanted a quick way to visualize JSON
5+
Expanding JSON with newlines still has a lot of noice
6+
I am experimenting with something shorter like yaml, but not using yaml constraints
7+
this is overkill.
8+
*/
9+
Format.SimplfyJson = (
10+
source as text,
11+
optional options as nullable record
12+
13+
) as text => [
14+
15+
// make defaults param
16+
options = options ?? [
17+
PadKeyValue = true,
18+
KeyValueDelim = " ▸ ",
19+
PadBraceEnding = true,
20+
TrimDoubleQuotes = true,
21+
ReplaceDoubleQuoteString = " ",
22+
TrimPrefixSuffixBrace = false
23+
],
24+
// Str = [ DoubleQuote = "#(0022)" ],
25+
keyValueDelim_1 = " ▸ ",
26+
keyValueDelim = ": ",
27+
acc0 = source,
28+
// acc1 = Text.Replace( acc0, "#(0022):{#(0022)", " => " ),
29+
acc1 =
30+
if not options[PadKeyValue]
31+
then acc0
32+
else
33+
Text.Replace( acc0, "#(0022):", options[KeyValueDelim] ),
34+
acc2 =
35+
if not options[TrimDoubleQuotes]
36+
then acc1
37+
else
38+
Text.Replace( acc1, "#(0022)", options[ReplaceDoubleQuoteString] ),
39+
acc3 =
40+
if not options[PadBraceEnding] then acc2
41+
else
42+
Text.Replace( acc2, "}", " }" ),
43+
acc4 = // this only strips the final instance because of acc3 running first
44+
// should instead be a replace first and last occurence instead
45+
if not options[TrimPrefixSuffixBrace] then acc3
46+
else
47+
[
48+
trimSpace = Text.Trim( acc3 ),
49+
trimStart = Text.TrimStart( trimSpace, "{" ),
50+
trimEnd = Text.TrimEnd( trimStart, "}" ),
51+
return = trimEnd
52+
53+
][return],
54+
55+
return = acc3
56+
57+
][return]
58+
in
59+
Format.SimplfyJson

0 commit comments

Comments
 (0)