-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSplitter.pq
More file actions
51 lines (49 loc) · 2.2 KB
/
Splitter.pq
File metadata and controls
51 lines (49 loc) · 2.2 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
40
41
42
43
44
45
46
47
48
49
50
51
section Splitter;
/////////////////////////
// Splitters //
/////////////////////////
shared Splitter.SplitTextByNonAlpha = Document(
"Splitter.SplitTextByNonAlpha",
"Splits text by characters that aren't [A-Za-z]",
{[ Description = "Split text", Code="PBI[Splitter.SplitTextByNonAlpha](""A1B,C"")", Result = "{ ""A"", ""B"", ""C"" }"]},
(line as text) => Splitter.SplitTextByNotIn(Text.Alphabet)
);
shared Splitter.SplitTextByNotIn = Document(
"Splitter.SplitTextByNotIn",
"Splits text on any characters that aren't the provided 'safe' characters",
{[ Description = "Split on non-alphanumeric", Code = "PBI[Splitter.SplitTextByNotIn](PBI[Text.Alphanumeric])(""Power BI is #1"")", Result = "{""Power BI is "", ""1""}"]},
(safeCharacters as text) => (line as nullable text) =>
if line is null then
{}
else
List.Accumulate(Text.ToList(line), {null} , (state, current) =>
let
doSkip = not Text.Contains(safeCharacters, current),
lastItem = List.Last(state),
appendLast = lastItem<>null
in
if doSkip then
if lastItem is null then
state
else
List.Combine({state, {null}})
else if appendLast then
List.Combine({List.RemoveLastN(state, 1), {lastItem & current}})
else
List.Combine({List.RemoveLastN(state, 1), {current}}))
);
/////////////////////////
// Dependencies //
/////////////////////////
Document = (name as text, description as text, valueOrExample as any, optional valueIfExample as any) =>
let
value = if valueIfExample is null then valueOrExample else valueIfExample,
examples = if valueIfExample is null then {} else valueOrExample
in
Value.ReplaceType(value, Value.Type(value) meta [
Documentation.Name = name,
Documentation.Description = description,
// [Description = "", Code="", Result =""]
Documentation.Examples = examples
]);
Text.Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";