Skip to content
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

Add a TrimmedString.trim method #487

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,32 @@ object string {

object NonEmptyString extends RefinedTypeOps[NonEmptyString, String]

/** A `String` that contains no leading or trailing whitespace. */
type TrimmedString = String Refined MatchesRegex[W.`"""^(?!\\s).*(?<!\\s)"""`.T]

object TrimmedString extends RefinedTypeOps[TrimmedString, String]
// scalastyle:off no.whitespace.after.left.bracket
/**
* A `String` that contains no leading or trailing whitespace.
*
* Note that a line separator (`\u2028') is not considered whitespace for the
* purposes of trimming.
*/
type TrimmedString = String Refined MatchesRegex[
W.`"""^(?![\\x{0000}-\\x{0020}])(?s:.*)(?<![\\x{0000}-\\x{0020}])"""`.T]
// scalastyle:on no.whitespace.after.left.bracket

object TrimmedString extends RefinedTypeOps[TrimmedString, String] {

/**
* Trim a string into a TrimmedString by removing leading and trailing
* whitespace.
*
* Example: {{{
* scala> import eu.timepit.refined.types.string._
*
* scala> TrimmedString.trim(" \n a b c ")
* res0: TrimmedString = a b c
* }}}
*/
def trim(s: String): TrimmedString = Refined.unsafeApply(s.trim)
}

/** A `String` representing a hexadecimal number */
type HexStringSpec = MatchesRegex[W.`"""^(([0-9a-f]+)|([0-9A-F]+))$"""`.T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ class StringTypesSpec extends Properties("StringTypes") {
(truncated.value ?= str.take(FString3.maxLength))
}

property("""TrimmedString.from(str)""") = forAll { (str: String) =>
TrimmedString.from(str).isRight ?= (TrimmedString.trim(str).value == str)
}

property("""TrimmedString.trim(str)""") = forAll { (str: String) =>
val trimmed = TrimmedString.trim(str)
TrimmedString.from(trimmed.value) ?= Right(trimmed)
}

// Hashes for ""
object EmptyString {
val md5 = "d41d8cd98f00b204e9800998ecf8427e"
Expand Down