Skip to content

Commit 58d9ec2

Browse files
committed
Add startsWith and endsWith
1 parent 157e372 commit 58d9ec2

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/Data/String/CodeUnits.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,15 @@ exports.splitAt = function (i) {
116116
return { before: s.substring(0, i), after: s.substring(i) };
117117
};
118118
};
119+
120+
exports.startsWith = function (pattern) {
121+
return function (s) {
122+
return s.startsWith(pattern);
123+
}
124+
}
125+
126+
exports.endsWith = function (pattern) {
127+
return function (s) {
128+
return s.endsWith(pattern);
129+
}
130+
}

src/Data/String/CodeUnits.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module Data.String.CodeUnits
2222
, dropWhile
2323
, slice
2424
, splitAt
25+
, startsWith
26+
, endsWith
2527
) where
2628

2729
import Prelude
@@ -343,3 +345,19 @@ foreign import _slice :: Int -> Int -> String -> String
343345
-- | splitAt i s == {before: take i s, after: drop i s}
344346
-- | ```
345347
foreign import splitAt :: Int -> String -> { before :: String, after :: String }
348+
349+
-- | Checks whether the given string starts with the pattern.
350+
-- |
351+
-- | ```purescript
352+
-- | startsWith (Pattern "foo") "foobar" == true
353+
-- | startsWith (Pattern "bar") "foobar" == false
354+
-- | ```
355+
foreign import startsWith :: Pattern -> String -> Boolean
356+
357+
-- | Checks whether the given string ends with the pattern.
358+
-- |
359+
-- | ```purescript
360+
-- | endsWith (Pattern "bar") "foobar" == true
361+
-- | endsWith (Pattern "foo") "foobar" == false
362+
-- | ```
363+
foreign import endsWith :: Pattern -> String -> Boolean

test/Test/Data/String/CodeUnits.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,17 @@ testStringCodeUnits = do
510510
{ actual: SCU.slice 3 1000 "purescript"
511511
, expected: Nothing -- e > l
512512
}
513+
514+
log "startsWith"
515+
assert $ SCU.startsWith (Pattern "foo") "foobar"
516+
assert $ SCU.startsWith (Pattern "foo") "foo"
517+
assert $ SCU.startsWith (Pattern "") ""
518+
assert $ SCU.startsWith (Pattern "") "foo"
519+
assert $ not $ SCU.startsWith (Pattern "foo") ""
520+
521+
log "endsWith"
522+
assert $ SCU.endsWith (Pattern "bar") "foobar"
523+
assert $ SCU.endsWith (Pattern "bar") "bar"
524+
assert $ SCU.endsWith (Pattern "") ""
525+
assert $ SCU.endsWith (Pattern "") "bar"
526+
assert $ not $ SCU.endsWith (Pattern "bar") ""

0 commit comments

Comments
 (0)