Skip to content

Add startsWith and endsWith #147

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Added `startsWith` and `endsWith` (#147)

Bugfixes:

Expand Down
2 changes: 1 addition & 1 deletion src/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Data.Array as Array
import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), defaultPred, defaultSucc, fromEnum, toEnum, toEnumWithDefaults)
import Data.Int (hexadecimal, toStringAs)
import Data.Maybe (Maybe(..))
import Data.String.CodeUnits (contains, stripPrefix, stripSuffix) as Exports
import Data.String.CodeUnits (contains, stripPrefix, stripSuffix, startsWith, endsWith) as Exports
import Data.String.CodeUnits as CU
import Data.String.Common (toUpper)
import Data.String.Pattern (Pattern)
Expand Down
33 changes: 30 additions & 3 deletions src/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module Data.String.CodeUnits
, dropWhile
, slice
, splitAt
, startsWith
, endsWith
) where

import Prelude
Expand All @@ -31,9 +33,10 @@ import Data.String.Pattern (Pattern(..))
import Data.String.Unsafe as U

-------------------------------------------------------------------------------
-- `stripPrefix`, `stripSuffix`, and `contains` are CodeUnit/CodePoint agnostic
-- as they are based on patterns rather than lengths/indices, but they need to
-- be defined in here to avoid a circular module dependency
-- `stripPrefix`, `stripSuffix`, `startsWith`, `endsWith`, and `contains` are
-- CodeUnit/CodePoint agnostic as they are based on patterns rather than
-- lengths/indices, but they need to be defined in here to avoid a circular
-- module dependency
-------------------------------------------------------------------------------

-- | If the string starts with the given prefix, return the portion of the
Expand Down Expand Up @@ -61,6 +64,30 @@ stripSuffix (Pattern suffix) str =
let { before, after } = splitAt (length str - length suffix) str in
if after == suffix then Just before else Nothing

-- | Checks whether the given string starts with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripPrefix`.
-- |
-- | ```purescript
-- | startsWith (Pattern "foo") "foobar" == true
-- | startsWith (Pattern "bar") "foobar" == false
-- | ```
startsWith :: Pattern -> String -> Boolean
startsWith pat = isJust <<< stripPrefix pat

-- | Checks whether the given string ends with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripSuffix`.
-- |
-- | ```purescript
-- | endsWith (Pattern "bar") "foobar" == true
-- | endsWith (Pattern "foo") "foobar" == false
-- | ```
endsWith :: Pattern -> String -> Boolean
endsWith pat = isJust <<< stripSuffix pat

-- | Checks whether the pattern appears in the given string.
-- |
-- | ```purescript
Expand Down
2 changes: 1 addition & 1 deletion src/Data/String/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module Data.String.NonEmpty
, module Data.String.NonEmpty.CodePoints
) where

import Data.String.NonEmpty.Internal (NonEmptyString, class MakeNonEmpty, NonEmptyReplacement(..), appendString, contains, fromString, join1With, joinWith, joinWith1, localeCompare, nes, prependString, replace, replaceAll, stripPrefix, stripSuffix, toLower, toString, toUpper, trim, unsafeFromString)
import Data.String.NonEmpty.Internal (NonEmptyString, class MakeNonEmpty, NonEmptyReplacement(..), appendString, contains, fromString, join1With, joinWith, joinWith1, localeCompare, nes, prependString, replace, replaceAll, stripPrefix, stripSuffix, startsWith, endsWith, toLower, toString, toUpper, trim, unsafeFromString)
import Data.String.Pattern (Pattern(..))
import Data.String.NonEmpty.CodePoints
25 changes: 25 additions & 0 deletions src/Data/String/NonEmpty/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ stripPrefix pat = fromString <=< liftS (String.stripPrefix pat)
stripSuffix :: Pattern -> NonEmptyString -> Maybe NonEmptyString
stripSuffix pat = fromString <=< liftS (String.stripSuffix pat)


-- | Checks whether the given string starts with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripPrefix`.
-- |
-- | ```purescript
-- | startsWith (Pattern "foo") (NonEmptyString "foobar") == true
-- | startsWith (Pattern "bar") (NonEmptyString "foobar") == false
-- | ```
startsWith :: Pattern -> NonEmptyString -> Boolean
startsWith = liftS <<< String.startsWith

-- | Checks whether the given string ends with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripSuffix`.
-- |
-- | ```purescript
-- | endsWith (Pattern "bar") (NonEmptyString "foobar") == true
-- | endsWith (Pattern "foo") (NonEmptyString "foobar") == false
-- | ```
endsWith :: Pattern -> NonEmptyString -> Boolean
endsWith = liftS <<< String.endsWith

-- | Checks whether the pattern appears in the given string.
-- |
-- | ```purescript
Expand Down
14 changes: 14 additions & 0 deletions test/Test/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ testStringCodeUnits = do
, expected: Just ""
}

log "startsWith"
assert $ SCU.startsWith (Pattern "foo") "foobar"
assert $ SCU.startsWith (Pattern "foo") "foo"
assert $ SCU.startsWith (Pattern "") ""
assert $ SCU.startsWith (Pattern "") "foo"
assert $ not $ SCU.startsWith (Pattern "foo") ""

log "endsWith"
assert $ SCU.endsWith (Pattern "bar") "foobar"
assert $ SCU.endsWith (Pattern "bar") "bar"
assert $ SCU.endsWith (Pattern "") ""
assert $ SCU.endsWith (Pattern "") "bar"
assert $ not $ SCU.endsWith (Pattern "bar") ""

log "charAt"
assertEqual
{ actual: SCU.charAt 0 ""
Expand Down
13 changes: 13 additions & 0 deletions test/Test/Data/String/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ testNonEmptyString = do
, expected: Nothing
}

log "startsWith"
assert $ NES.startsWith (Pattern "foo") (nes (Proxy :: Proxy "foobar"))
assert $ NES.startsWith (Pattern "foo") (nes (Proxy :: Proxy "foo"))
assert $ NES.startsWith (Pattern "") (nes (Proxy :: Proxy "foo"))
assert $ not $ NES.startsWith (Pattern "foo") (nes (Proxy :: Proxy "f"))

log "endsWith"
assert $ NES.endsWith (Pattern "bar") (nes (Proxy :: Proxy "foobar"))
assert $ NES.endsWith (Pattern "bar") (nes (Proxy :: Proxy "bar"))
assert $ NES.endsWith (Pattern "") (nes (Proxy :: Proxy "f"))
assert $ NES.endsWith (Pattern "") (nes (Proxy :: Proxy "bar"))
assert $ not $ NES.endsWith (Pattern "bar") (nes (Proxy :: Proxy "b"))

log "toLower"
assertEqual
{ actual: NES.toLower (nes (Proxy :: Proxy "bAtMaN"))
Expand Down