Skip to content

Commit 8fd322a

Browse files
authored
Merge pull request #72 from purescript/splitAt-record
[BREAKING] Make splitAt return a record, fixes #69
2 parents ed00f29 + f7f449c commit 8fd322a

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/Data/String.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ exports._splitAt = function (just) {
149149
return function (i) {
150150
return function (s) {
151151
return i >= 0 && i < s.length ?
152-
just([s.substring(0, i), s.substring(i)]) : nothing;
152+
just({ before: s.substring(0, i), after: s.substring(i) }) :
153+
nothing;
153154
};
154155
};
155156
};

src/Data/String.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ foreign import count :: (Char -> Boolean) -> String -> Int
232232
foreign import split :: Pattern -> String -> Array String
233233

234234
-- | Returns the substrings of split at the given index, if the index is within bounds.
235-
splitAt :: Int -> String -> Maybe (Array String)
235+
splitAt :: Int -> String -> Maybe { before :: String, after :: String }
236236
splitAt = _splitAt Just Nothing
237237

238238
foreign import _splitAt :: (forall a. a -> Maybe a)
239239
-> (forall a. Maybe a)
240240
-> Int
241241
-> String
242-
-> Maybe (Array String)
242+
-> Maybe { before :: String, after :: String }
243243

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

test/Test/Data/String.purs

+14-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prelude (Unit, Ordering(..), (==), ($), bind, negate, not, (/=), (&&))
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8-
import Data.Maybe (Maybe(..), isNothing)
8+
import Data.Maybe (Maybe(..), isNothing, maybe)
99
import Data.String
1010

1111
import Test.Assert (ASSERT, assert)
@@ -160,11 +160,19 @@ testString = do
160160
assert $ split (Pattern "d") "abc" == ["abc"]
161161

162162
log "splitAt"
163-
assert $ splitAt 1 "" == Nothing
164-
assert $ splitAt 0 "a" == Just ["", "a"]
165-
assert $ splitAt 1 "ab" == Just ["a", "b"]
166-
assert $ splitAt 3 "aabcc" == Just ["aab", "cc"]
167-
assert $ splitAt (-1) "abc" == Nothing
163+
let testSplitAt i str res =
164+
assert $ case splitAt i str of
165+
Nothing ->
166+
isNothing res
167+
Just { before, after } ->
168+
maybe false (\r ->
169+
r.before == before && r.after == after) res
170+
171+
testSplitAt 1 "" Nothing
172+
testSplitAt 0 "a" $ Just {before: "", after: "a"}
173+
testSplitAt 1 "ab" $ Just {before: "a", after: "b"}
174+
testSplitAt 3 "aabcc" $ Just {before: "aab", after: "cc"}
175+
testSplitAt (-1) "abc" $ Nothing
168176

169177
log "toCharArray"
170178
assert $ toCharArray "" == []

0 commit comments

Comments
 (0)