Skip to content

Commit d6adb7f

Browse files
committed
Add startsWith and endsWith for NonEmptyString
1 parent b9054ec commit d6adb7f

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/Data/String/CodePoints.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Data.Array as Array
3434
import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), defaultPred, defaultSucc, fromEnum, toEnum, toEnumWithDefaults)
3535
import Data.Int (hexadecimal, toStringAs)
3636
import Data.Maybe (Maybe(..))
37-
import Data.String.CodeUnits (contains, stripPrefix, stripSuffix) as Exports
37+
import Data.String.CodeUnits (contains, stripPrefix, stripSuffix, startsWith, endsWith) as Exports
3838
import Data.String.CodeUnits as CU
3939
import Data.String.Common (toUpper)
4040
import Data.String.Pattern (Pattern)

src/Data/String/NonEmpty.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module Data.String.NonEmpty
44
, module Data.String.NonEmpty.CodePoints
55
) where
66

7-
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)
7+
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)
88
import Data.String.Pattern (Pattern(..))
99
import Data.String.NonEmpty.CodePoints

src/Data/String/NonEmpty/Internal.purs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,31 @@ stripPrefix pat = fromString <=< liftS (String.stripPrefix pat)
123123
stripSuffix :: Pattern -> NonEmptyString -> Maybe NonEmptyString
124124
stripSuffix pat = fromString <=< liftS (String.stripSuffix pat)
125125

126+
127+
-- | Checks whether the given string starts with the pattern.
128+
-- |
129+
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
130+
-- | `stripPrefix`.
131+
-- |
132+
-- | ```purescript
133+
-- | startsWith (Pattern "foo") (NonEmptyString "foobar") == true
134+
-- | startsWith (Pattern "bar") (NonEmptyString "foobar") == false
135+
-- | ```
136+
startsWith :: Pattern -> NonEmptyString -> Boolean
137+
startsWith = liftS <<< String.startsWith
138+
139+
-- | Checks whether the given string ends with the pattern.
140+
-- |
141+
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
142+
-- | `stripSuffix`.
143+
-- |
144+
-- | ```purescript
145+
-- | endsWith (Pattern "bar") (NonEmptyString "foobar") == true
146+
-- | endsWith (Pattern "foo") (NonEmptyString "foobar") == false
147+
-- | ```
148+
endsWith :: Pattern -> NonEmptyString -> Boolean
149+
endsWith = liftS <<< String.endsWith
150+
126151
-- | Checks whether the pattern appears in the given string.
127152
-- |
128153
-- | ```purescript

test/Test/Data/String/NonEmpty.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ testNonEmptyString = do
144144
, expected: Nothing
145145
}
146146

147+
log "startsWith"
148+
assert $ NES.startsWith (Pattern "foo") (nes (Proxy :: Proxy "foobar"))
149+
assert $ NES.startsWith (Pattern "foo") (nes (Proxy :: Proxy "foo"))
150+
assert $ NES.startsWith (Pattern "") (nes (Proxy :: Proxy "foo"))
151+
assert $ not $ NES.startsWith (Pattern "foo") (nes (Proxy :: Proxy "f"))
152+
153+
log "endsWith"
154+
assert $ NES.endsWith (Pattern "bar") (nes (Proxy :: Proxy "foobar"))
155+
assert $ NES.endsWith (Pattern "bar") (nes (Proxy :: Proxy "bar"))
156+
assert $ NES.endsWith (Pattern "") (nes (Proxy :: Proxy "f"))
157+
assert $ NES.endsWith (Pattern "") (nes (Proxy :: Proxy "bar"))
158+
assert $ not $ NES.endsWith (Pattern "bar") (nes (Proxy :: Proxy "b"))
159+
147160
log "toLower"
148161
assertEqual
149162
{ actual: NES.toLower (nes (Proxy :: Proxy "bAtMaN"))

0 commit comments

Comments
 (0)