Skip to content

Commit 860f539

Browse files
committed
Create StringBuilder.module.pq
1 parent 818205c commit 860f539

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[
2+
/*
3+
About: Stand alone StringBuilder inspired functions
4+
5+
See Also:
6+
Write.Html.module.pq
7+
8+
*/
9+
// optional aggressive Aliases
10+
Sb.JoinDelim = StringBuilder.JoinDelimiter,
11+
Sb.Format = StringBuilder.Format,
12+
Sb.JoinFormat = StringBuilder.JoinFormat,
13+
Sb.JoinLineEnding = StringBuilder.JoinLineEnding,
14+
Sb.JoinNL = StringBuilder.JoinLineEnding,
15+
16+
Sb.Str = [
17+
LineEnding = "#(cr,lf)",
18+
NullStr = "#(2400)",
19+
SpaceStr = "#(2420)",
20+
Tab = "#(tab)"
21+
],
22+
23+
StringBuilder.JoinLineEnding = (
24+
items as list
25+
) as text => [
26+
return = Text.Combine( items, Sb.Str[LineEnding] )
27+
][return],
28+
29+
StringBuilder.JoinDelimiter = (
30+
items as list,
31+
separator as text
32+
) as text => [
33+
return = Text.Combine( items, separator )
34+
35+
][return],
36+
/*
37+
like StringBuilder.JoinFormat,
38+
but this accepts a single value verses a list
39+
*/
40+
StringBuilder.Format = (
41+
content as text,
42+
optional options as nullable record
43+
) as text => [
44+
return = StringBuilder.JoinFormat( {content}, options )
45+
][return],
46+
47+
/*
48+
Valid options are any combination of these:
49+
- Prefix,
50+
- Suffix
51+
- Separator
52+
*/
53+
StringBuilder.JoinFormat = (
54+
items as list,
55+
optional options as nullable record
56+
57+
) as text => [
58+
formatStr = "#[prefix]#[content]#[suffix]",
59+
60+
renderLines = List.Transform(
61+
items,
62+
(line) => Text.Format(
63+
formatStr, [
64+
prefix = options[Prefix]? ?? "",
65+
content = line,
66+
suffix = options[Suffix]? ?? ""
67+
]
68+
)
69+
),
70+
71+
return = Text.Combine(
72+
renderLines,
73+
options[Separator]? ?? Sb.Str[LineEnding]
74+
)
75+
76+
][return],
77+
78+
]

0 commit comments

Comments
 (0)