@@ -33,9 +33,10 @@ import Data.String.Pattern (Pattern(..))
3333import Data.String.Unsafe as U
3434
3535-- -----------------------------------------------------------------------------
36- -- `stripPrefix`, `stripSuffix`, and `contains` are CodeUnit/CodePoint agnostic
37- -- as they are based on patterns rather than lengths/indices, but they need to
38- -- be defined in here to avoid a circular module dependency
36+ -- `stripPrefix`, `stripSuffix`, `startsWith`, `endsWith`, and `contains` are
37+ -- CodeUnit/CodePoint agnostic as they are based on patterns rather than
38+ -- lengths/indices, but they need to be defined in here to avoid a circular
39+ -- module dependency
3940-- -----------------------------------------------------------------------------
4041
4142-- | If the string starts with the given prefix, return the portion of the
@@ -63,6 +64,30 @@ stripSuffix (Pattern suffix) str =
6364 let { before, after } = splitAt (length str - length suffix) str in
6465 if after == suffix then Just before else Nothing
6566
67+ -- | Checks whether the given string starts with the pattern.
68+ -- |
69+ -- | **NOTE**: if you also want to get the string stripped of the pattern, see
70+ -- | `stripPrefix`.
71+ -- |
72+ -- | ```purescript
73+ -- | startsWith (Pattern "foo") "foobar" == true
74+ -- | startsWith (Pattern "bar") "foobar" == false
75+ -- | ```
76+ startsWith :: Pattern -> String -> Boolean
77+ startsWith pat = isJust <<< stripPrefix pat
78+
79+ -- | Checks whether the given string ends with the pattern.
80+ -- |
81+ -- | **NOTE**: if you also want to get the string stripped of the pattern, see
82+ -- | `stripSuffix`.
83+ -- |
84+ -- | ```purescript
85+ -- | endsWith (Pattern "bar") "foobar" == true
86+ -- | endsWith (Pattern "foo") "foobar" == false
87+ -- | ```
88+ endsWith :: Pattern -> String -> Boolean
89+ endsWith pat = isJust <<< stripSuffix pat
90+
6691-- | Checks whether the pattern appears in the given string.
6792-- |
6893-- | ```purescript
@@ -345,27 +370,3 @@ foreign import _slice :: Int -> Int -> String -> String
345370-- | splitAt i s == {before: take i s, after: drop i s}
346371-- | ```
347372foreign import splitAt :: Int -> String -> { before :: String , after :: String }
348-
349- -- | Checks whether the given string starts with the pattern.
350- -- |
351- -- | **NOTE**: if you also want to get the string stripped of the pattern, see
352- -- | `stripPrefix`.
353- -- |
354- -- | ```purescript
355- -- | startsWith (Pattern "foo") "foobar" == true
356- -- | startsWith (Pattern "bar") "foobar" == false
357- -- | ```
358- startsWith :: Pattern -> String -> Boolean
359- startsWith pat = isJust <<< stripPrefix pat
360-
361- -- | Checks whether the given string ends with the pattern.
362- -- |
363- -- | **NOTE**: if you also want to get the string stripped of the pattern, see
364- -- | `stripSuffix`.
365- -- |
366- -- | ```purescript
367- -- | endsWith (Pattern "bar") "foobar" == true
368- -- | endsWith (Pattern "foo") "foobar" == false
369- -- | ```
370- endsWith :: Pattern -> String -> Boolean
371- endsWith pat = isJust <<< stripSuffix pat
0 commit comments