Skip to content

Commit 24a912f

Browse files
committed
Merge pull request #41 from LiamGoodacre/master
Uppercase/lowercase on Char, tests, and more
2 parents d6eb480 + 1523fc3 commit 24a912f

File tree

15 files changed

+327
-13
lines changed

15 files changed

+327
-13
lines changed

bower.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
],
2121
"dependencies": {
2222
"purescript-maybe": "^0.3.0"
23+
},
24+
"devDependencies": {
25+
"purescript-assert": "~0.1.0",
26+
"purescript-console": "~0.1.0"
2327
}
2428
}

docs/Data/Char.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,20 @@ fromCharCode :: Int -> Char
2626

2727
Constructs a character from the given Unicode numeric value.
2828

29+
#### `toLower`
30+
31+
``` purescript
32+
toLower :: Char -> Char
33+
```
34+
35+
Converts a character to lowercase.
36+
37+
#### `toUpper`
38+
39+
``` purescript
40+
toUpper :: Char -> Char
41+
```
42+
43+
Converts a character to uppercase.
44+
2945

docs/Data/String/Regex.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ See the [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
115115
#### `search`
116116

117117
``` purescript
118-
search :: Regex -> String -> Int
118+
search :: Regex -> String -> Maybe Int
119119
```
120120

121-
Returns the index of the first match of the `Regex` in the string, or
122-
`-1` if there is no match.
121+
Returns `Just` the index of the first match of the `Regex` in the string,
122+
or `Nothing` if there is no match.
123123

124124
#### `split`
125125

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"scripts": {
44
"postinstall": "pulp dep install",
5-
"build": "jshint src && jscs src && pulp build && rimraf docs && pulp docs"
5+
"build": "jshint src && jscs src && pulp build && pulp test && rimraf docs && pulp docs"
66
},
77
"devDependencies": {
88
"jscs": "^1.13.1",

src/Data/Char.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ exports.toCharCode = function (c) {
1414
exports.fromCharCode = function (c) {
1515
return String.fromCharCode(c);
1616
};
17+
18+
exports.toLower = function (c) {
19+
return c.toLowerCase();
20+
};
21+
22+
exports.toUpper = function (c) {
23+
return c.toUpperCase();
24+
};

src/Data/Char.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module Data.Char
33
( toString
44
, fromCharCode
55
, toCharCode
6+
, toLower
7+
, toUpper
68
) where
79

810
import Prelude
@@ -16,6 +18,12 @@ foreign import toCharCode :: Char -> Int
1618
-- | Constructs a character from the given Unicode numeric value.
1719
foreign import fromCharCode :: Int -> Char
1820

21+
-- | Converts a character to lowercase.
22+
foreign import toLower :: Char -> Char
23+
24+
-- | Converts a character to uppercase.
25+
foreign import toUpper :: Char -> Char
26+
1927
-- | Characters fall within the Unicode range.
2028
instance boundedChar :: Bounded Char where
2129
top = fromCharCode zero

src/Data/String.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ exports["_indexOf'"] = function (just) {
5151
return function (x) {
5252
return function (startAt) {
5353
return function (s) {
54+
if (startAt < 0 || startAt > s.length) return nothing;
5455
var i = s.indexOf(x, startAt);
5556
return i === -1 ? nothing : just(i);
5657
};
@@ -75,6 +76,7 @@ exports["_lastIndexOf'"] = function (just) {
7576
return function (x) {
7677
return function (startAt) {
7778
return function (s) {
79+
if (startAt < 0 || startAt > s.length) return nothing;
7880
var i = s.lastIndexOf(x, startAt);
7981
return i === -1 ? nothing : just(i);
8082
};
@@ -93,7 +95,7 @@ exports._localeCompare = function (lt) {
9395
return function (s1) {
9496
return function (s2) {
9597
var result = s1.localeCompare(s2);
96-
return result < 0 ? lt : result > 1 ? gt : eq;
98+
return result < 0 ? lt : result > 0 ? gt : eq;
9799
};
98100
};
99101
};

src/Data/String.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module Data.String
3434
) where
3535

3636
import Prelude
37-
import Data.Char
37+
import qualified Data.Char as C
3838
import Data.Maybe (Maybe(..), isJust)
3939
import Data.Monoid (Monoid)
4040
import qualified Data.String.Unsafe as U
@@ -51,7 +51,7 @@ foreign import _charAt :: (forall a. a -> Maybe a)
5151

5252
-- | Returns a string of length `1` containing the given character.
5353
fromChar :: Char -> String
54-
fromChar = toString
54+
fromChar = C.toString
5555

5656
-- | Returns a string of length `1` containing the given character.
5757
-- | Same as `fromChar`.

src/Data/String/Regex.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ exports["replace'"] = function (r) {
7070
};
7171
};
7272

73-
exports.search = function (r) {
74-
return function (s) {
75-
return s.search(r);
73+
exports._search = function (just) {
74+
return function (nothing) {
75+
return function (r) {
76+
return function (s) {
77+
var result = s.search(r);
78+
return result === -1 ? nothing : just(result);
79+
};
80+
};
7681
};
7782
};
7883

src/Data/String/Regex.purs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,16 @@ foreign import replace :: Regex -> String -> String -> String
104104
-- | See the [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter).
105105
foreign import replace' :: Regex -> (String -> Array String -> String) -> String -> String
106106

107-
-- | Returns the index of the first match of the `Regex` in the string, or
108-
-- | `-1` if there is no match.
109-
foreign import search :: Regex -> String -> Int
107+
foreign import _search :: (forall r. r -> Maybe r)
108+
-> (forall r. Maybe r)
109+
-> Regex
110+
-> String
111+
-> Maybe Int
112+
113+
-- | Returns `Just` the index of the first match of the `Regex` in the string,
114+
-- | or `Nothing` if there is no match.
115+
search :: Regex -> String -> Maybe Int
116+
search = _search Just Nothing
110117

111118
-- | Split the string into an array of substrings along occurences of the `Regex`.
112119
foreign import split :: Regex -> String -> Array String

test/Test/Data/Char.purs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Test.Data.Char (testChar) where
2+
3+
import Prelude
4+
import Control.Monad.Eff.Console (log)
5+
import Data.Char
6+
import Test.Assert (assert)
7+
8+
testChar = do
9+
log "toString"
10+
assert $ toString 'a' == "a"
11+
12+
log "toCharCode"
13+
assert $ toCharCode 'a' == 97
14+
assert $ toCharCode '\n' == 10
15+
16+
log "fromCharCode"
17+
assert $ fromCharCode 97 == 'a'
18+
assert $ fromCharCode 10 == '\n'
19+
20+
log "toLower"
21+
assert $ toLower 'A' == 'a'
22+
assert $ toLower 'a' == 'a'
23+
24+
log "toUpper"
25+
assert $ toUpper 'a' == 'A'
26+
assert $ toUpper 'A' == 'A'
27+

test/Test/Data/String.purs

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
module Test.Data.String (testString) where
2+
3+
import Prelude
4+
import Data.Maybe
5+
import Control.Monad.Eff.Console (log)
6+
import Data.String
7+
import Test.Assert (assert)
8+
9+
testString = do
10+
log "charAt"
11+
assert $ charAt 0 "" == Nothing
12+
assert $ charAt 0 "a" == Just 'a'
13+
assert $ charAt 1 "a" == Nothing
14+
assert $ charAt 0 "ab" == Just 'a'
15+
assert $ charAt 1 "ab" == Just 'b'
16+
assert $ charAt 2 "ab" == Nothing
17+
18+
log "fromChar"
19+
assert $ fromChar 'a' == "a"
20+
21+
log "singleton"
22+
assert $ singleton 'a' == "a"
23+
24+
log "charCodeAt"
25+
assert $ charCodeAt 0 "" == Nothing
26+
assert $ charCodeAt 0 "a" == Just 97
27+
assert $ charCodeAt 1 "a" == Nothing
28+
assert $ charCodeAt 0 "ab" == Just 97
29+
assert $ charCodeAt 1 "ab" == Just 98
30+
assert $ charCodeAt 2 "ab" == Nothing
31+
32+
log "toChar"
33+
assert $ toChar "" == Nothing
34+
assert $ toChar "a" == Just 'a'
35+
assert $ toChar "ab" == Nothing
36+
37+
log "null"
38+
assert $ null ""
39+
assert $ not (null "a")
40+
41+
log "uncons"
42+
assert $ isNothing (uncons "")
43+
assert $ case uncons "a" of
44+
Nothing -> false
45+
Just m -> m.head == 'a' && m.tail == ""
46+
assert $ case uncons "ab" of
47+
Nothing -> false
48+
Just m -> m.head == 'a' && m.tail == "b"
49+
50+
log "takeWhile"
51+
assert $ takeWhile (\c -> true) "abc" == "abc"
52+
assert $ takeWhile (\c -> false) "abc" == ""
53+
assert $ takeWhile (\c -> c /= 'b') "aabbcc" == "aa"
54+
55+
log "dropWhile"
56+
assert $ dropWhile (\c -> true) "abc" == ""
57+
assert $ dropWhile (\c -> false) "abc" == "abc"
58+
assert $ dropWhile (\c -> c /= 'b') "aabbcc" == "bbcc"
59+
60+
log "stripPrefix"
61+
assert $ stripPrefix "" "" == Just ""
62+
assert $ stripPrefix "" "abc" == Just "abc"
63+
assert $ stripPrefix "a" "abc" == Just "bc"
64+
assert $ stripPrefix "!" "abc" == Nothing
65+
assert $ stripPrefix "!" "" == Nothing
66+
67+
log "fromCharArray"
68+
assert $ fromCharArray [] == ""
69+
assert $ fromCharArray ['a', 'b'] == "ab"
70+
71+
log "contains"
72+
assert $ contains "" ""
73+
assert $ contains "" "abcd"
74+
assert $ contains "bc" "abcd"
75+
assert $ not (contains "cb" "abcd")
76+
77+
log "indexOf"
78+
assert $ indexOf "" "" == Just 0
79+
assert $ indexOf "" "abcd" == Just 0
80+
assert $ indexOf "bc" "abcd" == Just 1
81+
assert $ indexOf "cb" "abcd" == Nothing
82+
83+
log "indexOf'"
84+
assert $ indexOf' "" 0 "" == Just 0
85+
assert $ indexOf' "" (-1) "ab" == Nothing
86+
assert $ indexOf' "" 0 "ab" == Just 0
87+
assert $ indexOf' "" 1 "ab" == Just 1
88+
assert $ indexOf' "" 2 "ab" == Just 2
89+
assert $ indexOf' "" 3 "ab" == Nothing
90+
assert $ indexOf' "bc" 0 "abcd" == Just 1
91+
assert $ indexOf' "bc" 1 "abcd" == Just 1
92+
assert $ indexOf' "bc" 2 "abcd" == Nothing
93+
assert $ indexOf' "cb" 0 "abcd" == Nothing
94+
95+
log "lastIndexOf"
96+
assert $ lastIndexOf "" "" == Just 0
97+
assert $ lastIndexOf "" "abcd" == Just 4
98+
assert $ lastIndexOf "bc" "abcd" == Just 1
99+
assert $ lastIndexOf "cb" "abcd" == Nothing
100+
101+
log "lastIndexOf'"
102+
assert $ lastIndexOf' "" 0 "" == Just 0
103+
assert $ lastIndexOf' "" (-1) "ab" == Nothing
104+
assert $ lastIndexOf' "" 0 "ab" == Just 0
105+
assert $ lastIndexOf' "" 1 "ab" == Just 1
106+
assert $ lastIndexOf' "" 2 "ab" == Just 2
107+
assert $ lastIndexOf' "" 3 "ab" == Nothing
108+
assert $ lastIndexOf' "bc" 0 "abcd" == Nothing
109+
assert $ lastIndexOf' "bc" 1 "abcd" == Just 1
110+
assert $ lastIndexOf' "bc" 2 "abcd" == Just 1
111+
assert $ lastIndexOf' "cb" 0 "abcd" == Nothing
112+
113+
log "length"
114+
assert $ length "" == 0
115+
assert $ length "a" == 1
116+
assert $ length "ab" == 2
117+
118+
log "localeCompare"
119+
assert $ localeCompare "" "" == EQ
120+
assert $ localeCompare "a" "a" == EQ
121+
assert $ localeCompare "a" "b" == LT
122+
assert $ localeCompare "b" "a" == GT
123+
124+
log "replace"
125+
assert $ replace "b" "" "abc" == "ac"
126+
assert $ replace "b" "!" "abc" == "a!c"
127+
assert $ replace "d" "!" "abc" == "abc"
128+
129+
log "take"
130+
assert $ take 0 "ab" == ""
131+
assert $ take 1 "ab" == "a"
132+
assert $ take 2 "ab" == "ab"
133+
assert $ take 3 "ab" == "ab"
134+
135+
log "drop"
136+
assert $ drop 0 "ab" == "ab"
137+
assert $ drop 1 "ab" == "b"
138+
assert $ drop 2 "ab" == ""
139+
assert $ drop 3 "ab" == ""
140+
141+
log "count"
142+
assert $ count (\c -> true) "" == 0
143+
assert $ count (\c -> true) "ab" == 2
144+
assert $ count (\c -> false) "ab" == 0
145+
assert $ count (\c -> c == 'a') "aabbcc" == 2
146+
assert $ count (\c -> c == 'b') "aabbcc" == 0
147+
assert $ count (\c -> c /= 'a') "aabbcc" == 0
148+
assert $ count (\c -> c /= 'b') "aabbcc" == 2
149+
150+
log "split"
151+
assert $ split "" "" == []
152+
assert $ split "" "a" == ["a"]
153+
assert $ split "" "ab" == ["a", "b"]
154+
assert $ split "b" "aabcc" == ["aa", "cc"]
155+
assert $ split "d" "abc" == ["abc"]
156+
157+
log "toCharArray"
158+
assert $ toCharArray "" == []
159+
assert $ toCharArray "a" == ['a']
160+
assert $ toCharArray "ab" == ['a', 'b']
161+
162+
log "toLower"
163+
assert $ toLower "bAtMaN" == "batman"
164+
165+
log "toUpper"
166+
assert $ toUpper "bAtMaN" == "BATMAN"
167+
168+
log "trim"
169+
assert $ trim " abc " == "abc"
170+
171+
log "joinWith"
172+
assert $ joinWith "" [] == ""
173+
assert $ joinWith "" ["a", "b"] == "ab"
174+
assert $ joinWith "--" ["a", "b", "c"] == "a--b--c"
175+

0 commit comments

Comments
 (0)