Skip to content

Commit 1594744

Browse files
committed
Module: Debug utils: added SimplifyJson
1 parent a76cd86 commit 1594744

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

source-modules/DebugUtils.module.pq

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@
4444
// stop folding without forcing buffer or index column enumerations
4545
StopFold = (source as table) as table => Table.StopFolding(source),
4646

47+
// example output: [ Core = "2.129.255.0" ]
48+
Versions.Module = Module.Versions(),
49+
4750
// see: https://blog.crossjoin.co.uk/2023/12/10/getting-different-versions-of-data-with-value-versions-in-power-query/
4851
Versions = (value as any) as table => Value.Versions( value ),
52+
4953
// see: https://blog.crossjoin.co.uk/2023/12/10/getting-different-versions-of-data-with-value-versions-in-power-query/
5054
Versions.Which = (value as any) as any => Value.VersionIdentity( value ),
5155

@@ -77,5 +81,57 @@
7781
json = Text.FromBinary( Json.FromValue( source, config[Encoding] ) , config[Encoding] ),
7882
json_expanded = Text.Replace( json, "},{", "},#(cr,lf){"),
7983
return = if config[Expand] then json_expanded else json
84+
][return],
85+
86+
// takes a string of json, replaces values to simplify it
87+
SimplifyJson = (
88+
source as text,
89+
optional options as nullable record
90+
91+
) as text => [
92+
93+
// make defaults param
94+
options = options ?? [
95+
PadKeyValue = true,
96+
KeyValueDelim = " ▸ ",
97+
PadBraceEnding = true,
98+
TrimDoubleQuotes = true,
99+
ReplaceDoubleQuoteString = " ",
100+
TrimPrefixSuffixBrace = false
101+
],
102+
// Str = [ DoubleQuote = "#(0022)" ],
103+
keyValueDelim_1 = " ▸ ",
104+
keyValueDelim = ": ",
105+
acc0 = source,
106+
// acc1 = Text.Replace( acc0, "#(0022):{#(0022)", " => " ),
107+
acc1 =
108+
if not options[PadKeyValue]
109+
then acc0
110+
else
111+
Text.Replace( acc0, "#(0022):", options[KeyValueDelim] ),
112+
113+
acc2 =
114+
if not options[TrimDoubleQuotes]
115+
then acc1
116+
else
117+
Text.Replace( acc1, "#(0022)", options[ReplaceDoubleQuoteString] ),
118+
acc3 =
119+
if not options[PadBraceEnding] then acc2
120+
else
121+
Text.Replace( acc2, "}", " }" ),
122+
acc4 = // this only strips the final instance because of acc3 running first
123+
// should instead be a replace first and last occurence instead
124+
if not options[TrimPrefixSuffixBrace] then acc3
125+
else
126+
[
127+
trimSpace = Text.Trim( acc3 ),
128+
trimStart = Text.TrimStart( trimSpace, "{" ),
129+
trimEnd = Text.TrimEnd( trimStart, "}" ),
130+
return = trimEnd
131+
132+
][return],
133+
134+
return = acc3
135+
80136
][return]
81137
]

0 commit comments

Comments
 (0)