Skip to content

[BREAKING] Make splitAt return a record, fixes #69 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2017
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
3 changes: 2 additions & 1 deletion src/Data/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ exports._splitAt = function (just) {
return function (i) {
return function (s) {
return i >= 0 && i < s.length ?
just([s.substring(0, i), s.substring(i)]) : nothing;
just({ before: s.substring(0, i), after: s.substring(i) }) :
nothing;
};
};
};
Expand Down
4 changes: 2 additions & 2 deletions src/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ foreign import count :: (Char -> Boolean) -> String -> Int
foreign import split :: Pattern -> 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 :: Int -> String -> Maybe { before :: String, after :: String }
splitAt = _splitAt Just Nothing

foreign import _splitAt :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Int
-> String
-> Maybe (Array String)
-> Maybe { before :: String, after :: String }

-- | Converts the string into an array of characters.
foreign import toCharArray :: String -> Array Char
Expand Down
20 changes: 14 additions & 6 deletions test/Test/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prelude (Unit, Ordering(..), (==), ($), bind, negate, not, (/=), (&&))
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Maybe (Maybe(..), isNothing)
import Data.Maybe (Maybe(..), isNothing, maybe)
import Data.String

import Test.Assert (ASSERT, assert)
Expand Down Expand Up @@ -160,11 +160,19 @@ testString = do
assert $ split (Pattern "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
let testSplitAt i str res =
assert $ case splitAt i str of
Nothing ->
isNothing res
Just { before, after } ->
maybe false (\r ->
r.before == before && r.after == after) res

testSplitAt 1 "" Nothing
testSplitAt 0 "a" $ Just {before: "", after: "a"}
testSplitAt 1 "ab" $ Just {before: "a", after: "b"}
testSplitAt 3 "aabcc" $ Just {before: "aab", after: "cc"}
testSplitAt (-1) "abc" $ Nothing

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