|
1 | | --- | Wraps the functions of Javascript's `String` object. |
2 | | --- | A String represents a sequence of characters. |
3 | | --- | For details of the underlying implementation, see [String Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). |
4 | 1 | module Data.String |
5 | 2 | ( module Data.String.Pattern |
6 | | - , contains |
7 | | - , null |
8 | | - , localeCompare |
9 | | - , replace |
10 | | - , replaceAll |
11 | | - , stripPrefix |
12 | | - , stripSuffix |
13 | | - , split |
14 | | - , toLower |
15 | | - , toUpper |
16 | | - , trim |
17 | | - , joinWith |
| 3 | + , module Data.String.Common |
| 4 | + , module Data.String.CodeUnits |
18 | 5 | ) where |
19 | 6 |
|
20 | | -import Prelude |
21 | | - |
22 | | -import Data.Maybe (Maybe(..), isJust) |
23 | | -import Data.String.CodeUnits as SCU |
24 | 7 | import Data.String.Pattern (Pattern(..), Replacement(..)) |
25 | | - |
26 | | --- | Returns `true` if the given string is empty. |
27 | | --- | |
28 | | --- | ```purescript |
29 | | --- | null "" == true |
30 | | --- | null "Hi" == false |
31 | | --- | ``` |
32 | | -null :: String -> Boolean |
33 | | -null s = s == "" |
34 | | - |
35 | | --- | If the string starts with the given prefix, return the portion of the |
36 | | --- | string left after removing it, as a Just value. Otherwise, return Nothing. |
37 | | --- | |
38 | | --- | ```purescript |
39 | | --- | stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org" |
40 | | --- | stripPrefix (Pattern "http:") "https://purescript.org" == Nothing |
41 | | --- | ``` |
42 | | -stripPrefix :: Pattern -> String -> Maybe String |
43 | | -stripPrefix prefix@(Pattern prefixS) str = |
44 | | - case SCU.indexOf prefix str of |
45 | | - Just 0 -> Just $ SCU.drop (SCU.length prefixS) str |
46 | | - _ -> Nothing |
47 | | - |
48 | | --- | If the string ends with the given suffix, return the portion of the |
49 | | --- | string left after removing it, as a `Just` value. Otherwise, return |
50 | | --- | `Nothing`. |
51 | | --- | |
52 | | --- | ```purescript |
53 | | --- | stripSuffix (Pattern ".exe") "psc.exe" == Just "psc" |
54 | | --- | stripSuffix (Pattern ".exe") "psc" == Nothing |
55 | | --- | ``` |
56 | | -stripSuffix :: Pattern -> String -> Maybe String |
57 | | -stripSuffix suffix@(Pattern suffixS) str = |
58 | | - case SCU.lastIndexOf suffix str of |
59 | | - Just x | x == SCU.length str - SCU.length suffixS -> Just $ SCU.take x str |
60 | | - _ -> Nothing |
61 | | - |
62 | | --- | Checks whether the pattern appears in the given string. |
63 | | --- | |
64 | | --- | ```purescript |
65 | | --- | contains (Pattern "needle") "haystack with needle" == true |
66 | | --- | contains (Pattern "needle") "haystack" == false |
67 | | --- | ``` |
68 | | -contains :: Pattern -> String -> Boolean |
69 | | -contains pat = isJust <<< SCU.indexOf pat |
70 | | - |
71 | | --- | Compare two strings in a locale-aware fashion. This is in contrast to |
72 | | --- | the `Ord` instance on `String` which treats strings as arrays of code |
73 | | --- | units: |
74 | | --- | |
75 | | --- | ```purescript |
76 | | --- | "ä" `localeCompare` "b" == LT |
77 | | --- | "ä" `compare` "b" == GT |
78 | | --- | ``` |
79 | | -localeCompare :: String -> String -> Ordering |
80 | | -localeCompare = _localeCompare LT EQ GT |
81 | | - |
82 | | -foreign import _localeCompare |
83 | | - :: Ordering |
84 | | - -> Ordering |
85 | | - -> Ordering |
86 | | - -> String |
87 | | - -> String |
88 | | - -> Ordering |
89 | | - |
90 | | --- | Replaces the first occurence of the pattern with the replacement string. |
91 | | --- | |
92 | | --- | ```purescript |
93 | | --- | replace (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b <= c" |
94 | | --- | ``` |
95 | | -foreign import replace :: Pattern -> Replacement -> String -> String |
96 | | - |
97 | | --- | Replaces all occurences of the pattern with the replacement string. |
98 | | --- | |
99 | | --- | ```purescript |
100 | | --- | replaceAll (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b ≤ c" |
101 | | --- | ``` |
102 | | -foreign import replaceAll :: Pattern -> Replacement -> String -> String |
103 | | - |
104 | | --- | Returns the substrings of the second string separated along occurences |
105 | | --- | of the first string. |
106 | | --- | |
107 | | --- | ```purescript |
108 | | --- | split (Pattern " ") "hello world" == ["hello", "world"] |
109 | | --- | ``` |
110 | | -foreign import split :: Pattern -> String -> Array String |
111 | | - |
112 | | --- | Returns the argument converted to lowercase. |
113 | | --- | |
114 | | --- | ```purescript |
115 | | --- | toLower "hElLo" == "hello" |
116 | | --- | ``` |
117 | | -foreign import toLower :: String -> String |
118 | | - |
119 | | --- | Returns the argument converted to uppercase. |
120 | | --- | |
121 | | --- | ```purescript |
122 | | --- | toUpper "Hello" == "HELLO" |
123 | | --- | ``` |
124 | | -foreign import toUpper :: String -> String |
125 | | - |
126 | | --- | Removes whitespace from the beginning and end of a string, including |
127 | | --- | [whitespace characters](http://www.ecma-international.org/ecma-262/5.1/#sec-7.2) |
128 | | --- | and [line terminators](http://www.ecma-international.org/ecma-262/5.1/#sec-7.3). |
129 | | --- | |
130 | | --- | ```purescript |
131 | | --- | trim " Hello \n World\n\t " == "Hello \n World" |
132 | | --- | ``` |
133 | | -foreign import trim :: String -> String |
134 | | - |
135 | | --- | Joins the strings in the array together, inserting the first argument |
136 | | --- | as separator between them. |
137 | | --- | |
138 | | --- | ```purescript |
139 | | --- | joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange" |
140 | | --- | ``` |
141 | | -foreign import joinWith :: String -> Array String -> String |
| 8 | +import Data.String.Common (joinWith, localeCompare, null, replace, replaceAll, split, toLower, toUpper, trim) |
| 9 | +import Data.String.CodeUnits (contains, stripPrefix, stripSuffix) |
0 commit comments