Commits

Jiří Knesl committed 8ec7b83

initial

Comments (0)

Files changed (6)

+all:
+	make compile
+	make unittest
+
+compile:
+	livescript -bc lib/ESCR.ls
+	livescript -bc test/ESCRTest.ls
+
+unittest:
+	expresso
+ESCR plugin
+
+Changes Czech localized-chars to numbers
+
+Automatically turns +ěščřžýáíé into 1234567890
+
+Licence: BSD-like
+Author: Jiri Knesl <jiri@sprintmasters.cz> (http://www.knesl.com)
+
+Example in jQuery:
+
+$(document).ready(function(){
+	var escr = new ESCR();
+	$("input.numeric").change(function(){
+		val = $(this).val;
+		val( escr.rewrite( val() ) );
+	});
+});
+var ESCR;
+ESCR = (function(){
+  ESCR.displayName = 'ESCR';
+  var prototype = ESCR.prototype, constructor = ESCR;
+  prototype.replaces = {
+    "+": "1",
+    "ě": "2",
+    "š": "3",
+    "č": "4",
+    "ř": "5",
+    "ž": "6",
+    "ý": "7",
+    "á": "8",
+    "í": "9",
+    "é": "0"
+  };
+  prototype.rewrite = function(value){
+    return this.rewriteWithReplaces(value, this.replaces);
+  };
+  prototype.rewriteWithReplaces = function(value, replaces){
+    var key, replacesWithoutFirst, replaceValue, valueReplaced;
+    key = this.firstKeyIn(replaces);
+    if (key === null) {
+      return value;
+    } else {
+      replacesWithoutFirst = replaces;
+      replaceValue = replaces[key] + "";
+      delete replacesWithoutFirst[key];
+      valueReplaced = value.replace(key, replaceValue);
+      return this.rewriteWithReplaces(valueReplaced, replacesWithoutFirst);
+    }
+  };
+  prototype.firstKeyIn = function(replaces){
+    var firstKey, key;
+    firstKey = null;
+    for (key in replaces) {
+      if (replaces.hasOwnProperty(key)) {
+        firstKey = key;
+        break;
+      }
+    }
+    return firstKey;
+  };
+  function ESCR(){}
+  return ESCR;
+}());
+module.exports = ESCR;
+class ESCR
+
+	replaces: {"+":"1", "ě":"2", "š":"3", "č":"4", "ř":"5", "ž":"6", "ý":"7", "á":"8", "í":"9", "é":"0"}
+
+	rewrite: (value) ->
+		@rewrite-with-replaces value, @replaces
+
+	rewrite-with-replaces: (value, replaces) ->
+		key = @first-key-in replaces
+		if key is null
+			value
+		else
+			replaces-without-first = replaces
+			replace-value = replaces[key] + ""
+			delete replaces-without-first[key]
+
+			value-replaced = value.replace(key, replace-value)
+			@rewrite-with-replaces value-replaced, replaces-without-first
+
+	first-key-in: (replaces) ->
+		first-key = null
+		for key of replaces
+			if replaces.has-own-property key
+				first-key = key
+				break
+
+		first-key
+
+module.exports = ESCR
+var ESCR, assert;
+ESCR = require("../lib/escr");
+assert = require("assert");
+exports['test rewrite'] = function(){
+  var escr;
+  escr = new ESCR;
+  return assert.equal('1234567890', escr.rewrite("+ěščřžýáíé"));
+};
+ESCR = require "../lib/escr"
+assert = require "assert"
+
+exports['test rewrite'] = ->
+	escr = new ESCR
+	assert.equal '1234567890', escr.rewrite "+ěščřžýáíé"