-
Notifications
You must be signed in to change notification settings - Fork 4
/
SummarizeRec.pq
88 lines (68 loc) · 2.36 KB
/
SummarizeRec.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let
/*
converts records into a string
let
source =
in
Summarize_Record(source)
Example: Basic
input:
[ Name = "Dog", Age = 3, DogYears = 3*3 ],
output: as text:
'[ Age = 3, DogYears = 9, Name = "Dog" ]'
Example: Values are converted using 'culture':
input:
[ now = DateTime.LocalNow(), utc = DateTimeZone.UtcNow(), culture = Culture.Current ]
output as text:
[ now = 1/7/2021 10:52:12 AM, utc = 1/7/2021 4:52:12 PM +00:00, culture = en-US ]
Example3:
options = [ separator = "#(lf,tab)", prefix = "[#(lf,tab)", suffix = "#(lf)]" ],
output:
[
culture = en-US
now = 1/7/2021 11:14:59 AM
utc = 1/7/2021 5:14:59 PM +00:00
]
when options[sort] = true
in:
[ Name = cat, Lives = 9 ]
out:
[ Lives = 9, Name = cat ]
Example 4:
in:
[ Num = 123.45 ]
with default
[ Num = 123.45 ]
with [ culture = "de-DE" ]
[ Num = 123,45 ]
check if identical
*/
Summarize.Rec = (source as record, optional options as nullable record) as text =>
let
// next: simplify parameter handling w/ Validate_OptionalArgRecord
outputPrefix = Text.From(options[prefix]? ?? "[ "),
outputSuffix = Text.From(options[suffix]? ?? " ]"),
outputSeparator = Text.From(options[separator]? ?? ", "),
culture = options[culture]? ?? null,
// outputSuffix = options[suffix]? ?? " ]",
optionSort = options[sort]? ?? true,
_n = Record.FieldNames( source ),
names = if optionSort then List.Sort(_n) else _n,
pairs = List.Transform(
names,
(key as text) =>
[
Key = key,
Value = Record.Field( source, key )
]
),
pairsAsText = List.Transform(
pairs,
(field as record) =>
field[Key] & " = " & Text.From( field[Value], culture )
),
x3 = outputPrefix & Text.Combine( pairsAsText, outputSeparator ) & outputSuffix
in
x3
in
Summarize.Rec