diff options
author | Rocky Madden <git@rockymadden.com> | 2012-10-20 15:54:58 -0600 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-10-20 15:54:58 -0600 |
commit | 8e5953adf334c9eb4d288b40e5f46a7e710131b2 (patch) | |
tree | 1a69b6a37447660a4b9eec89c7500a6070faced1 | |
parent | 89d8b433959af9d5ea8c8a9379c4c7a8d7b0dd47 (diff) | |
download | stringmetric-8e5953adf334c9eb4d288b40e5f46a7e710131b2.tar.gz stringmetric-8e5953adf334c9eb4d288b40e5f46a7e710131b2.tar.bz2 stringmetric-8e5953adf334c9eb4d288b40e5f46a7e710131b2.zip |
Created AsciiLetterOnlyStringCleaner and spec.
-rwxr-xr-x | core/source/core/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleaner.scala | 14 | ||||
-rwxr-xr-x | core/source/test/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleanerSpec.scala | 26 |
2 files changed, 40 insertions, 0 deletions
diff --git a/core/source/core/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleaner.scala b/core/source/core/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleaner.scala new file mode 100755 index 0000000..5a93b30 --- /dev/null +++ b/core/source/core/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleaner.scala @@ -0,0 +1,14 @@ +package org.hashtree.stringmetric + +/** A decorator [[org.hashtree.stringmetric.StringCleaner]]. Ensures only ASCII letters matter. */ +trait AsciiLetterOnlyStringCleaner extends StringCleaner { + abstract override def clean(charArray: Array[Char]): Array[Char] = { + super.clean( + charArray.filter { c => + (c >= 65 && c <= 90 ) || (c >= 97 && c <= 122) + } + ) + } + + abstract override def clean(string: String): String = clean(string.toCharArray).mkString +}
\ No newline at end of file diff --git a/core/source/test/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleanerSpec.scala b/core/source/test/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleanerSpec.scala new file mode 100755 index 0000000..99ed40d --- /dev/null +++ b/core/source/test/scala/org/hashtree/stringmetric/AsciiLetterOnlyStringCleanerSpec.scala @@ -0,0 +1,26 @@ +package org.hashtree.stringmetric + +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class AsciiLetterOnlyStringCleanerSpec extends ScalaTest { + private final val Cleaner = new StringCleanerDelegate with AsciiLetterOnlyStringCleaner + + "AsciiLetterOnlyStringCleaner" should provide { + "overloaded clean method" when passed { + "String with mixed characters" should returns { + "String with non-letters removed" in { + Cleaner.clean("!@#$%^&*()abc") should equal ("abc") + Cleaner.clean("!@#$%^&*()abc123") should equal ("abc") + } + } + "character array with mixed characters" should returns { + "character array with non-letters removed" in { + Cleaner.clean("!@#$%^&*()abc".toCharArray) should equal ("abc".toCharArray) + Cleaner.clean("!@#$%^&*()abc123".toCharArray) should equal ("abc".toCharArray) + } + } + } + } +}
\ No newline at end of file |