Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added smsifier with smsspec

  • Loading branch information...
commit 8f23ff61ed751c7c5070e8f3fcfbce9eff35de4f 1 parent 6f62dd3
@AnaMarjanica authored
View
4 build.sbt
@@ -2,7 +2,7 @@ organization := "hr.element.etb"
name := "etb-slug"
-version := "0.0.1"
+version := "0.0.2"
// ### Build settings ###
@@ -33,4 +33,4 @@ publishArtifact in (Compile, packageDoc) := false
// ### Misc ###
-initialCommands := "import hr.element.doit.slug._"
+//initialCommands := "import hr.element.doit.slug._"
View
49 src/main/scala/hr/element/etb/slug/SMSify.scala
@@ -0,0 +1,49 @@
+package hr.element.etb
+package slug
+
+import com.ibm.icu.text.Transliterator
+import java.util.regex.Pattern
+
+
+
+object SMSifier {
+ protected val SMSUnsafeChars = """[^!-/0-9:-@A-Za-z£¥\n\s]+"""
+
+ private val default =
+ new SMSifier(TransliterateRules.latinToASCII, "-")
+
+ def apply(text: String) = default(text)
+}
+
+
+case class SMSifier (transRules: String, replacement: String) {
+ private val Trans = Transliterator.getInstance(transRules)
+
+ private val SMSUnsafeReplacePattern =
+ "(%s|%s)+" format(SMSifier.SMSUnsafeChars, Pattern.quote(replacement)) r
+
+ private val SMSUnsafeTrimPattern =
+ "^%s|%1$s$" format(SMSifier.SMSUnsafeChars) r
+
+ private val SMSWhiteSpace =
+ """\s+"""r
+
+ // convert latin letters to ASCII (ex. đ->d)
+ protected val transliterate =
+ Trans.transliterate(_: String)
+
+ // trim all SMS non-safe characters from the beginning and the end
+ protected val trim =
+ SMSUnsafeTrimPattern.replaceAllIn(_: String, "")
+
+ protected val trimWhiteSpaceToOne =
+ SMSWhiteSpace.replaceAllIn(_: String, " ")
+
+ // replace all SMS non-safe characters
+ protected val sanitize =
+ SMSUnsafeReplacePattern.replaceAllIn(_: String, replacement)
+
+ def apply(text: String) = {
+ (transliterate andThen trim andThen trimWhiteSpaceToOne andThen sanitize)(text)
+ }
+}
View
42 src/test/scala/hr/element/etb/slug/SMSSpec.scala
@@ -0,0 +1,42 @@
+package hr.element.etb.slug
+package test
+
+import org.scalatest._
+import org.scalatest.matchers._
+
+
+
+class SMSSpec extends FeatureSpec with GivenWhenThen with MustMatchers{
+
+ feature("SMS URL sanitization"){
+ info("SMS must convert every evil character combination into a pretty URL-safe string")
+
+ scenario("SMS unsafe character removal"){
+ val in = "aZ{}[]! a";
+ given ("a string containing: %s" format in)
+ val res = "aZ-! a"
+ then ("it should return a string: %s" format res)
+ val out = SMSifier(in)
+ out must equal (res)
+ }
+
+ scenario("SMS unsafe character trimming"){
+ val in = "!aćsčš ";
+ given ("a string containing: %s" format in)
+ val res = "!acscs "
+ then ("it should return a string: %s" format res)
+ val out = SMSifier(in)
+ out must equal (res)
+ }
+
+ scenario("SMS whitespace trimming to one character"){
+ val in = "Helooooooooooooo oooo";
+ given ("a string containing: %s" format in)
+ val res = "Helooooooooooooo oooo";
+ then ("it should return a string: %s" format res)
+ val out = SMSifier(in)
+ out must equal (res)
+ }
+ }
+
+}
View
9 src/test/scala/hr/element/etb/slug/SluggifySpec.scala
@@ -34,5 +34,14 @@ class SluggifySpec extends FeatureSpec with GivenWhenThen with MustMatchers {
val out = Sluggifier(in)
out must equal (res)
}
+
+ scenario("URL unsafe character trimming2"){
+ val in = """~ $Hajduk ! Is better!!!Slayer rules$ ~""" mkString;
+ given ("an URL unsafe string: %s" format in)
+ val res = "hajduk-is-better-slayer-rules"
+ then ("the resulting slug must be trimmed: %s" format res)
+ val out = Sluggifier(in)
+ out must equal (res)
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.