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
11 changes: 11 additions & 0 deletions src/Data/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ exports.split = function (sep) {
};
};

exports._splitAt = function (just) {
return function (nothing) {
return function (i) {
return function (s) {
return i >= 0 && i < s.length ?
just([s.substring(0, i), s.substring(i)]) : nothing;
};
};
};
};

exports.toCharArray = function (s) {
return s.split("");
};
Expand Down
11 changes: 11 additions & 0 deletions src/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Data.String
, stripSuffix
, count
, split
, splitAt
, toCharArray
, toLower
, toUpper
Expand Down Expand Up @@ -197,6 +198,16 @@ foreign import count :: (Char -> Boolean) -> String -> Int
-- | * `split " " "hello world" == ["hello", "world"]`
foreign import split :: String -> String -> Array String

-- | Returns the substrings of split at the given index, if the index is within bounds.
splitAt :: Int -> String -> Maybe (Array String)
splitAt = _splitAt Just Nothing

foreign import _splitAt :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Int
-> String
-> Maybe (Array String)

-- | Converts the string into an array of characters.
foreign import toCharArray :: String -> Array Char

Expand Down
7 changes: 7 additions & 0 deletions test/Test/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ testString = do
assert $ split "b" "aabcc" == ["aa", "cc"]
assert $ split "d" "abc" == ["abc"]

log "splitAt"
assert $ splitAt 1 "" == Nothing
assert $ splitAt 0 "a" == Just ["", "a"]
assert $ splitAt 1 "ab" == Just ["a", "b"]
assert $ splitAt 3 "aabcc" == Just ["aab", "cc"]
assert $ splitAt (-1) "abc" == Nothing

log "toCharArray"
assert $ toCharArray "" == []
assert $ toCharArray "a" == ['a']
Expand Down