Skip to content

feat: nes -> use VTA instead of Proxy [BREAKING CHANGE] #175

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 1 commit 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
10 changes: 5 additions & 5 deletions src/Data/String/NonEmpty/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Data.String as String
import Data.String.Pattern (Pattern)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Prim.TypeError as TE
import Type.Proxy (Proxy)
import Type.Proxy (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)

-- | A string that is known not to be empty.
Expand All @@ -39,16 +39,16 @@ instance showNonEmptyString :: Show NonEmptyString where
-- |
-- | ``` purescript
-- | something :: NonEmptyString
-- | something = nes (Proxy :: Proxy "something")
-- | something = nes @"something"
-- | ```
class MakeNonEmpty (s :: Symbol) where
nes :: Proxy s -> NonEmptyString
nes :: NonEmptyString

instance makeNonEmptyBad :: TE.Fail (TE.Text "Cannot create an NonEmptyString from an empty Symbol") => MakeNonEmpty "" where
nes _ = NonEmptyString ""
nes = NonEmptyString ""

else instance nonEmptyNonEmpty :: IsSymbol s => MakeNonEmpty s where
nes p = NonEmptyString (reflectSymbol p)
nes = NonEmptyString (reflectSymbol (Proxy :: Proxy s))

-- | A newtype used in cases to specify a non-empty replacement for a pattern.
newtype NonEmptyReplacement = NonEmptyReplacement NonEmptyString
Expand Down
137 changes: 68 additions & 69 deletions test/Test/Data/String/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Effect (Effect)
import Effect.Console (log)
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert, assertEqual)
import Type.Proxy (Proxy(..))

testNonEmptyString :: Effect Unit
testNonEmptyString = do
Expand All @@ -22,7 +21,7 @@ testNonEmptyString = do
}
assertEqual
{ actual: NES.fromString "hello"
, expected: Just (nes (Proxy :: Proxy "hello"))
, expected: Just (nes @"hello")
}

log "toString"
Expand All @@ -33,136 +32,136 @@ testNonEmptyString = do

log "appendString"
assertEqual
{ actual: NES.appendString (nes (Proxy :: Proxy "Hello")) " world"
, expected: nes (Proxy :: Proxy "Hello world")
{ actual: NES.appendString (nes @"Hello") " world"
, expected: nes @"Hello world"
}
assertEqual
{ actual: NES.appendString (nes (Proxy :: Proxy "Hello")) ""
, expected: nes (Proxy :: Proxy "Hello")
{ actual: NES.appendString (nes @"Hello") ""
, expected: nes @"Hello"
}

log "prependString"
assertEqual
{ actual: NES.prependString "be" (nes (Proxy :: Proxy "fore"))
, expected: nes (Proxy :: Proxy "before")
{ actual: NES.prependString "be" (nes @"fore")
, expected: nes @"before"
}
assertEqual
{ actual: NES.prependString "" (nes (Proxy :: Proxy "fore"))
, expected: nes (Proxy :: Proxy "fore")
{ actual: NES.prependString "" (nes @"fore")
, expected: nes @"fore"
}

log "contains"
assert $ NES.contains (Pattern "") (nes (Proxy :: Proxy "abcd"))
assert $ NES.contains (Pattern "bc") (nes (Proxy :: Proxy "abcd"))
assert $ not NES.contains (Pattern "cb") (nes (Proxy :: Proxy "abcd"))
assert $ NES.contains (Pattern "needle") (nes (Proxy :: Proxy "haystack with needle"))
assert $ not NES.contains (Pattern "needle") (nes (Proxy :: Proxy "haystack"))
assert $ NES.contains (Pattern "") (nes @"abcd")
assert $ NES.contains (Pattern "bc") (nes @"abcd")
assert $ not NES.contains (Pattern "cb") (nes @"abcd")
assert $ NES.contains (Pattern "needle") (nes @"haystack with needle")
assert $ not NES.contains (Pattern "needle") (nes @"haystack")

log "localeCompare"
assertEqual
{ actual: NES.localeCompare (nes (Proxy :: Proxy "a")) (nes (Proxy :: Proxy "a"))
{ actual: NES.localeCompare (nes @"a") (nes @"a")
, expected: EQ
}
assertEqual
{ actual: NES.localeCompare (nes (Proxy :: Proxy "a")) (nes (Proxy :: Proxy "b"))
{ actual: NES.localeCompare (nes @"a") (nes @"b")
, expected: LT
}
assertEqual
{ actual: NES.localeCompare (nes (Proxy :: Proxy "b")) (nes (Proxy :: Proxy "a"))
{ actual: NES.localeCompare (nes @"b") (nes @"a")
, expected: GT
}

log "replace"
assertEqual
{ actual: NES.replace (Pattern "b") (NES.NonEmptyReplacement (nes (Proxy :: Proxy "!"))) (nes (Proxy :: Proxy "abc"))
, expected: nes (Proxy :: Proxy "a!c")
{ actual: NES.replace (Pattern "b") (NES.NonEmptyReplacement (nes @"!")) (nes @"abc")
, expected: nes @"a!c"
}
assertEqual
{ actual: NES.replace (Pattern "b") (NES.NonEmptyReplacement (nes (Proxy :: Proxy "!"))) (nes (Proxy :: Proxy "abbc"))
, expected: nes (Proxy :: Proxy "a!bc")
{ actual: NES.replace (Pattern "b") (NES.NonEmptyReplacement (nes @"!")) (nes @"abbc")
, expected: nes @"a!bc"
}
assertEqual
{ actual: NES.replace (Pattern "d") (NES.NonEmptyReplacement (nes (Proxy :: Proxy "!"))) (nes (Proxy :: Proxy "abc"))
, expected: nes (Proxy :: Proxy "abc")
{ actual: NES.replace (Pattern "d") (NES.NonEmptyReplacement (nes @"!")) (nes @"abc")
, expected: nes @"abc"
}

log "replaceAll"
assertEqual
{ actual: NES.replaceAll (Pattern "[b]") (NES.NonEmptyReplacement (nes (Proxy :: Proxy "!"))) (nes (Proxy :: Proxy "a[b]c"))
, expected: nes (Proxy :: Proxy "a!c")
{ actual: NES.replaceAll (Pattern "[b]") (NES.NonEmptyReplacement (nes @"!")) (nes @"a[b]c")
, expected: nes @"a!c"
}
assertEqual
{ actual: NES.replaceAll (Pattern "[b]") (NES.NonEmptyReplacement (nes (Proxy :: Proxy "!"))) (nes (Proxy :: Proxy "a[b]c[b]"))
, expected: nes (Proxy :: Proxy "a!c!")
{ actual: NES.replaceAll (Pattern "[b]") (NES.NonEmptyReplacement (nes @"!")) (nes @"a[b]c[b]")
, expected: nes @"a!c!"
}
assertEqual
{ actual: NES.replaceAll (Pattern "x") (NES.NonEmptyReplacement (nes (Proxy :: Proxy "!"))) (nes (Proxy :: Proxy "abc"))
, expected: nes (Proxy :: Proxy "abc")
{ actual: NES.replaceAll (Pattern "x") (NES.NonEmptyReplacement (nes @"!")) (nes @"abc")
, expected: nes @"abc"
}

log "stripPrefix"
assertEqual
{ actual: NES.stripPrefix (Pattern "") (nes (Proxy :: Proxy "abc"))
, expected: Just (nes (Proxy :: Proxy "abc"))
{ actual: NES.stripPrefix (Pattern "") (nes @"abc")
, expected: Just (nes @"abc")
}
assertEqual
{ actual: NES.stripPrefix (Pattern "a") (nes (Proxy :: Proxy "abc"))
, expected: Just (nes (Proxy :: Proxy "bc"))
{ actual: NES.stripPrefix (Pattern "a") (nes @"abc")
, expected: Just (nes @"bc")
}
assertEqual
{ actual: NES.stripPrefix (Pattern "abc") (nes (Proxy :: Proxy "abc"))
{ actual: NES.stripPrefix (Pattern "abc") (nes @"abc")
, expected: Nothing
}
assertEqual
{ actual: NES.stripPrefix (Pattern "!") (nes (Proxy :: Proxy "abc"))
{ actual: NES.stripPrefix (Pattern "!") (nes @"abc")
, expected: Nothing
}
assertEqual
{ actual: NES.stripPrefix (Pattern "http:") (nes (Proxy :: Proxy "http://purescript.org"))
, expected: Just (nes (Proxy :: Proxy "//purescript.org"))
{ actual: NES.stripPrefix (Pattern "http:") (nes @"http://purescript.org")
, expected: Just (nes @"//purescript.org")
}
assertEqual
{ actual: NES.stripPrefix (Pattern "http:") (nes (Proxy :: Proxy "https://purescript.org"))
{ actual: NES.stripPrefix (Pattern "http:") (nes @"https://purescript.org")
, expected: Nothing
}
assertEqual
{ actual: NES.stripPrefix (Pattern "Hello!") (nes (Proxy :: Proxy "Hello!"))
{ actual: NES.stripPrefix (Pattern "Hello!") (nes @"Hello!")
, expected: Nothing
}

log "stripSuffix"
assertEqual
{ actual: NES.stripSuffix (Pattern ".exe") (nes (Proxy :: Proxy "purs.exe"))
, expected: Just (nes (Proxy :: Proxy "purs"))
{ actual: NES.stripSuffix (Pattern ".exe") (nes @"purs.exe")
, expected: Just (nes @"purs")
}
assertEqual
{ actual: NES.stripSuffix (Pattern ".exe") (nes (Proxy :: Proxy "purs"))
{ actual: NES.stripSuffix (Pattern ".exe") (nes @"purs")
, expected: Nothing
}
assertEqual
{ actual: NES.stripSuffix (Pattern "Hello!") (nes (Proxy :: Proxy "Hello!"))
{ actual: NES.stripSuffix (Pattern "Hello!") (nes @"Hello!")
, expected: Nothing
}

log "toLower"
assertEqual
{ actual: NES.toLower (nes (Proxy :: Proxy "bAtMaN"))
, expected: nes (Proxy :: Proxy "batman")
{ actual: NES.toLower (nes @"bAtMaN")
, expected: nes @"batman"
}

log "toUpper"
assertEqual
{ actual: NES.toUpper (nes (Proxy :: Proxy "bAtMaN"))
, expected: nes (Proxy :: Proxy "BATMAN")
{ actual: NES.toUpper (nes @"bAtMaN")
, expected: nes @"BATMAN"
}

log "trim"
assertEqual
{ actual: NES.trim (nes (Proxy :: Proxy " abc "))
, expected: Just (nes (Proxy :: Proxy "abc"))
{ actual: NES.trim (nes @" abc ")
, expected: Just (nes @"abc")
}
assertEqual
{ actual: NES.trim (nes (Proxy :: Proxy " \n"))
{ actual: NES.trim (nes @" \n")
, expected: Nothing
}

Expand All @@ -172,48 +171,48 @@ testNonEmptyString = do
, expected: ""
}
assertEqual
{ actual: NES.joinWith "" [nes (Proxy :: Proxy "a"), nes (Proxy :: Proxy "b")]
{ actual: NES.joinWith "" [nes @"a", nes @"b"]
, expected: "ab"
}
assertEqual
{ actual: NES.joinWith "--" [nes (Proxy :: Proxy "a"), nes (Proxy :: Proxy "b"), nes (Proxy :: Proxy "c")]
{ actual: NES.joinWith "--" [nes @"a", nes @"b", nes @"c"]
, expected: "a--b--c"
}

log "join1With"
assertEqual
{ actual: NES.join1With "" (nea [nes (Proxy :: Proxy "a"), nes (Proxy :: Proxy "b")])
, expected: nes (Proxy :: Proxy "ab")
{ actual: NES.join1With "" (nea [nes @"a", nes @"b"])
, expected: nes @"ab"
}
assertEqual
{ actual: NES.join1With "--" (nea [nes (Proxy :: Proxy "a"), nes (Proxy :: Proxy "b"), nes (Proxy :: Proxy "c")])
, expected: nes (Proxy :: Proxy "a--b--c")
{ actual: NES.join1With "--" (nea [nes @"a", nes @"b", nes @"c"])
, expected: nes @"a--b--c"
}
assertEqual
{ actual: NES.join1With ", " (nea [nes (Proxy :: Proxy "apple"), nes (Proxy :: Proxy "banana")])
, expected: nes (Proxy :: Proxy "apple, banana")
{ actual: NES.join1With ", " (nea [nes @"apple", nes @"banana"])
, expected: nes @"apple, banana"
}
assertEqual
{ actual: NES.join1With "" (nea [nes (Proxy :: Proxy "apple"), nes (Proxy :: Proxy "banana")])
, expected: nes (Proxy :: Proxy "applebanana")
{ actual: NES.join1With "" (nea [nes @"apple", nes @"banana"])
, expected: nes @"applebanana"
}

log "joinWith1"
assertEqual
{ actual: NES.joinWith1 (nes (Proxy :: Proxy " ")) (nea ["a", "b"])
, expected: nes (Proxy :: Proxy "a b")
{ actual: NES.joinWith1 (nes @" ") (nea ["a", "b"])
, expected: nes @"a b"
}
assertEqual
{ actual: NES.joinWith1 (nes (Proxy :: Proxy "--")) (nea ["a", "b", "c"])
, expected: nes (Proxy :: Proxy "a--b--c")
{ actual: NES.joinWith1 (nes @"--") (nea ["a", "b", "c"])
, expected: nes @"a--b--c"
}
assertEqual
{ actual: NES.joinWith1 (nes (Proxy :: Proxy ", ")) (nea ["apple", "banana"])
, expected: nes (Proxy :: Proxy "apple, banana")
{ actual: NES.joinWith1 (nes @", ") (nea ["apple", "banana"])
, expected: nes @"apple, banana"
}
assertEqual
{ actual: NES.joinWith1 (nes (Proxy :: Proxy "/")) (nea ["a", "b", "", "c", ""])
, expected: nes (Proxy :: Proxy "a/b//c/")
{ actual: NES.joinWith1 (nes @"/") (nea ["a", "b", "", "c", ""])
, expected: nes @"a/b//c/"
}

nea :: Array ~> NEA.NonEmptyArray
Expand Down
Loading