forked from purescript/purescript-strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.purs
More file actions
266 lines (238 loc) Β· 7.2 KB
/
String.purs
File metadata and controls
266 lines (238 loc) Β· 7.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
-- | Wraps the functions of Javascript's `String` object.
-- | A String represents a sequence of characters.
-- | For details of the underlying implementation, see [String Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
module Data.String
(
charAt,
charCodeAt,
fromCharArray,
fromChar,
indexOf,
indexOf',
lastIndexOf,
lastIndexOf',
null,
uncons,
length,
singleton,
localeCompare,
replace,
count,
take,
takeWhile,
drop,
dropWhile,
split,
toCharArray,
toLower,
toUpper,
trim,
joinWith
) where
import Data.Maybe
import Data.Char
import Data.Function
import qualified Data.String.Unsafe as U
foreign import _charAt
"""
function _charAt(i, s, Just, Nothing) {
return i >= 0 && i < s.length ? Just(s.charAt(i)) : Nothing;
}
""" :: forall a. Fn4 Number String (a -> Maybe a) (Maybe a) (Maybe Char)
-- | Returns the character at the given index, if the index is within bounds.
charAt :: Number -> String -> Maybe Char
charAt n s = runFn4 _charAt n s Just Nothing
-- | Returns a string of length `1` containing the given character.
fromChar :: Char -> String
fromChar = charString
-- | Returns a string of length `1` containing the given character.
-- | Same as `fromChar`.
singleton :: Char -> String
singleton = fromChar
foreign import _charCodeAt
"""
function _charCodeAt(i, s, Just, Nothing) {
return i >= 0 && i < s.length ? Just(s.charCodeAt(i)) : Nothing;
}
""" :: forall a. Fn4 Number String (a -> Maybe a) (Maybe a) (Maybe Number)
-- | Returns the numeric Unicode value of the character at the given index,
-- | if the index is within bounds.
charCodeAt :: Number -> String -> Maybe Number
charCodeAt n s = runFn4 _charCodeAt n s Just Nothing
-- | Returns `true` if the given string is empty.
null :: String -> Boolean
null s = length s == 0
-- | Returns the first character and the rest of the string,
-- | if the string is not empty.
uncons :: String -> Maybe {head :: Char, tail :: String}
uncons s | null s = Nothing
uncons s = Just {head : U.charAt 0 s, tail : drop 1 s}
-- | Returns the longest prefix (possibly empty) of characters that satisfy
-- | the predicate:
takeWhile :: (Char -> Boolean) -> String -> String
takeWhile p s = take (count p s) s
-- | Returns the suffix remaining after `takeWhile`.
dropWhile :: (Char -> Boolean) -> String -> String
dropWhile p s = drop (count p s) s
-- | Converts an array of characters into a string.
foreign import fromCharArray
"""
function fromCharArray(a) {
return a.join('');
}
""" :: [Char] -> String
-- | Returns the index of the first occurrence of the first string in the
-- | second string. Returns `-1` if there is no match.
foreign import indexOf
"""
function indexOf(x) {
return function(s) {
return s.indexOf(x);
};
}
""" :: String -> String -> Number
-- | Returns the index of the first occurrence of the first string in the
-- | second string, starting at the given index. Returns `-1` if there is
-- | no match.
foreign import indexOf'
"""
function indexOf$prime(x) {
return function(startAt) {
return function(s) {
return s.indexOf(x, startAt);
};
};
}
""" :: String -> Number -> String -> Number
-- | Returns the index of the last occurrence of the first string in the
-- | second string. Returns `-1` if there is no match.
foreign import lastIndexOf
"""
function lastIndexOf(x) {
return function(s) {
return s.lastIndexOf(x);
};
}
""" :: String -> String -> Number
-- | Returns the index of the first occurrence of the last string in the
-- | second string, starting at the given index. Returns `-1` if there is
-- | no match.
foreign import lastIndexOf'
"""
function lastIndexOf$prime(x) {
return function(startAt) {
return function(s) {
return s.lastIndexOf(x, startAt);
};
};
}
""" :: String -> Number -> String -> Number
-- | Returns the number of characters the string is composed of.
foreign import length
"""
function length(s) {
return s.length;
}
""" :: String -> Number
-- | Locale-aware sort order comparison. Returns a negative number if the
-- | first string occurs before the second in a sort, a positive number
-- | if the first string occurs after the second, and `0` if their sort order
-- | is equal.
foreign import localeCompare
"""
function localeCompare(s1) {
return function(s2) {
return s1.localeCompare(s2);
};
}
""" :: String -> String -> Number
-- | Replaces the first occurence of the first argument with the second argument.
foreign import replace
"""
function replace(s1) {
return function(s2) {
return function(s3) {
return s3.replace(s1, s2);
};
};
}
""" :: String -> String -> String -> String
-- | Returns the first `n` characters of the string.
foreign import take
"""
function take(n) {
return function(s) {
return s.substr(0, n);
};
}
""" :: Number -> String -> String
-- | Returns the string without the first `n` characters.
foreign import drop
"""
function drop(n) {
return function(s) {
return s.substr(n);
};
}
""" :: Number -> String -> String
-- | Returns the number of characters in the string for which the predicate holds.
foreign import count
"""
function count(p){
return function(s){
var i;
for(i = 0; i < s.length && p(s.charAt(i)); i++){};
return i;
};
}
""" :: (Char -> Boolean) -> String -> Number
-- | Returns the substrings of the first string separated along occurences
-- | of the second string.
foreign import split
"""
function split(sep) {
return function(s) {
return s.split(sep);
};
}
""" :: String -> String -> [String]
-- | Converts the string into an array of characters.
foreign import toCharArray
"""
function toCharArray(s) {
return s.split('');
}
""" :: String -> [Char]
-- | Returns the argument converted to lowercase.
foreign import toLower
"""
function toLower(s) {
return s.toLowerCase();
}
""" :: String -> String
-- | Returns the argument converted to uppercase.
foreign import toUpper
"""
function toUpper(s) {
return s.toUpperCase();
}
""" :: String -> String
-- | Removes whitespace from the beginning and end of a string, including
-- | [whitespace characters](http://www.ecma-international.org/ecma-262/5.1/#sec-7.2)
-- | and [line terminators](http://www.ecma-international.org/ecma-262/5.1/#sec-7.3).
foreign import trim
"""
function trim(s) {
return s.trim();
}
""" :: String -> String
-- | Joins the strings in the array together, inserting the first argument
-- | as separator between them.
foreign import joinWith
"""
function joinWith(s) {
return function(xs) {
return xs.join(s);
};
}
""" :: String -> [String] -> String