Skip to content

Commit c013a7e

Browse files
authored
Merge pull request #66 from purescript/bump
Prepare for 2.0 release
2 parents 3f7ad6f + 96b451a commit c013a7e

13 files changed

+254
-150
lines changed

bower.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-either": "^1.0.0",
21-
"purescript-maybe": "^1.0.0"
20+
"purescript-either": "^2.0.0",
21+
"purescript-maybe": "^2.0.0"
2222
},
2323
"devDependencies": {
24-
"purescript-assert": "^1.0.0",
25-
"purescript-console": "^1.0.0",
24+
"purescript-assert": "^2.0.0",
25+
"purescript-console": "^2.0.0",
2626
"purescript-partial": "^1.1.2"
2727
}
2828
}

src/Data/Char.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/* global exports */
21
"use strict";
32

4-
// module Data.Char
5-
63
exports.toCharCode = function (c) {
74
return c.charCodeAt(0);
85
};

src/Data/String.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/* global exports */
21
"use strict";
32

4-
// module Data.String
5-
63
exports._charAt = function (just) {
74
return function (nothing) {
85
return function (i) {
@@ -114,6 +111,14 @@ exports.replace = function (s1) {
114111
};
115112
};
116113

114+
exports.replaceAll = function (s1) {
115+
return function (s2) {
116+
return function (s3) {
117+
return s3.replace(new RegExp(s1.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"), "g"), s2);
118+
};
119+
};
120+
};
121+
117122
exports.take = function (n) {
118123
return function (s) {
119124
return s.substr(0, n);

src/Data/String.purs

+99-66
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
-- | A String represents a sequence of characters.
33
-- | For details of the underlying implementation, see [String Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
44
module Data.String
5-
( charAt
5+
( Pattern(..)
6+
, Replacement(..)
7+
, charAt
68
, charCodeAt
79
, fromCharArray
810
, toChar
@@ -17,6 +19,7 @@ module Data.String
1719
, singleton
1820
, localeCompare
1921
, replace
22+
, replaceAll
2023
, take
2124
, takeWhile
2225
, drop
@@ -36,17 +39,39 @@ module Data.String
3639
import Prelude
3740

3841
import Data.Maybe (Maybe(..), isJust)
42+
import Data.Newtype (class Newtype)
3943
import Data.String.Unsafe as U
4044

45+
-- | A newtype used in cases where there is a string to be matched.
46+
newtype Pattern = Pattern String
47+
48+
derive instance eqPattern :: Eq Pattern
49+
derive instance ordPattern :: Ord Pattern
50+
derive instance newtypePattern :: Newtype Pattern _
51+
52+
instance showPattern :: Show Pattern where
53+
show (Pattern s) = "(Pattern " <> s <> ")"
54+
55+
-- | A newtype used in cases to specify a replacement for a pattern.
56+
newtype Replacement = Replacement String
57+
58+
derive instance eqReplacement :: Eq Replacement
59+
derive instance ordReplacement :: Ord Replacement
60+
derive instance newtypeReplacement :: Newtype Replacement _
61+
62+
instance showReplacement :: Show Replacement where
63+
show (Replacement s) = "(Replacement " <> s <> ")"
64+
4165
-- | Returns the character at the given index, if the index is within bounds.
4266
charAt :: Int -> String -> Maybe Char
4367
charAt = _charAt Just Nothing
4468

45-
foreign import _charAt :: (forall a. a -> Maybe a)
46-
-> (forall a. Maybe a)
47-
-> Int
48-
-> String
49-
-> Maybe Char
69+
foreign import _charAt
70+
:: (forall a. a -> Maybe a)
71+
-> (forall a. Maybe a)
72+
-> Int
73+
-> String
74+
-> Maybe Char
5075

5176
-- | Returns a string of length `1` containing the given character.
5277
foreign import singleton :: Char -> String
@@ -56,19 +81,21 @@ foreign import singleton :: Char -> String
5681
charCodeAt :: Int -> String -> Maybe Int
5782
charCodeAt = _charCodeAt Just Nothing
5883

59-
foreign import _charCodeAt :: (forall a. a -> Maybe a)
60-
-> (forall a. Maybe a)
61-
-> Int
62-
-> String
63-
-> Maybe Int
84+
foreign import _charCodeAt
85+
:: (forall a. a -> Maybe a)
86+
-> (forall a. Maybe a)
87+
-> Int
88+
-> String
89+
-> Maybe Int
6490

6591
toChar :: String -> Maybe Char
6692
toChar = _toChar Just Nothing
6793

68-
foreign import _toChar :: (forall a. a -> Maybe a)
69-
-> (forall a. Maybe a)
70-
-> String
71-
-> Maybe Char
94+
foreign import _toChar
95+
:: (forall a. a -> Maybe a)
96+
-> (forall a. Maybe a)
97+
-> String
98+
-> Maybe Char
7299

73100
-- | Returns `true` if the given string is empty.
74101
null :: String -> Boolean
@@ -81,7 +108,7 @@ uncons "" = Nothing
81108
uncons s = Just { head: U.charAt zero s, tail: drop one s }
82109

83110
-- | Returns the longest prefix (possibly empty) of characters that satisfy
84-
-- | the predicate:
111+
-- | the predicate.
85112
takeWhile :: (Char -> Boolean) -> String -> String
86113
takeWhile p s = take (count p s) s
87114

@@ -91,80 +118,82 @@ dropWhile p s = drop (count p s) s
91118

92119
-- | If the string starts with the given prefix, return the portion of the
93120
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
94-
-- | * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"`
95-
-- | * `stripPrefix "http:" "https://purescript.org" == Nothing`
96-
stripPrefix :: String -> String -> Maybe String
97-
stripPrefix prefix str =
121+
-- | * `stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"`
122+
-- | * `stripPrefix (Pattern "http:") "https://purescript.org" == Nothing`
123+
stripPrefix :: Pattern -> String -> Maybe String
124+
stripPrefix prefix@(Pattern prefixS) str =
98125
case indexOf prefix str of
99-
Just 0 -> Just $ drop (length prefix) str
100-
_ -> Nothing
126+
Just 0 -> Just $ drop (length prefixS) str
127+
_ -> Nothing
101128

102129
-- | If the string ends with the given suffix, return the portion of the
103130
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
104-
-- | * `stripSuffix ".exe" "psc.exe" == Just "psc"`
105-
-- | * `stripSuffix ".exe" "psc" == Nothing`
106-
stripSuffix :: String -> String -> Maybe String
107-
stripSuffix suffix str =
131+
-- | * `stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"`
132+
-- | * `stripSuffix (Pattern ".exe") "psc" == Nothing`
133+
stripSuffix :: Pattern -> String -> Maybe String
134+
stripSuffix suffix@(Pattern suffixS) str =
108135
case lastIndexOf suffix str of
109-
Just x | x == length str - length suffix ->
110-
Just $ take x str
111-
_ ->
112-
Nothing
136+
Just x | x == length str - length suffixS -> Just $ take x str
137+
_ -> Nothing
113138

114139
-- | Converts an array of characters into a string.
115140
foreign import fromCharArray :: Array Char -> String
116141

117142
-- | Checks whether the first string exists in the second string.
118-
contains :: String -> String -> Boolean
119-
contains x s = isJust (indexOf x s)
143+
contains :: Pattern -> String -> Boolean
144+
contains pat = isJust <<< indexOf pat
120145

121146
-- | Returns the index of the first occurrence of the first string in the
122147
-- | second string. Returns `Nothing` if there is no match.
123-
indexOf :: String -> String -> Maybe Int
148+
indexOf :: Pattern -> String -> Maybe Int
124149
indexOf = _indexOf Just Nothing
125150

126-
foreign import _indexOf :: (forall a. a -> Maybe a)
127-
-> (forall a. Maybe a)
128-
-> String
129-
-> String
130-
-> Maybe Int
151+
foreign import _indexOf
152+
:: (forall a. a -> Maybe a)
153+
-> (forall a. Maybe a)
154+
-> Pattern
155+
-> String
156+
-> Maybe Int
131157

132158
-- | Returns the index of the first occurrence of the first string in the
133159
-- | second string, starting at the given index. Returns `Nothing` if there is
134160
-- | no match.
135-
indexOf' :: String -> Int -> String -> Maybe Int
161+
indexOf' :: Pattern -> Int -> String -> Maybe Int
136162
indexOf' = _indexOf' Just Nothing
137163

138-
foreign import _indexOf' :: (forall a. a -> Maybe a)
139-
-> (forall a. Maybe a)
140-
-> String
141-
-> Int
142-
-> String
143-
-> Maybe Int
164+
foreign import _indexOf'
165+
:: (forall a. a -> Maybe a)
166+
-> (forall a. Maybe a)
167+
-> Pattern
168+
-> Int
169+
-> String
170+
-> Maybe Int
144171

145172
-- | Returns the index of the last occurrence of the first string in the
146173
-- | second string. Returns `Nothing` if there is no match.
147-
lastIndexOf :: String -> String -> Maybe Int
174+
lastIndexOf :: Pattern -> String -> Maybe Int
148175
lastIndexOf = _lastIndexOf Just Nothing
149176

150-
foreign import _lastIndexOf :: (forall a. a -> Maybe a)
151-
-> (forall a. Maybe a)
152-
-> String
153-
-> String
154-
-> Maybe Int
177+
foreign import _lastIndexOf
178+
:: (forall a. a -> Maybe a)
179+
-> (forall a. Maybe a)
180+
-> Pattern
181+
-> String
182+
-> Maybe Int
155183

156184
-- | Returns the index of the last occurrence of the first string in the
157185
-- | second string, starting at the given index. Returns `Nothing` if there is
158186
-- | no match.
159-
lastIndexOf' :: String -> Int -> String -> Maybe Int
187+
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
160188
lastIndexOf' = _lastIndexOf' Just Nothing
161189

162-
foreign import _lastIndexOf' :: (forall a. a -> Maybe a)
163-
-> (forall a. Maybe a)
164-
-> String
165-
-> Int
166-
-> String
167-
-> Maybe Int
190+
foreign import _lastIndexOf'
191+
:: (forall a. a -> Maybe a)
192+
-> (forall a. Maybe a)
193+
-> Pattern
194+
-> Int
195+
-> String
196+
-> Maybe Int
168197

169198
-- | Returns the number of characters the string is composed of.
170199
foreign import length :: String -> Int
@@ -173,15 +202,19 @@ foreign import length :: String -> Int
173202
localeCompare :: String -> String -> Ordering
174203
localeCompare = _localeCompare LT EQ GT
175204

176-
foreign import _localeCompare :: Ordering
177-
-> Ordering
178-
-> Ordering
179-
-> String
180-
-> String
181-
-> Ordering
205+
foreign import _localeCompare
206+
:: Ordering
207+
-> Ordering
208+
-> Ordering
209+
-> String
210+
-> String
211+
-> Ordering
182212

183213
-- | Replaces the first occurence of the first argument with the second argument.
184-
foreign import replace :: String -> String -> String -> String
214+
foreign import replace :: Pattern -> Replacement -> String -> String
215+
216+
-- | Replaces all occurences of the first argument with the second argument.
217+
foreign import replaceAll :: Pattern -> Replacement -> String -> String
185218

186219
-- | Returns the first `n` characters of the string.
187220
foreign import take :: Int -> String -> String
@@ -196,7 +229,7 @@ foreign import count :: (Char -> Boolean) -> String -> Int
196229
-- | Returns the substrings of the second string separated along occurences
197230
-- | of the first string.
198231
-- | * `split " " "hello world" == ["hello", "world"]`
199-
foreign import split :: String -> String -> Array String
232+
foreign import split :: Pattern -> String -> Array String
200233

201234
-- | Returns the substrings of split at the given index, if the index is within bounds.
202235
splitAt :: Int -> String -> Maybe (Array String)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Data.String.CaseInsensitive where
2+
3+
import Prelude
4+
5+
import Data.Newtype (class Newtype)
6+
import Data.String (toLower)
7+
8+
-- | A newtype for case insensitive string comparisons and ordering.
9+
newtype CaseInsensitiveString = CaseInsensitiveString String
10+
11+
instance eqCaseInsensitiveString :: Eq CaseInsensitiveString where
12+
eq (CaseInsensitiveString s1) (CaseInsensitiveString s2) =
13+
toLower s1 == toLower s2
14+
15+
instance ordCaseInsensitiveString :: Ord CaseInsensitiveString where
16+
compare (CaseInsensitiveString s1) (CaseInsensitiveString s2) =
17+
compare (toLower s1) (toLower s2)
18+
19+
instance showCaseInsensitiveString :: Show CaseInsensitiveString where
20+
show (CaseInsensitiveString s) = "(CaseInsensitiveString " <> s <> ")"
21+
22+
derive instance newtypeCaseInsensitiveString :: Newtype CaseInsensitiveString _

src/Data/String/Regex.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/* global exports */
21
"use strict";
32

4-
// module Data.String.Regex
5-
63
exports["showRegex'"] = function (r) {
74
return "" + r;
85
};

0 commit comments

Comments
 (0)