Skip to content

Commit 9112112

Browse files
committed
wrap in maybe
1 parent f152cc5 commit 9112112

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Data/String.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,15 @@ exports.drop = function (n) {
131131
};
132132
};
133133

134-
exports.slice = function (b) {
135-
return function (e) {
136-
return function (s) {
137-
return s.slice(b, e);
134+
exports._slice = function (just) {
135+
return function (nothing) {
136+
return function (b) {
137+
return function (e) {
138+
return function (s) {
139+
var res = s.slice(b, e);
140+
return (res.length == 0 ? nothing : just(res));
141+
};
142+
};
138143
};
139144
};
140145
};

src/Data/String.purs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,24 @@ takeWhile p s = take (count p s) s
173173
dropWhile :: (Char -> Boolean) -> String -> String
174174
dropWhile p s = drop (count p s) s
175175

176-
-- | Returns the substring at indices [begin, end).
176+
-- | Returns the substring at indices `[begin, end)`.
177177
-- | If either index is negative, it is normalised to `length s - index`,
178-
-- | where `s` is the input string. An empty string is returned if either
178+
-- | where `s` is the input string. `Nothing` is returned if either
179179
-- | index is out of bounds or if `begin > end` after normalisation.
180180
-- |
181181
-- | ```purescript
182-
-- | slice 0 1 "purescript" == "p"
183-
-- | slice 3 6 "purescript" == "ecr"
184-
-- | slice -4 -1 "purescript" == "rip"
182+
-- | slice 0 1 "purescript" == Just "p"
183+
-- | slice 3 6 "purescript" == Just "ecr"
184+
-- | slice -4 -1 "purescript" == Just "rip"
185+
-- | slice -4 3 "purescript" == Nothing
185186
-- | ```
186-
foreign import slice :: Int -> Int -> String -> String
187+
slice :: Int -> Int -> String -> Maybe String
188+
slice = _slice Just Nothing
189+
190+
foreign import _slice
191+
:: (forall a. a -> Maybe a)
192+
-> (forall a. Maybe a)
193+
-> Int -> Int -> String -> Maybe String
187194

188195
-- | If the string starts with the given prefix, return the portion of the
189196
-- | string left after removing it, as a Just value. Otherwise, return Nothing.

0 commit comments

Comments
 (0)