Skip to content

Commit 6558a55

Browse files
sharkdphdgarrood
authored andcommitted
Add usage examples to the documentation (#88)
* Add usage examples to the documentation * Detailed explanation for 'localeCompare', add results
1 parent 43d8699 commit 6558a55

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

src/Data/String.purs

+29-15
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ foreign import singleton :: Char -> String
7878

7979
-- | Returns the numeric Unicode value of the character at the given index,
8080
-- | if the index is within bounds.
81+
-- | - `charCodeAt 2 "5 €" == Just 0x20AC`
8182
charCodeAt :: Int -> String -> Maybe Int
8283
charCodeAt = _charCodeAt Just Nothing
8384

@@ -88,6 +89,8 @@ foreign import _charCodeAt
8889
-> String
8990
-> Maybe Int
9091

92+
-- | Converts the string to a character, if the length of the string is
93+
-- | exactly `1`.
9194
toChar :: String -> Maybe Char
9295
toChar = _toChar Just Nothing
9396

@@ -109,6 +112,7 @@ uncons s = Just { head: U.charAt zero s, tail: drop one s }
109112

110113
-- | Returns the longest prefix (possibly empty) of characters that satisfy
111114
-- | the predicate.
115+
-- | * `takeWhile (_ /= ':') "http://purescript.org" == "http"`
112116
takeWhile :: (Char -> Boolean) -> String -> String
113117
takeWhile p s = take (count p s) s
114118

@@ -127,7 +131,8 @@ stripPrefix prefix@(Pattern prefixS) str =
127131
_ -> Nothing
128132

129133
-- | If the string ends with the given suffix, return the portion of the
130-
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
134+
-- | string left after removing it, as a `Just` value. Otherwise, return
135+
-- | `Nothing`.
131136
-- | * `stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"`
132137
-- | * `stripSuffix (Pattern ".exe") "psc" == Nothing`
133138
stripSuffix :: Pattern -> String -> Maybe String
@@ -139,12 +144,15 @@ stripSuffix suffix@(Pattern suffixS) str =
139144
-- | Converts an array of characters into a string.
140145
foreign import fromCharArray :: Array Char -> String
141146

142-
-- | Checks whether the first string exists in the second string.
147+
-- | Checks whether the pattern appears in the given string.
148+
-- | * `contains (Pattern "needle") "haystack with needle" == true`
149+
-- | * `contains (Pattern "needle") "haystack" == false`
143150
contains :: Pattern -> String -> Boolean
144151
contains pat = isJust <<< indexOf pat
145152

146-
-- | Returns the index of the first occurrence of the first string in the
147-
-- | second string. Returns `Nothing` if there is no match.
153+
-- | Returns the index of the first occurrence of the pattern in the
154+
-- | given string. Returns `Nothing` if there is no match.
155+
-- | * `indexOf (Pattern "c") "abcde" == Just 2`
148156
indexOf :: Pattern -> String -> Maybe Int
149157
indexOf = _indexOf Just Nothing
150158

@@ -155,8 +163,8 @@ foreign import _indexOf
155163
-> String
156164
-> Maybe Int
157165

158-
-- | Returns the index of the first occurrence of the first string in the
159-
-- | second string, starting at the given index. Returns `Nothing` if there is
166+
-- | Returns the index of the first occurrence of the pattern in the
167+
-- | given string, starting at the specified index. Returns `Nothing` if there is
160168
-- | no match.
161169
indexOf' :: Pattern -> Int -> String -> Maybe Int
162170
indexOf' = _indexOf' Just Nothing
@@ -169,8 +177,8 @@ foreign import _indexOf'
169177
-> String
170178
-> Maybe Int
171179

172-
-- | Returns the index of the last occurrence of the first string in the
173-
-- | second string. Returns `Nothing` if there is no match.
180+
-- | Returns the index of the last occurrence of the pattern in the
181+
-- | given string. Returns `Nothing` if there is no match.
174182
lastIndexOf :: Pattern -> String -> Maybe Int
175183
lastIndexOf = _lastIndexOf Just Nothing
176184

@@ -181,9 +189,9 @@ foreign import _lastIndexOf
181189
-> String
182190
-> Maybe Int
183191

184-
-- | Returns the index of the last occurrence of the first string in the
185-
-- | second string, starting at the given index. Returns `Nothing` if there is
186-
-- | no match.
192+
-- | 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.
187195
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
188196
lastIndexOf' = _lastIndexOf' Just Nothing
189197

@@ -198,7 +206,11 @@ foreign import _lastIndexOf'
198206
-- | Returns the number of characters the string is composed of.
199207
foreign import length :: String -> Int
200208

201-
-- | Locale-aware sort order comparison.
209+
-- | Compare two strings in a locale-aware fashion. This is in contrast to
210+
-- | the `Ord` instance on `String` which treats strings as arrays of code
211+
-- | units:
212+
-- | - `"ä" `localeCompare` "b" == LT`
213+
-- | - `"ä" `compare` "b" == GT`
202214
localeCompare :: String -> String -> Ordering
203215
localeCompare = _localeCompare LT EQ GT
204216

@@ -210,10 +222,11 @@ foreign import _localeCompare
210222
-> String
211223
-> Ordering
212224

213-
-- | Replaces the first occurence of the first argument with the second argument.
225+
-- | Replaces the first occurence of the pattern with the replacement string.
226+
-- | * `replace (Pattern "http") (Replacement "https") "http://purescript.org" == "https://purescript.org"`
214227
foreign import replace :: Pattern -> Replacement -> String -> String
215228

216-
-- | Replaces all occurences of the first argument with the second argument.
229+
-- | Replaces all occurences of the pattern with the replacement string.
217230
foreign import replaceAll :: Pattern -> Replacement -> String -> String
218231

219232
-- | Returns the first `n` characters of the string.
@@ -231,7 +244,7 @@ foreign import count :: (Char -> Boolean) -> String -> Int
231244
-- | * `split (Pattern " ") "hello world" == ["hello", "world"]`
232245
foreign import split :: Pattern -> String -> Array String
233246

234-
-- | Returns the substrings of split at the given index, if the index is within bounds.
247+
-- | Returns the substrings of a split at the given index, if the index is within bounds.
235248
splitAt :: Int -> String -> Maybe { before :: String, after :: String }
236249
splitAt = _splitAt Just Nothing
237250

@@ -257,4 +270,5 @@ foreign import trim :: String -> String
257270

258271
-- | Joins the strings in the array together, inserting the first argument
259272
-- | as separator between them.
273+
-- | * `joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange"`
260274
foreign import joinWith :: String -> Array String -> String

0 commit comments

Comments
 (0)