diff options
author | Rocky Madden <git@rockymadden.com> | 2013-12-28 17:08:23 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-12-28 17:08:23 -0700 |
commit | 6148bc303b91ad20c3ba5902da25b462f3ce6df5 (patch) | |
tree | 43781a9a56290aae9c9fc3fa8cac4578cd3525d3 /core/source | |
parent | 6b2af5a3db2e8f4c485f77dbf09d2661d76d51e0 (diff) | |
download | stringmetric-6148bc303b91ad20c3ba5902da25b462f3ce6df5.tar.gz stringmetric-6148bc303b91ad20c3ba5902da25b462f3ce6df5.tar.bz2 stringmetric-6148bc303b91ad20c3ba5902da25b462f3ce6df5.zip |
Added transforms.
Diffstat (limited to 'core/source')
3 files changed, 193 insertions, 2 deletions
diff --git a/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala b/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala index 3bbed88..64be8fa 100755 --- a/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala +++ b/core/source/main/scala/com/rockymadden/stringmetric/Metric.scala @@ -2,11 +2,11 @@ package com.rockymadden.stringmetric object Metric { trait MetricLike[A, B] { - def compare(a1: A, a2: A): Option[B] + def compare(a: A, b: A): Option[B] } trait StringMetricLike[A] extends MetricLike[Array[Char], A] { - def compare(string1: String, string2: String): Option[A] + def compare(a: String, b: String): Option[A] } } diff --git a/core/source/main/scala/com/rockymadden/stringmetric/Transform.scala b/core/source/main/scala/com/rockymadden/stringmetric/Transform.scala new file mode 100644 index 0000000..b09ebb6 --- /dev/null +++ b/core/source/main/scala/com/rockymadden/stringmetric/Transform.scala @@ -0,0 +1,69 @@ +package com.rockymadden.stringmetric + +object Transform { + import scala.collection.immutable.NumericRange + + + type Transform[A] = (A => A) + type StringTransform = Transform[String] + + + object StringTransform { + private final val Ascii = NumericRange(0x00, 0x7F, 1) + private final val ExtendedAscii = NumericRange(0x00, 0x7F, 1) + private final val Latin = NumericRange(0x00, 0x24F, 1) + private final val LowerCase = NumericRange(0x61, 0x7A, 1) + private final val Numbers = NumericRange(0x30, 0x39, 1) + private final val UpperCase = NumericRange(0x41, 0x5A, 1) + + private final val filter: ((String, (Char => Boolean)) => String) = (s, f) => + s.toCharArray.filter(c => f(c)).mkString + + private final val filterNot: ((String, (Char => Boolean)) => String) = (s, f) => + s.toCharArray.filterNot(c => f(c)).mkString + + val filterAlpha: StringTransform = (string) => filter(string, c => { + val ci = c.toInt + LowerCase.contains(ci) || UpperCase.contains(ci) + }) + + val filterNotAlpha: StringTransform = (string) => filterNot(string, c => { + val ci = c.toInt + LowerCase.contains(ci) || UpperCase.contains(ci) + }) + + val filterAlphaNumeric: StringTransform = (string) => filter(string, c => { + val ci = c.toInt + LowerCase.contains(ci) || UpperCase.contains(ci) || Numbers.contains(ci) + }) + + val filterNotAlphaNumeric: StringTransform = (string) => filterNot(string, c => { + val ci = c.toInt + LowerCase.contains(ci) || UpperCase.contains(ci) || Numbers.contains(ci) + }) + + val filterAscii: StringTransform = (string) => filter(string, c => Ascii.contains(c.toInt)) + + val filterNotAscii: StringTransform = (string) => filterNot(string, c => Ascii.contains(c.toInt)) + + val filterExtendedAscii: StringTransform = (string) => filter(string, c => ExtendedAscii.contains(c.toInt)) + + val filterNotExtendedAscii: StringTransform = (string) => filterNot(string, c => ExtendedAscii.contains(c.toInt)) + + val filterLatin: StringTransform = (string) => filter(string, c => Latin.contains(c.toInt)) + + val filterNotLatin: StringTransform = (string) => filterNot(string, c => Latin.contains(c.toInt)) + + val filterLowerCase: StringTransform = (string) => filter(string, c => LowerCase.contains(c.toInt)) + + val filterNotLowerCase: StringTransform = (string) => filterNot(string, c => LowerCase.contains(c.toInt)) + + val filterNumeric: StringTransform = (string) => filter(string, c => Numbers.contains(c.toInt)) + + val filterNotNumeric: StringTransform = (string) => filterNot(string, c => Numbers.contains(c.toInt)) + + val filterUpperCase: StringTransform = (string) => filter(string, c => UpperCase.contains(c.toInt)) + + val filterNotUpperCase: StringTransform = (string) => filterNot(string, c => UpperCase.contains(c.toInt)) + } +} diff --git a/core/source/test/scala/com/rockymadden/stringmetric/TransformSpec.scala b/core/source/test/scala/com/rockymadden/stringmetric/TransformSpec.scala new file mode 100644 index 0000000..a5da25d --- /dev/null +++ b/core/source/test/scala/com/rockymadden/stringmetric/TransformSpec.scala @@ -0,0 +1,122 @@ +package com.rockymadden.stringmetric + +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class TransformSpec extends ScalaTest { "Transform" should provide { + import com.rockymadden.stringmetric.Transform._ + + "filterAlpha()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterAlpha("aBc123" + 0x250.toChar) should be ("aBc") + } + } + } + "filterNotAlpha()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotAlpha("aBc123" + 0x250.toChar) should be ("123" + 0x250.toChar) + } + } + } + "filterAlphaNumeric()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterAlphaNumeric("aBc123" + 0x250.toChar) should be ("aBc123") + } + } + } + "filterNotAlphaNumeric()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotAlphaNumeric("aBc123" + 0x250.toChar) should be ("" + 0x250.toChar) + } + } + } + "filterAscii()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterAscii("aBc" + 0x80.toChar) should be ("aBc") + } + } + } + "filterNotAscii()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotAscii("aBc" + 0x100.toChar) should be ("" + 0x100.toChar) + } + } + } + "filterExtendedAscii()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterExtendedAscii("aBc" + 0x100.toChar) should be ("aBc") + } + } + } + "filterNotExtendedAscii()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotExtendedAscii("aBc" + 0x250.toChar) should be ("" + 0x250.toChar) + } + } + } + "filterLatin()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterLatin("aBc" + 0x250.toChar) should be ("aBc") + } + } + } + "filterNotLatin()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotLatin("aBc" + 0x300.toChar) should be ("" + 0x300.toChar) + } + } + } + "filterLowerCase()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterLowerCase("aBc123" + 0x250.toChar) should be ("ac") + } + } + } + "filterNotLowerCase()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotLowerCase("aBc123" + 0x250.toChar) should be ("B123" + 0x250.toChar) + } + } + } + "filterNumeric()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNumeric("aBc123" + 0x250.toChar) should be ("123") + } + } + } + "filterNotNumeric()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotNumeric("aBc123" + 0x250.toChar) should be ("aBc" + 0x250.toChar) + } + } + } + "filterUpperCase()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterUpperCase("aBc123" + 0x250.toChar) should be ("B") + } + } + } + "filterNotUpperCase()" when passed { + "String" should returns { + "transformed String" in { + StringTransform.filterNotUpperCase("aBc123" + 0x250.toChar) should be ("ac123" + 0x250.toChar) + } + } + } +}} |