Skip to content

Commit 48c7756

Browse files
committed
Add unsafeFromString partial function
1 parent 2b5cae7 commit 48c7756

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/Data/String/NonEmpty.purs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Data.String.NonEmpty
88
( NonEmptyString
99
, NonEmptyReplacement(..)
1010
, fromString
11+
, unsafeFromString
1112
, fromCharArray
1213
, singleton
1314
, cons
@@ -53,7 +54,7 @@ import Prelude
5354

5455
import Data.Foldable (class Foldable)
5556
import Data.Foldable as F
56-
import Data.Maybe (Maybe(..))
57+
import Data.Maybe (Maybe(..), fromJust)
5758
import Data.Semigroup.Foldable (class Foldable1)
5859
import Data.Semigroup.Foldable as F1
5960
import Data.String (Pattern(..))
@@ -69,7 +70,7 @@ derive newtype instance ordNonEmptyString ∷ Ord NonEmptyString
6970
derive newtype instance semigroupNonEmptyStringSemigroup NonEmptyString
7071

7172
instance showNonEmptyString :: Show NonEmptyString where
72-
show (NonEmptyString s) = "(NonEmptyString.fromString " <> show s <> ")"
73+
show (NonEmptyString s) = "(NonEmptyString.unsafeFromString " <> show s <> ")"
7374

7475
-- | A newtype used in cases to specify a non-empty replacement for a pattern.
7576
newtype NonEmptyReplacement = NonEmptyReplacement NonEmptyString
@@ -93,6 +94,10 @@ fromString = case _ of
9394
"" -> Nothing
9495
s -> Just (NonEmptyString s)
9596

97+
-- | A partial version of `fromString`.
98+
unsafeFromString :: Partial => String -> NonEmptyString
99+
unsafeFromString = fromJust <<< fromString
100+
96101
-- | Creates a `NonEmptyString` from a character array `String`, returning
97102
-- | `Nothing` if the input is empty.
98103
-- |

test/Test/Data/String/NonEmpty.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,8 @@ testNonEmptyString = do
240240
assert $ joinWith1 (nes ", ") (NEA ["apple", "banana"]) == nes "apple, banana"
241241
assert $ joinWith1 (nes "/") (NEA ["a", "b", "", "c", ""]) == nes "a/b//c/"
242242

243-
244243
nes :: String -> NonEmptyString
245-
nes s = unsafePartial (fromJust (fromString s))
244+
nes = unsafePartial unsafeFromString
246245

247246
newtype NEA a = NEA (Array a)
248247

0 commit comments

Comments
 (0)