-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathText.WordWrap.pq
More file actions
39 lines (34 loc) · 1.47 KB
/
Text.WordWrap.pq
File metadata and controls
39 lines (34 loc) · 1.47 KB
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
let
/*
Example:
wrapped = Text.WordWrap(LoremIpsum, 80)
LoremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu laoreet turpis. Curabitur lacinia, risus ut rhoncus mattis, turpis lorem iaculis justo, nec ultrices arcu erat vitae felis. Pellentesque vulputate efficitur scelerisque. Etiam bibendum dignissim mauris",
*/
// calculate length of string *after* the rightmost newline
strNewline = "#(lf)",
Text_LengthAfterNewline = (string as text) as number =>
let
posLastNewline = Text.PositionOf(string, strNewline, Occurrence.Last),
posOffset = if posLastNewline <> -1 then posLastNewline else 0,
deltaLen = Text.Length(string) - posOffset
in
deltaLen,
// word wraps text
Text.WordWrap = (string as text, max_width as number) as text =>
let
words = Text.Split(string, " "),
accum_result = List.Accumulate(
words, "",
(state as text, current as text) as text =>
let
len = Text_LengthAfterNewline(state) + Text.Length(current) + 1,
maybeNewline =
if len > max_width then strNewline else "",
accum_string = Text.Combine({state & maybeNewline, current}, " ")
in
accum_string
)
in
accum_result
in
Text.WordWrap