@@ -43,6 +43,14 @@ import Data.Newtype (class Newtype)
43
43
import Data.String.Unsafe as U
44
44
45
45
-- | A newtype used in cases where there is a string to be matched.
46
+ -- |
47
+ -- | ```purescript
48
+ -- | pursPattern = Pattern ".purs"
49
+ -- | --can be used like this:
50
+ -- | contains pursPattern "Test.purs"
51
+ -- | == true
52
+ -- | ```
53
+ -- |
46
54
newtype Pattern = Pattern String
47
55
48
56
derive instance eqPattern :: Eq Pattern
@@ -63,6 +71,12 @@ instance showReplacement :: Show Replacement where
63
71
show (Replacement s) = " (Replacement " <> show s <> " )"
64
72
65
73
-- | Returns the character at the given index, if the index is within bounds.
74
+ -- |
75
+ -- | ```purescript
76
+ -- | charAt 2 "Hello" == Just 'l'
77
+ -- | charAt 10 "Hello" == Nothing
78
+ -- | ```
79
+ -- |
66
80
charAt :: Int -> String -> Maybe Char
67
81
charAt = _charAt Just Nothing
68
82
@@ -74,11 +88,20 @@ foreign import _charAt
74
88
-> Maybe Char
75
89
76
90
-- | Returns a string of length `1` containing the given character.
91
+ -- |
92
+ -- | ```purescript
93
+ -- | singleton 'l' == "l"
94
+ -- | ```
95
+ -- |
77
96
foreign import singleton :: Char -> String
78
97
79
98
-- | Returns the numeric Unicode value of the character at the given index,
80
99
-- | if the index is within bounds.
81
- -- | - `charCodeAt 2 "5 €" == Just 0x20AC`
100
+ -- | ```purescript
101
+ -- | charCodeAt 2 "5 €" == Just 0x20AC
102
+ -- | charCodeAt 10 "5 €" == Nothing
103
+ -- | ```
104
+ -- |
82
105
charCodeAt :: Int -> String -> Maybe Int
83
106
charCodeAt = _charCodeAt Just Nothing
84
107
@@ -91,6 +114,12 @@ foreign import _charCodeAt
91
114
92
115
-- | Converts the string to a character, if the length of the string is
93
116
-- | exactly `1`.
117
+ -- |
118
+ -- | ```purescript
119
+ -- | toChar "l" == Just 'l'
120
+ -- | toChar "Hi" == Nothing -- since length is not 1
121
+ -- | ```
122
+ -- |
94
123
toChar :: String -> Maybe Char
95
124
toChar = _toChar Just Nothing
96
125
@@ -101,29 +130,54 @@ foreign import _toChar
101
130
-> Maybe Char
102
131
103
132
-- | Returns `true` if the given string is empty.
133
+ -- |
134
+ -- | ```purescript
135
+ -- | null "" == true
136
+ -- | null "Hi" == false
137
+ -- | ```
138
+ -- |
104
139
null :: String -> Boolean
105
140
null s = s == " "
106
141
107
142
-- | Returns the first character and the rest of the string,
108
143
-- | if the string is not empty.
144
+ -- |
145
+ -- | ```purescript
146
+ -- | uncons "" == Nothing
147
+ -- | uncons "Hello World" == Just { head: 'H', tail: "ello World" }
148
+ -- | ```
149
+ -- |
109
150
uncons :: String -> Maybe { head :: Char , tail :: String }
110
151
uncons " " = Nothing
111
152
uncons s = Just { head: U .charAt zero s, tail: drop one s }
112
153
113
154
-- | Returns the longest prefix (possibly empty) of characters that satisfy
114
155
-- | the predicate.
115
- -- | * `takeWhile (_ /= ':') "http://purescript.org" == "http"`
156
+ -- |
157
+ -- | ```purescript
158
+ -- | takeWhile (_ /= ':') "http://purescript.org" == "http"
159
+ -- | ```
160
+ -- |
116
161
takeWhile :: (Char -> Boolean ) -> String -> String
117
162
takeWhile p s = take (count p s) s
118
163
119
164
-- | Returns the suffix remaining after `takeWhile`.
165
+ -- |
166
+ -- | ```purescript
167
+ -- | dropWhile (_ /= '.') "Test.purs" == ".purs"
168
+ -- | ```
169
+ -- |
120
170
dropWhile :: (Char -> Boolean ) -> String -> String
121
171
dropWhile p s = drop (count p s) s
122
172
123
173
-- | If the string starts with the given prefix, return the portion of the
124
174
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
125
- -- | * `stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"`
126
- -- | * `stripPrefix (Pattern "http:") "https://purescript.org" == Nothing`
175
+ -- |
176
+ -- | ```purescript
177
+ -- | stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"
178
+ -- | stripPrefix (Pattern "http:") "https://purescript.org" == Nothing
179
+ -- | ```
180
+ -- |
127
181
stripPrefix :: Pattern -> String -> Maybe String
128
182
stripPrefix prefix@(Pattern prefixS) str =
129
183
case indexOf prefix str of
@@ -133,26 +187,44 @@ stripPrefix prefix@(Pattern prefixS) str =
133
187
-- | If the string ends with the given suffix, return the portion of the
134
188
-- | string left after removing it, as a `Just` value. Otherwise, return
135
189
-- | `Nothing`.
136
- -- | * `stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"`
137
- -- | * `stripSuffix (Pattern ".exe") "psc" == Nothing`
190
+ -- |
191
+ -- | ```purescript
192
+ -- | stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"
193
+ -- | stripSuffix (Pattern ".exe") "psc" == Nothing
194
+ -- | ```
195
+ -- |
138
196
stripSuffix :: Pattern -> String -> Maybe String
139
197
stripSuffix suffix@(Pattern suffixS) str =
140
198
case lastIndexOf suffix str of
141
199
Just x | x == length str - length suffixS -> Just $ take x str
142
200
_ -> Nothing
143
201
144
202
-- | Converts an array of characters into a string.
203
+ -- |
204
+ -- | ```purescript
205
+ -- | fromCharArray ['H', 'e', 'l', 'l', 'o'] == "Hello"
206
+ -- | ```
207
+ -- |
145
208
foreign import fromCharArray :: Array Char -> String
146
209
147
210
-- | Checks whether the pattern appears in the given string.
148
- -- | * `contains (Pattern "needle") "haystack with needle" == true`
149
- -- | * `contains (Pattern "needle") "haystack" == false`
211
+ -- |
212
+ -- | ```purescript
213
+ -- | contains (Pattern "needle") "haystack with needle" == true
214
+ -- | contains (Pattern "needle") "haystack" == false
215
+ -- | ```
216
+ -- |
150
217
contains :: Pattern -> String -> Boolean
151
218
contains pat = isJust <<< indexOf pat
152
219
153
220
-- | Returns the index of the first occurrence of the pattern in the
154
221
-- | given string. Returns `Nothing` if there is no match.
155
- -- | * `indexOf (Pattern "c") "abcde" == Just 2`
222
+ -- |
223
+ -- | ```purescript
224
+ -- | indexOf (Pattern "c") "abcdc" == Just 2
225
+ -- | indexOf (Pattern "c") "aaa" == Nothing
226
+ -- | ```
227
+ -- |
156
228
indexOf :: Pattern -> String -> Maybe Int
157
229
indexOf = _indexOf Just Nothing
158
230
@@ -166,6 +238,12 @@ foreign import _indexOf
166
238
-- | Returns the index of the first occurrence of the pattern in the
167
239
-- | given string, starting at the specified index. Returns `Nothing` if there is
168
240
-- | no match.
241
+ -- |
242
+ -- | ```purescript
243
+ -- | indexOf' (Pattern "a") 2 "ababa" == Just 2
244
+ -- | indexOf' (Pattern "a") 3 "ababa" == Just 4
245
+ -- | ```
246
+ -- |
169
247
indexOf' :: Pattern -> Int -> String -> Maybe Int
170
248
indexOf' = _indexOf' Just Nothing
171
249
@@ -179,6 +257,12 @@ foreign import _indexOf'
179
257
180
258
-- | Returns the index of the last occurrence of the pattern in the
181
259
-- | given string. Returns `Nothing` if there is no match.
260
+ -- |
261
+ -- | ```purescript
262
+ -- | lastIndexOf (Pattern "c") "abcdc" == Just 4
263
+ -- | lastIndexOf (Pattern "c") "aaa" == Nothing
264
+ -- | ```
265
+ -- |
182
266
lastIndexOf :: Pattern -> String -> Maybe Int
183
267
lastIndexOf = _lastIndexOf Just Nothing
184
268
@@ -190,8 +274,16 @@ foreign import _lastIndexOf
190
274
-> Maybe Int
191
275
192
276
-- | Returns the index of the last occurrence of the pattern in the
193
- -- | given string, starting at the specified index. Returns `Nothing`
194
- -- | if there is no match.
277
+ -- | given string, starting at the specified index
278
+ -- | and searching backwards towards the beginning of the string.
279
+ -- | Returns `Nothing` if there is no match.
280
+ -- |
281
+ -- | ```purescript
282
+ -- | lastIndexOf' (Pattern "a") 1 "ababa" == Just 0
283
+ -- | lastIndexOf' (Pattern "a") 3 "ababa" == Just 2
284
+ -- | lastIndexOf' (Pattern "a") 4 "ababa" == Just 4
285
+ -- | ```
286
+ -- |
195
287
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
196
288
lastIndexOf' = _lastIndexOf' Just Nothing
197
289
@@ -204,13 +296,22 @@ foreign import _lastIndexOf'
204
296
-> Maybe Int
205
297
206
298
-- | Returns the number of characters the string is composed of.
299
+ -- |
300
+ -- | ```purescript
301
+ -- | length "Hello World" == 11
302
+ -- | ```
303
+ -- |
207
304
foreign import length :: String -> Int
208
305
209
306
-- | Compare two strings in a locale-aware fashion. This is in contrast to
210
307
-- | the `Ord` instance on `String` which treats strings as arrays of code
211
308
-- | units:
212
- -- | - `"ä" `localeCompare` "b" == LT`
213
- -- | - `"ä" `compare` "b" == GT`
309
+ -- |
310
+ -- | ```purescript
311
+ -- | "ä" `localeCompare` "b" == LT
312
+ -- | "ä" `compare` "b" == GT
313
+ -- | ```
314
+ -- |
214
315
localeCompare :: String -> String -> Ordering
215
316
localeCompare = _localeCompare LT EQ GT
216
317
@@ -223,28 +324,62 @@ foreign import _localeCompare
223
324
-> Ordering
224
325
225
326
-- | Replaces the first occurence of the pattern with the replacement string.
226
- -- | * `replace (Pattern "http") (Replacement "https") "http://purescript.org" == "https://purescript.org"`
327
+ -- |
328
+ -- | ```purescript
329
+ -- | replace (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b <= c"
330
+ -- | ```
331
+ -- |
227
332
foreign import replace :: Pattern -> Replacement -> String -> String
228
333
229
334
-- | Replaces all occurences of the pattern with the replacement string.
335
+ -- |
336
+ -- | ```purescript
337
+ -- | replaceAll (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b ≤ c"
338
+ -- | ```
339
+ -- |
230
340
foreign import replaceAll :: Pattern -> Replacement -> String -> String
231
341
232
342
-- | Returns the first `n` characters of the string.
343
+ -- |
344
+ -- | ```purescript
345
+ -- | take 5 "Hello World" == "Hello"
346
+ -- | ```
347
+ -- |
233
348
foreign import take :: Int -> String -> String
234
349
235
350
-- | Returns the string without the first `n` characters.
351
+ -- |
352
+ -- | ```purescript
353
+ -- | drop 6 "Hello World" == "World"
354
+ -- | ```
355
+ -- |
236
356
foreign import drop :: Int -> String -> String
237
357
238
358
-- | Returns the number of contiguous characters at the beginning
239
359
-- | of the string for which the predicate holds.
360
+ -- |
361
+ -- | ```purescript
362
+ -- | count (_ /= ' ') "Hello World" == 5 -- since length "Hello" == 5
363
+ -- | ```
364
+ -- |
240
365
foreign import count :: (Char -> Boolean ) -> String -> Int
241
366
242
367
-- | Returns the substrings of the second string separated along occurences
243
368
-- | of the first string.
244
- -- | * `split (Pattern " ") "hello world" == ["hello", "world"]`
369
+ -- |
370
+ -- | ```purescript
371
+ -- | split (Pattern " ") "hello world" == ["hello", "world"]
372
+ -- | ```
373
+ -- |
245
374
foreign import split :: Pattern -> String -> Array String
246
375
247
376
-- | Returns the substrings of a split at the given index, if the index is within bounds.
377
+ -- |
378
+ -- | ```purescript
379
+ -- | splitAt 2 "Hello World" == Just { before: "He", after: "llo World"}
380
+ -- | splitAt 10 "Hi" == Nothing
381
+ -- | ```
382
+ -- |
248
383
splitAt :: Int -> String -> Maybe { before :: String , after :: String }
249
384
splitAt = _splitAt Just Nothing
250
385
@@ -255,20 +390,44 @@ foreign import _splitAt :: (forall a. a -> Maybe a)
255
390
-> Maybe { before :: String , after :: String }
256
391
257
392
-- | Converts the string into an array of characters.
393
+ -- |
394
+ -- | ```purescript
395
+ -- | toCharArray "Hello☺\n" == ['H','e','l','l','o','☺','\n']
396
+ -- | ```
397
+ -- |
258
398
foreign import toCharArray :: String -> Array Char
259
399
260
400
-- | Returns the argument converted to lowercase.
401
+ -- |
402
+ -- | ```purescript
403
+ -- | toLower "hElLo" == "hello"
404
+ -- | ```
405
+ -- |
261
406
foreign import toLower :: String -> String
262
407
263
408
-- | Returns the argument converted to uppercase.
409
+ -- |
410
+ -- | ```purescript
411
+ -- | toUpper "Hello" == "HELLO"
412
+ -- | ```
413
+ -- |
264
414
foreign import toUpper :: String -> String
265
415
266
416
-- | Removes whitespace from the beginning and end of a string, including
267
417
-- | [whitespace characters](http://www.ecma-international.org/ecma-262/5.1/#sec-7.2)
268
418
-- | and [line terminators](http://www.ecma-international.org/ecma-262/5.1/#sec-7.3).
419
+ -- |
420
+ -- | ```purescript
421
+ -- | trim " Hello \n World\n\t " == "Hello \n World"
422
+ -- | ```
423
+ -- |
269
424
foreign import trim :: String -> String
270
425
271
426
-- | Joins the strings in the array together, inserting the first argument
272
427
-- | as separator between them.
273
- -- | * `joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange"`
428
+ -- |
429
+ -- | ```purescript
430
+ -- | joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange"
431
+ -- | ```
432
+ -- |
274
433
foreign import joinWith :: String -> Array String -> String
0 commit comments