Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
],
"dependencies": {
"purescript-maybe": "^0.3.0"
},
"devDependencies": {
"purescript-assert": "~0.1.0",
"purescript-console": "~0.1.0"
}
}
16 changes: 16 additions & 0 deletions docs/Data/Char.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,20 @@ fromCharCode :: Int -> Char

Constructs a character from the given Unicode numeric value.

#### `toLower`

``` purescript
toLower :: Char -> Char
```

Converts a character to lowercase.

#### `toUpper`

``` purescript
toUpper :: Char -> Char
```

Converts a character to uppercase.


5 changes: 3 additions & 2 deletions docs/Data/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ of the string for which the predicate holds.
split :: String -> String -> Array String
```

Returns the substrings of the first string separated along occurences
of the second string.
Returns the substrings of the second string separated along occurences
of the first string.
* `split " " "hello world" == ["hello", "world"]`

#### `toCharArray`

Expand Down
6 changes: 3 additions & 3 deletions docs/Data/String/Regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ See the [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
#### `search`

``` purescript
search :: Regex -> String -> Int
search :: Regex -> String -> Maybe Int
```

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

#### `split`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"scripts": {
"postinstall": "pulp dep install",
"build": "jshint src && jscs src && pulp build && rimraf docs && pulp docs"
"build": "jshint src && jscs src && pulp build && pulp test && rimraf docs && pulp docs"
},
"devDependencies": {
"jscs": "^1.13.1",
Expand Down
8 changes: 8 additions & 0 deletions src/Data/Char.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ exports.toCharCode = function (c) {
exports.fromCharCode = function (c) {
return String.fromCharCode(c);
};

exports.toLower = function (c) {
return c.toLowerCase();
};

exports.toUpper = function (c) {
return c.toUpperCase();
};
12 changes: 8 additions & 4 deletions src/Data/Char.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Data.Char
( toString
, fromCharCode
, toCharCode
, toLower
, toUpper
) where

import Prelude
Expand All @@ -16,11 +18,13 @@ foreign import toCharCode :: Char -> Int
-- | Constructs a character from the given Unicode numeric value.
foreign import fromCharCode :: Int -> Char

-- | Converts a character to lowercase.
foreign import toLower :: Char -> Char

-- | Converts a character to uppercase.
foreign import toUpper :: Char -> Char

-- | Characters fall within the Unicode range.
instance boundedChar :: Bounded Char where
top = fromCharCode zero
bottom = fromCharCode 65535

-- | Characters can be rendered as a string with `show`.
instance showChar :: Show Char where
show c = "Char " ++ show (toString c)
4 changes: 3 additions & 1 deletion src/Data/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ exports["_indexOf'"] = function (just) {
return function (x) {
return function (startAt) {
return function (s) {
if (startAt < 0 || startAt > s.length) return nothing;
var i = s.indexOf(x, startAt);
return i === -1 ? nothing : just(i);
};
Expand All @@ -75,6 +76,7 @@ exports["_lastIndexOf'"] = function (just) {
return function (x) {
return function (startAt) {
return function (s) {
if (startAt < 0 || startAt > s.length) return nothing;
var i = s.lastIndexOf(x, startAt);
return i === -1 ? nothing : just(i);
};
Expand All @@ -93,7 +95,7 @@ exports._localeCompare = function (lt) {
return function (s1) {
return function (s2) {
var result = s1.localeCompare(s2);
return result < 0 ? lt : result > 1 ? gt : eq;
return result < 0 ? lt : result > 0 ? gt : eq;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions src/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Data.String
) where

import Prelude
import Data.Char
import qualified Data.Char as C
import Data.Maybe (Maybe(..), isJust)
import Data.Monoid (Monoid)
import qualified Data.String.Unsafe as U
Expand All @@ -50,7 +50,7 @@ foreign import _charAt :: (forall a. a -> Maybe a)

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

-- | Returns a string of length `1` containing the given character.
-- | Same as `fromChar`.
Expand Down
11 changes: 8 additions & 3 deletions src/Data/String/Regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ exports["replace'"] = function (r) {
};
};

exports.search = function (r) {
return function (s) {
return s.search(r);
exports._search = function (just) {
return function (nothing) {
return function (r) {
return function (s) {
var result = s.search(r);
return result === -1 ? nothing : just(result);
};
};
};
};

Expand Down
13 changes: 10 additions & 3 deletions src/Data/String/Regex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ foreign import replace :: Regex -> String -> String -> String
-- | See the [reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter).
foreign import replace' :: Regex -> (String -> Array String -> String) -> String -> String

-- | Returns the index of the first match of the `Regex` in the string, or
-- | `-1` if there is no match.
foreign import search :: Regex -> String -> Int
foreign import _search :: (forall r. r -> Maybe r)
-> (forall r. Maybe r)
-> Regex
-> String
-> Maybe Int

-- | Returns `Just` the index of the first match of the `Regex` in the string,
-- | or `Nothing` if there is no match.
search :: Regex -> String -> Maybe Int
search = _search Just Nothing

-- | Split the string into an array of substrings along occurences of the `Regex`.
foreign import split :: Regex -> String -> Array String
27 changes: 27 additions & 0 deletions test/Test/Data/Char.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Test.Data.Char (testChar) where

import Prelude
import Control.Monad.Eff.Console (log)
import Data.Char
import Test.Assert (assert)

testChar = do
log "toString"
assert $ toString 'a' == "a"

log "toCharCode"
assert $ toCharCode 'a' == 97
assert $ toCharCode '\n' == 10

log "fromCharCode"
assert $ fromCharCode 97 == 'a'
assert $ fromCharCode 10 == '\n'

log "toLower"
assert $ toLower 'A' == 'a'
assert $ toLower 'a' == 'a'

log "toUpper"
assert $ toUpper 'a' == 'A'
assert $ toUpper 'A' == 'A'

175 changes: 175 additions & 0 deletions test/Test/Data/String.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
module Test.Data.String (testString) where

import Prelude
import Data.Maybe
import Control.Monad.Eff.Console (log)
import Data.String
import Test.Assert (assert)

testString = do
log "charAt"
assert $ charAt 0 "" == Nothing
assert $ charAt 0 "a" == Just 'a'
assert $ charAt 1 "a" == Nothing
assert $ charAt 0 "ab" == Just 'a'
assert $ charAt 1 "ab" == Just 'b'
assert $ charAt 2 "ab" == Nothing

log "fromChar"
assert $ fromChar 'a' == "a"

log "singleton"
assert $ singleton 'a' == "a"

log "charCodeAt"
assert $ charCodeAt 0 "" == Nothing
assert $ charCodeAt 0 "a" == Just 97
assert $ charCodeAt 1 "a" == Nothing
assert $ charCodeAt 0 "ab" == Just 97
assert $ charCodeAt 1 "ab" == Just 98
assert $ charCodeAt 2 "ab" == Nothing

log "toChar"
assert $ toChar "" == Nothing
assert $ toChar "a" == Just 'a'
assert $ toChar "ab" == Nothing

log "null"
assert $ null ""
assert $ not (null "a")

log "uncons"
assert $ isNothing (uncons "")
assert $ case uncons "a" of
Nothing -> false
Just m -> m.head == 'a' && m.tail == ""
assert $ case uncons "ab" of
Nothing -> false
Just m -> m.head == 'a' && m.tail == "b"

log "takeWhile"
assert $ takeWhile (\c -> true) "abc" == "abc"
assert $ takeWhile (\c -> false) "abc" == ""
assert $ takeWhile (\c -> c /= 'b') "aabbcc" == "aa"

log "dropWhile"
assert $ dropWhile (\c -> true) "abc" == ""
assert $ dropWhile (\c -> false) "abc" == "abc"
assert $ dropWhile (\c -> c /= 'b') "aabbcc" == "bbcc"

log "stripPrefix"
assert $ stripPrefix "" "" == Just ""
assert $ stripPrefix "" "abc" == Just "abc"
assert $ stripPrefix "a" "abc" == Just "bc"
assert $ stripPrefix "!" "abc" == Nothing
assert $ stripPrefix "!" "" == Nothing

log "fromCharArray"
assert $ fromCharArray [] == ""
assert $ fromCharArray ['a', 'b'] == "ab"

log "contains"
assert $ contains "" ""
assert $ contains "" "abcd"
assert $ contains "bc" "abcd"
assert $ not (contains "cb" "abcd")

log "indexOf"
assert $ indexOf "" "" == Just 0
assert $ indexOf "" "abcd" == Just 0
assert $ indexOf "bc" "abcd" == Just 1
assert $ indexOf "cb" "abcd" == Nothing

log "indexOf'"
assert $ indexOf' "" 0 "" == Just 0
assert $ indexOf' "" (-1) "ab" == Nothing
assert $ indexOf' "" 0 "ab" == Just 0
assert $ indexOf' "" 1 "ab" == Just 1
assert $ indexOf' "" 2 "ab" == Just 2
assert $ indexOf' "" 3 "ab" == Nothing
assert $ indexOf' "bc" 0 "abcd" == Just 1
assert $ indexOf' "bc" 1 "abcd" == Just 1
assert $ indexOf' "bc" 2 "abcd" == Nothing
assert $ indexOf' "cb" 0 "abcd" == Nothing

log "lastIndexOf"
assert $ lastIndexOf "" "" == Just 0
assert $ lastIndexOf "" "abcd" == Just 4
assert $ lastIndexOf "bc" "abcd" == Just 1
assert $ lastIndexOf "cb" "abcd" == Nothing

log "lastIndexOf'"
assert $ lastIndexOf' "" 0 "" == Just 0
assert $ lastIndexOf' "" (-1) "ab" == Nothing
assert $ lastIndexOf' "" 0 "ab" == Just 0
assert $ lastIndexOf' "" 1 "ab" == Just 1
assert $ lastIndexOf' "" 2 "ab" == Just 2
assert $ lastIndexOf' "" 3 "ab" == Nothing
assert $ lastIndexOf' "bc" 0 "abcd" == Nothing
assert $ lastIndexOf' "bc" 1 "abcd" == Just 1
assert $ lastIndexOf' "bc" 2 "abcd" == Just 1
assert $ lastIndexOf' "cb" 0 "abcd" == Nothing

log "length"
assert $ length "" == 0
assert $ length "a" == 1
assert $ length "ab" == 2

log "localeCompare"
assert $ localeCompare "" "" == EQ
assert $ localeCompare "a" "a" == EQ
assert $ localeCompare "a" "b" == LT
assert $ localeCompare "b" "a" == GT

log "replace"
assert $ replace "b" "" "abc" == "ac"
assert $ replace "b" "!" "abc" == "a!c"
assert $ replace "d" "!" "abc" == "abc"

log "take"
assert $ take 0 "ab" == ""
assert $ take 1 "ab" == "a"
assert $ take 2 "ab" == "ab"
assert $ take 3 "ab" == "ab"

log "drop"
assert $ drop 0 "ab" == "ab"
assert $ drop 1 "ab" == "b"
assert $ drop 2 "ab" == ""
assert $ drop 3 "ab" == ""

log "count"
assert $ count (\c -> true) "" == 0
assert $ count (\c -> true) "ab" == 2
assert $ count (\c -> false) "ab" == 0
assert $ count (\c -> c == 'a') "aabbcc" == 2
assert $ count (\c -> c == 'b') "aabbcc" == 0
assert $ count (\c -> c /= 'a') "aabbcc" == 0
assert $ count (\c -> c /= 'b') "aabbcc" == 2

log "split"
assert $ split "" "" == []
assert $ split "" "a" == ["a"]
assert $ split "" "ab" == ["a", "b"]
assert $ split "b" "aabcc" == ["aa", "cc"]
assert $ split "d" "abc" == ["abc"]

log "toCharArray"
assert $ toCharArray "" == []
assert $ toCharArray "a" == ['a']
assert $ toCharArray "ab" == ['a', 'b']

log "toLower"
assert $ toLower "bAtMaN" == "batman"

log "toUpper"
assert $ toUpper "bAtMaN" == "BATMAN"

log "trim"
assert $ trim " abc " == "abc"

log "joinWith"
assert $ joinWith "" [] == ""
assert $ joinWith "" ["a", "b"] == "ab"
assert $ joinWith "--" ["a", "b", "c"] == "a--b--c"

Loading