Skip to content

Commit 58e66ed

Browse files
committed
conversions between NonEmptyArray/NonEmptyString
addresses purescript#101 This uses unsafePartial for the sake of efficiency.
1 parent 6fce657 commit 58e66ed

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"purescript-maybe": "^3.0.0",
2323
"purescript-partial": "^1.2.0",
2424
"purescript-unfoldable": "^3.0.0",
25-
"purescript-arrays": "^4.0.1",
25+
"purescript-arrays": "^4.3.0",
2626
"purescript-integers": "^3.2.0"
2727
},
2828
"devDependencies": {

src/Data/String/NonEmpty.purs

+12
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ module Data.String.NonEmpty
1010
, fromString
1111
, unsafeFromString
1212
, fromCharArray
13+
, fromNonEmptyCharArray
1314
, singleton
1415
, cons
1516
, snoc
1617
, fromFoldable1
1718
, toString
1819
, toCharArray
20+
, toNonEmptyCharArray
1921
, charAt
2022
, charCodeAt
2123
, toChar
@@ -52,6 +54,8 @@ module Data.String.NonEmpty
5254

5355
import Prelude
5456

57+
import Data.Array.NonEmpty (NonEmptyArray)
58+
import Data.Array.NonEmpty as NEA
5559
import Data.Foldable (class Foldable)
5660
import Data.Foldable as F
5761
import Data.Maybe (Maybe(..), fromJust)
@@ -60,6 +64,7 @@ import Data.Semigroup.Foldable as F1
6064
import Data.String (Pattern(..))
6165
import Data.String as String
6266
import Data.String.Unsafe as U
67+
import Partial.Unsafe (unsafePartial)
6368
import Unsafe.Coerce (unsafeCoerce)
6469

6570
-- | A string that is known not to be empty.
@@ -110,6 +115,9 @@ fromCharArray = case _ of
110115
[] -> Nothing
111116
cs -> Just (NonEmptyString (String.fromCharArray cs))
112117

118+
fromNonEmptyCharArray :: NonEmptyArray Char -> NonEmptyString
119+
fromNonEmptyCharArray = unsafePartial fromJust <<< fromCharArray <<< NEA.toArray
120+
113121
-- | Creates a `NonEmptyString` from a character.
114122
singleton :: Char -> NonEmptyString
115123
singleton = NonEmptyString <<< String.singleton
@@ -181,6 +189,10 @@ toChar (NonEmptyString s) = String.toChar s
181189
toCharArray :: NonEmptyString -> Array Char
182190
toCharArray (NonEmptyString s) = String.toCharArray s
183191

192+
-- | Converts the `NonEmptyString` into a non-empty array of characters.
193+
toNonEmptyCharArray :: NonEmptyString -> NonEmptyArray Char
194+
toNonEmptyCharArray = unsafePartial fromJust <<< NEA.fromArray <<< toCharArray
195+
184196
-- | Appends a string to this non-empty string. Since one of the strings is
185197
-- | non-empty we know the result will be too.
186198
-- |

test/Test/Data/String/NonEmpty.purs

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Data.String.NonEmpty
44

55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
7+
import Data.Array.NonEmpty as NEA
78
import Data.Array.Partial as AP
89
import Data.Foldable (class Foldable, foldl)
910
import Data.Maybe (Maybe(..), fromJust, isNothing, maybe)
@@ -22,6 +23,9 @@ testNonEmptyString = do
2223
assert $ fromCharArray [] == Nothing
2324
assert $ fromCharArray ['a', 'b'] == Just (nes "ab")
2425

26+
log "fromNonEmptyCharArray"
27+
assert $ fromNonEmptyCharArray (NEA.singleton 'b') == singleton 'b'
28+
2529
log "singleton"
2630
assert $ singleton 'a' == nes "a"
2731

@@ -64,6 +68,10 @@ testNonEmptyString = do
6468
assert $ toCharArray (nes "ab") == ['a', 'b']
6569
assert $ toCharArray (nes "Hello☺\n") == ['H','e','l','l','o','☺','\n']
6670

71+
log "toNonEmptyCharArray"
72+
assert $ toNonEmptyCharArray (nes "ab")
73+
== unsafePartial fromJust (NEA.fromArray ['a', 'b'])
74+
6775
log "appendString"
6876
assert $ appendString (nes "Hello") " world" == nes "Hello world"
6977
assert $ appendString (nes "Hello") "" == nes "Hello"

0 commit comments

Comments
 (0)