Skip to content

Commit 1523fc3

Browse files
committed
tests, changes, and fixes
Testing -- * Added `test/` directory. * Added `pulp test` to the `build` script. * New dev dependencies `purescript-assert` and `purescript-console` Breaking changes -- * `Data.String.Regex.search` now gives back `Maybe Int`. * `Nothing` for no match instead of `-1`. * `Data.Char.toUpperChar` is now `Data.Char.toUpper`. * `Data.Char.toLowerChar` is now `Data.Char.toLower`. Bug fixes -- * `Data.String.indexOf'` wrong for index out of bounds. * `Data.String.lastIndexOf'` wrong for index out of bounds. * `Data.String.localeCompare` off by 1. Notes -- The vast majority of functions have at least one test case. Lots of room for more though, especially in `Data.String.Regex`! Tests could also be added for checking that the unsafe functions throw in the documented ways.
1 parent ecc6ad6 commit 1523fc3

File tree

15 files changed

+305
-23
lines changed

15 files changed

+305
-23
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ fromCharCode :: Int -> Char
2626

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

29-
#### `toLowerChar`
29+
#### `toLower`
3030

3131
``` purescript
32-
toLowerChar :: Char -> Char
32+
toLower :: Char -> Char
3333
```
3434

3535
Converts a character to lowercase.
3636

37-
#### `toUpperChar`
37+
#### `toUpper`
3838

3939
``` purescript
40-
toUpperChar :: Char -> Char
40+
toUpper :: Char -> Char
4141
```
4242

4343
Converts a character to uppercase.

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ exports.fromCharCode = function (c) {
1515
return String.fromCharCode(c);
1616
};
1717

18-
exports.toLowerChar = function (c) {
18+
exports.toLower = function (c) {
1919
return c.toLowerCase();
2020
};
2121

22-
exports.toUpperChar = function (c) {
22+
exports.toUpper = function (c) {
2323
return c.toUpperCase();
2424
};

src/Data/Char.purs

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

1010
import Prelude
@@ -19,10 +19,10 @@ foreign import toCharCode :: Char -> Int
1919
foreign import fromCharCode :: Int -> Char
2020

2121
-- | Converts a character to lowercase.
22-
foreign import toLowerChar :: Char -> Char
22+
foreign import toLower :: Char -> Char
2323

2424
-- | Converts a character to uppercase.
25-
foreign import toUpperChar :: Char -> Char
25+
foreign import toUpper :: Char -> Char
2626

2727
-- | Characters fall within the Unicode range.
2828
instance boundedChar :: Bounded Char where

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
@@ -33,7 +33,7 @@ module Data.String
3333
) where
3434

3535
import Prelude
36-
import Data.Char
36+
import qualified Data.Char as C
3737
import Data.Maybe (Maybe(..), isJust)
3838
import Data.Monoid (Monoid)
3939
import qualified Data.String.Unsafe as U
@@ -50,7 +50,7 @@ foreign import _charAt :: (forall a. a -> Maybe a)
5050

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

5555
-- | Returns a string of length `1` containing the given character.
5656
-- | 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

0 commit comments

Comments
 (0)