-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.ua
157 lines (120 loc) · 4.43 KB
/
lib.ua
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Uiua Essential - closest thing to a standard library
Alphabet ← +@a⇡26
Digits ← +@0⇡10
Alphanumeric ← ⊂⊂ Alphabet ⌵Alphabet Digits
Whitespace ← " \n\r\t"
CsvDelimiters ← ",;|\t"
# Make the first character uppercase.
# result ? input
Capitalize ← ⍣(⍜⊢⌵|∘)
TrimStartMask ↚ \↥¬∈Whitespace
TrimEndMask ↚ ⇌TrimStartMask⇌
TrimMask ↚ ↧⊃(TrimStartMask|TrimEndMask)
# Trim whitespace from the start of a string.
TrimStart ← ▽TrimStartMask.
# Trim whitespace from the end of a string.
TrimEnd ← ▽TrimEndMask.
# Trim whitespace from both ends of a string.
Trim ← ▽TrimMask.
# Change characters in a string to uppercase.
ToUppercase ← ⌵
# Change characters in a string to lowercase.
ToLowercase ← ¯⌵
# Check if an array starts with another array.
# bool ? needle haystack
StartsWith ← ⍣(≍⟜(↙⧻)|0)
# Check if an array ends with another array.
# bool ? needle haystack
EndsWith ← ⍣(≍⟜(↙¯⧻)|0)
# Check if an array is a sub-array of another array.
# bool ? needle haystack
Contains ← /↥[⊃(/↥⌕|≍[]|≍""|≍{})]
# Split an array by delimiter.
# boxedArray ? delimiter string
SplitBy ← ⊜□⊸(¬↧1⦷)
# Join array elements with a delimiter
# array ? delimiter array
JoinWith ← /$"___"
# Match one array with another array and replece the matches with yet another array.
# result ? searchFor replaceWith subject
Replace ← (
⊃(=0⧻⋅⋅∘|⊙⊙∘)
⨬(⍜⊜∘≡⋅∘ ⊃(⊸⦷⊙⋅∘|¤⋅∘)|⋅⋅∘)
)
# Insert an array into a position of another array.
# result ? arrayToInsert position arrayToInsertInto
Insert ← (
◡(≥0-⊃(⧻⋅⋅∘|⋅∘))
⨬(⍜⊜∘(¤⊂:⊢)⊃(≥⊃(⋅∘|⇡⧻⋅⋅∘)|⋅⋅∘|∘)
| ⊂:⊙⋅∘)
)
# Extract words from an arbitrary string.
# Mostly used to transform strings to different formats.
# boxedArray ? string
ExtractWords ← (
# Replace non-alphanumeric characters with spaces
⍜▽(⋅@\s)⊸(¬∈Alphanumeric)
# Insert a space in word boundries
⍜⊜∘≡(⊂⊂:" "°⊟)⦷[1 0]≠⌵..
# Split words by spaces
⊜(□¯⌵)≠" ".
)
# Transform a string to PascalCase.
ToPascalCase ← /$"__"⍚Capitalize ExtractWords
# Transform a string to camelCase.
ToCamelCase ← /$"__"⍜(↘1|⍚Capitalize) ExtractWords
# Transform a string to Pascal_Snake_Case.
ToPascalSnakeCase ← /$"_\__"⍚Capitalize ExtractWords
# Transform a string to camel_Snake_Case.
ToCamelSnakeCase ← /$"_\__"⍜(↘1|⍚Capitalize) ExtractWords
# Transform a string to Train-Case.
ToTrainCase ← /$"_-_"⍚Capitalize ExtractWords
# Transform a string to snake_case.
ToSnakeCase ← /$"_\__"ExtractWords
# Transform a string to kebab-case.
ToKebabCase ← /$"_-_"ExtractWords
# Transform a string to flatcase.
ToFlatCase ← /$"__"ExtractWords
# Transform a string to UPPERFLATCASE.
ToUpperFlatCase ← ⌵ToFlatCase
# Transform a string to SCREAMING_SNAKE_CASE.
ToScreamingSnakeCase ← ⌵ToSnakeCase
# Transform a string to COBOL-CASE.
ToCobolCase ← ⌵ToKebabCase
PadGeneric! ↚ ⨬(⋅∘|^0▽⊙@\s) ⊸>0 -⊃(⧻⋅∘|∘|⋅∘)
# Add spaces to the left of the string until it matches the desired length.
# result ? targetLength string
PadLeft ← PadGeneric!⊂
# Add spaces to the right of the string until it matches the desired length.
# result ? targetLength string
PadRight ← PadGeneric!(⊂:)
# Parse CSV by a custom delimiter.
# parsedCsv ? delimiter string
ParseCsvByDelimiter ← (
# Find string delimiters
◫[[2]]⊃(⊚|¤⧻)⌕"\"\"\"".:
# Mask strings by delimiters
¬/↥≡(↧⊃(≥⊙⋅∘|<+3⋅⊙∘)°⊟⊙⇡)
# Replace custom delimiter with commas
⍜⊜∘≡⋅@,↧1×⦷⊃(⋅⋅∘|⋅∘|∘|⋅∘)
# Parse CSV
°csv
)
# Parse CSV delimited by semicolons.
# parsedCsv ? string
ParseCsvBySemicolon ← ParseCsvByDelimiter ";"
# Parse CSV delimited by pipes.
# parsedCsv ? string
ParseCsvByPipe ← ParseCsvByDelimiter "|"
# Parse CSV delimited by tabs.
# parsedCsv ? string
ParseCsvByTabs ← ParseCsvByDelimiter "\t"
# Given a CSV string, find the most used character for CSV delimiter.
# delimiter ? csvString
MostCommonCsvDelimiter ← ⊡⊢⍖≡(/+∈)⊃(∘|¤⋅∘|∘) CsvDelimiters
# Parse CSV by detected delimiter.
# parsedCsv ? csvString
ParseCsv ← ParseCsvByDelimiter MostCommonCsvDelimiter.
# Filter an array given a predicate.
# filteredValues ? filterFunction arrayToFilter
Filter! ← ▽≡(^0°□).