Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 4 additions & 5 deletions src/Data/String/NonEmpty/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@ import Data.Semigroup.Foldable (class Foldable1)
import Data.Semigroup.Foldable as F1
import Data.String.CodePoints (CodePoint)
import Data.String.CodePoints as CP
import Data.String.NonEmpty.Internal (NonEmptyString, fromString)
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
import Data.String.Pattern (Pattern)
import Partial.Unsafe (unsafePartial)
import Unsafe.Coerce (unsafeCoerce)

toNonEmptyString :: String -> NonEmptyString
toNonEmptyString = unsafeCoerce
toNonEmptyString = NonEmptyString

fromNonEmptyString :: NonEmptyString -> String
fromNonEmptyString = unsafeCoerce
fromNonEmptyString (NonEmptyString s) = s

liftS :: forall r. (String -> r) -> NonEmptyString -> r
liftS = unsafeCoerce
liftS f (NonEmptyString s) = f s

fromCodePointArray :: Array CodePoint -> Maybe NonEmptyString
fromCodePointArray = case _ of
Expand Down
8 changes: 4 additions & 4 deletions src/Data/String/NonEmpty/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ import Data.Maybe (Maybe(..), fromJust)
import Data.Semigroup.Foldable (class Foldable1)
import Data.Semigroup.Foldable as F1
import Data.String.CodeUnits as CU
import Data.String.NonEmpty.Internal (NonEmptyString, fromString)
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
import Data.String.Pattern (Pattern)
import Data.String.Unsafe as U
import Partial.Unsafe (unsafePartial)
import Unsafe.Coerce (unsafeCoerce)

toNonEmptyString :: String -> NonEmptyString
toNonEmptyString = unsafeCoerce
toNonEmptyString = NonEmptyString

fromNonEmptyString :: NonEmptyString -> String
fromNonEmptyString = unsafeCoerce
fromNonEmptyString (NonEmptyString s) = s

liftS :: forall r. (String -> r) -> NonEmptyString -> r
liftS = unsafeCoerce
liftS f (NonEmptyString s) = f s

-- | Creates a `NonEmptyString` from a character array `String`, returning
-- | `Nothing` if the input is empty.
Expand Down
12 changes: 12 additions & 0 deletions src/Data/String/NonEmpty/Internal.purs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-- | While most of the code in this module is safe, this module does
-- | export a few partial functions and the `NonEmptyString` constructor.
-- | While the partial functions are obvious from the `Partial` constraint in
-- | their type signature, the `NonEmptyString` constructor can be overlooked
-- | when searching for issues in one's code. See the constructor's
-- | documentation for more information.
module Data.String.NonEmpty.Internal where

import Prelude
Expand All @@ -13,6 +19,12 @@ import Prim.TypeError as TE
import Unsafe.Coerce (unsafeCoerce)

-- | A string that is known not to be empty.
-- |
-- | You can use this constructor to create a `NonEmptyString` that isn't
-- | non-empty, breaking the guarantee behind this newtype. It is
-- | provided as an escape hatch mainly for the `Data.NonEmpty.CodeUnits`
-- | and `Data.NonEmpty.CodePoints` modules. Use this at your own risk
-- | when you know what you are doing.
newtype NonEmptyString = NonEmptyString String

derive newtype instance eqNonEmptyString ∷ Eq NonEmptyString
Expand Down