@@ -43,6 +43,14 @@ import Data.Newtype (class Newtype)
4343import Data.String.Unsafe as U
4444
4545-- | 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+ -- |
4654newtype Pattern = Pattern String
4755
4856derive instance eqPattern :: Eq Pattern
@@ -63,6 +71,12 @@ instance showReplacement :: Show Replacement where
6371 show (Replacement s) = " (Replacement " <> show s <> " )"
6472
6573-- | 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+ -- |
6680charAt :: Int -> String -> Maybe Char
6781charAt = _charAt Just Nothing
6882
@@ -74,11 +88,20 @@ foreign import _charAt
7488 -> Maybe Char
7589
7690-- | Returns a string of length `1` containing the given character.
91+ -- |
92+ -- | ```purescript
93+ -- | singleton 'l' == "l"
94+ -- | ```
95+ -- |
7796foreign import singleton :: Char -> String
7897
7998-- | Returns the numeric Unicode value of the character at the given index,
8099-- | 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+ -- |
82105charCodeAt :: Int -> String -> Maybe Int
83106charCodeAt = _charCodeAt Just Nothing
84107
@@ -91,6 +114,12 @@ foreign import _charCodeAt
91114
92115-- | Converts the string to a character, if the length of the string is
93116-- | exactly `1`.
117+ -- |
118+ -- | ```purescript
119+ -- | toChar "l" == Just 'l'
120+ -- | toChar "Hi" == Nothing -- since length is not 1
121+ -- | ```
122+ -- |
94123toChar :: String -> Maybe Char
95124toChar = _toChar Just Nothing
96125
@@ -101,29 +130,54 @@ foreign import _toChar
101130 -> Maybe Char
102131
103132-- | Returns `true` if the given string is empty.
133+ -- |
134+ -- | ```purescript
135+ -- | null "" == true
136+ -- | null "Hi" == false
137+ -- | ```
138+ -- |
104139null :: String -> Boolean
105140null s = s == " "
106141
107142-- | Returns the first character and the rest of the string,
108143-- | if the string is not empty.
144+ -- |
145+ -- | ```purescript
146+ -- | uncons "" == Nothing
147+ -- | uncons "Hello World" == Just { head: 'H', tail: "ello World" }
148+ -- | ```
149+ -- |
109150uncons :: String -> Maybe { head :: Char , tail :: String }
110151uncons " " = Nothing
111152uncons s = Just { head: U .charAt zero s, tail: drop one s }
112153
113154-- | Returns the longest prefix (possibly empty) of characters that satisfy
114155-- | the predicate.
115- -- | * `takeWhile (_ /= ':') "http://purescript.org" == "http"`
156+ -- |
157+ -- | ```purescript
158+ -- | takeWhile (_ /= ':') "http://purescript.org" == "http"
159+ -- | ```
160+ -- |
116161takeWhile :: (Char -> Boolean ) -> String -> String
117162takeWhile p s = take (count p s) s
118163
119164-- | Returns the suffix remaining after `takeWhile`.
165+ -- |
166+ -- | ```purescript
167+ -- | dropWhile (_ /= '.') "Test.purs" == ".purs"
168+ -- | ```
169+ -- |
120170dropWhile :: (Char -> Boolean ) -> String -> String
121171dropWhile p s = drop (count p s) s
122172
123173-- | If the string starts with the given prefix, return the portion of the
124174-- | 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+ -- |
127181stripPrefix :: Pattern -> String -> Maybe String
128182stripPrefix prefix@(Pattern prefixS) str =
129183 case indexOf prefix str of
@@ -133,26 +187,44 @@ stripPrefix prefix@(Pattern prefixS) str =
133187-- | If the string ends with the given suffix, return the portion of the
134188-- | string left after removing it, as a `Just` value. Otherwise, return
135189-- | `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+ -- |
138196stripSuffix :: Pattern -> String -> Maybe String
139197stripSuffix suffix@(Pattern suffixS) str =
140198 case lastIndexOf suffix str of
141199 Just x | x == length str - length suffixS -> Just $ take x str
142200 _ -> Nothing
143201
144202-- | Converts an array of characters into a string.
203+ -- |
204+ -- | ```purescript
205+ -- | fromCharArray ['H', 'e', 'l', 'l', 'o'] == "Hello"
206+ -- | ```
207+ -- |
145208foreign import fromCharArray :: Array Char -> String
146209
147210-- | 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+ -- |
150217contains :: Pattern -> String -> Boolean
151218contains pat = isJust <<< indexOf pat
152219
153220-- | Returns the index of the first occurrence of the pattern in the
154221-- | 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+ -- |
156228indexOf :: Pattern -> String -> Maybe Int
157229indexOf = _indexOf Just Nothing
158230
@@ -166,6 +238,12 @@ foreign import _indexOf
166238-- | Returns the index of the first occurrence of the pattern in the
167239-- | given string, starting at the specified index. Returns `Nothing` if there is
168240-- | no match.
241+ -- |
242+ -- | ```purescript
243+ -- | indexOf' (Pattern "a") 2 "ababa" == Just 2
244+ -- | indexOf' (Pattern "a") 3 "ababa" == Just 4
245+ -- | ```
246+ -- |
169247indexOf' :: Pattern -> Int -> String -> Maybe Int
170248indexOf' = _indexOf' Just Nothing
171249
@@ -179,6 +257,12 @@ foreign import _indexOf'
179257
180258-- | Returns the index of the last occurrence of the pattern in the
181259-- | 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+ -- |
182266lastIndexOf :: Pattern -> String -> Maybe Int
183267lastIndexOf = _lastIndexOf Just Nothing
184268
@@ -190,8 +274,16 @@ foreign import _lastIndexOf
190274 -> Maybe Int
191275
192276-- | 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+ -- |
195287lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
196288lastIndexOf' = _lastIndexOf' Just Nothing
197289
@@ -204,13 +296,22 @@ foreign import _lastIndexOf'
204296 -> Maybe Int
205297
206298-- | Returns the number of characters the string is composed of.
299+ -- |
300+ -- | ```purescript
301+ -- | length "Hello World" == 11
302+ -- | ```
303+ -- |
207304foreign import length :: String -> Int
208305
209306-- | Compare two strings in a locale-aware fashion. This is in contrast to
210307-- | the `Ord` instance on `String` which treats strings as arrays of code
211308-- | units:
212- -- | - `"ä" `localeCompare` "b" == LT`
213- -- | - `"ä" `compare` "b" == GT`
309+ -- |
310+ -- | ```purescript
311+ -- | "ä" `localeCompare` "b" == LT
312+ -- | "ä" `compare` "b" == GT
313+ -- | ```
314+ -- |
214315localeCompare :: String -> String -> Ordering
215316localeCompare = _localeCompare LT EQ GT
216317
@@ -223,28 +324,62 @@ foreign import _localeCompare
223324 -> Ordering
224325
225326-- | 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+ -- |
227332foreign import replace :: Pattern -> Replacement -> String -> String
228333
229334-- | 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+ -- |
230340foreign import replaceAll :: Pattern -> Replacement -> String -> String
231341
232342-- | Returns the first `n` characters of the string.
343+ -- |
344+ -- | ```purescript
345+ -- | take 5 "Hello World" == "Hello"
346+ -- | ```
347+ -- |
233348foreign import take :: Int -> String -> String
234349
235350-- | Returns the string without the first `n` characters.
351+ -- |
352+ -- | ```purescript
353+ -- | drop 6 "Hello World" == "World"
354+ -- | ```
355+ -- |
236356foreign import drop :: Int -> String -> String
237357
238358-- | Returns the number of contiguous characters at the beginning
239359-- | of the string for which the predicate holds.
360+ -- |
361+ -- | ```purescript
362+ -- | count (_ /= ' ') "Hello World" == 5 -- since length "Hello" == 5
363+ -- | ```
364+ -- |
240365foreign import count :: (Char -> Boolean ) -> String -> Int
241366
242367-- | Returns the substrings of the second string separated along occurences
243368-- | of the first string.
244- -- | * `split (Pattern " ") "hello world" == ["hello", "world"]`
369+ -- |
370+ -- | ```purescript
371+ -- | split (Pattern " ") "hello world" == ["hello", "world"]
372+ -- | ```
373+ -- |
245374foreign import split :: Pattern -> String -> Array String
246375
247376-- | 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+ -- |
248383splitAt :: Int -> String -> Maybe { before :: String , after :: String }
249384splitAt = _splitAt Just Nothing
250385
@@ -255,20 +390,44 @@ foreign import _splitAt :: (forall a. a -> Maybe a)
255390 -> Maybe { before :: String , after :: String }
256391
257392-- | Converts the string into an array of characters.
393+ -- |
394+ -- | ```purescript
395+ -- | toCharArray "Hello☺\n" == ['H','e','l','l','o','☺','\n']
396+ -- | ```
397+ -- |
258398foreign import toCharArray :: String -> Array Char
259399
260400-- | Returns the argument converted to lowercase.
401+ -- |
402+ -- | ```purescript
403+ -- | toLower "hElLo" == "hello"
404+ -- | ```
405+ -- |
261406foreign import toLower :: String -> String
262407
263408-- | Returns the argument converted to uppercase.
409+ -- |
410+ -- | ```purescript
411+ -- | toUpper "Hello" == "HELLO"
412+ -- | ```
413+ -- |
264414foreign import toUpper :: String -> String
265415
266416-- | Removes whitespace from the beginning and end of a string, including
267417-- | [whitespace characters](http://www.ecma-international.org/ecma-262/5.1/#sec-7.2)
268418-- | 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+ -- |
269424foreign import trim :: String -> String
270425
271426-- | Joins the strings in the array together, inserting the first argument
272427-- | 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+ -- |
274433foreign import joinWith :: String -> Array String -> String
0 commit comments