Commit ce8e64e4 authored by Smari McCarthy's avatar Smari McCarthy
Browse files

Start matching symbols

parent 37af0df8
......@@ -56,4 +56,4 @@
}
.marka > .marka-results > .marka-result {
padding:0.1em;
}
\ No newline at end of file
}
......@@ -2,13 +2,11 @@ cached_exchange_rates = {}
function Marka(target) {
this.setCurrency("eur");
this.getValueInSetCurrency(200, "usd", function(value){
console.log("Exchanged to: " + value);
});
if (!target) {
this.target = $('body').get();
} else {
......@@ -21,7 +19,8 @@ function Marka(target) {
Marka.prototype.match = function() {
// docs: https://github.com/padolsey/findAndReplaceDOMText
var currencySymbols = "[€$¢¥₹₨]";
this.currencySymbols = "[€$¢¥₹₨]";
var currencySymbols = this.currencySymbols;
var currencyWords = "(" + currencySymbols + "|kr|[Dd]ollars|[Cc]ents on the dollar|[Cc]ents|[Ee]uros|[Pp]ounds|[Rr]upees|[Zz]loty)";
var numbers = "[\\d,.]+";
var optSpace = "[\\s]*";
......@@ -35,7 +34,6 @@ Marka.prototype.match = function() {
var format3 = numbers + optSpace + magnitudes + optSpace + currencyWords;
var regex = new RegExp("(" + format1 + "|" + format2 + "|" + format3 + ")", "g")
findAndReplaceDOMText(this.target, {
find: regex,
wrap: this.wrapNode()
......@@ -46,20 +44,55 @@ Marka.prototype.doConversions = function() {
var self = this;
$(".marka").each(function(index, value) {
var text = $(value).text();
self.splitSymbols(text);
var res = self.splitSymbols(text);
if (!res) {
$(value).replaceWith(text)
}
});
}
Marka.prototype.splitSymbols = function(text) {
var numbers = /[\d,.]+/;
var number = text.match(numbers)[0];
try {
var number = text.match(numbers)[0];
} catch (e) {
return false;
}
var residual = text.replace(numbers, "");
var symbol = this.findSymbol(residual);
return {number: number, symbol: symbol};
var result = this.findSymbol(residual);
return true;
}
Marka.prototype.findSymbol = function(text) {
var result = {certain: false, symbol: null}
text = $.trim(text);
console.log(">"+text+"<");
if (text.match(/[A-Z]{3}/)) {
for (code in currency_info) {
if (code == text) {
console.log("FOUND SYMBOL: " + code);
}
}
} else if (text.match(/[$€¥]/)) {
for (code in currency_info) {
if (currency_info[code].symbol == text) {
console.log("FOUND SYMBOL: " + code);
}
}
}
return text;
/*
"ILS": {
"symbol": "₪",
"name": "Israeli New Sheqel",
"symbol_native": "₪",
"decimal_digits": 2,
"rounding": 0,
"code": "ILS",
"name_plural": "Israeli new sheqels"
},
*/
}
Marka.prototype.wrapNode = function() {
......@@ -75,7 +108,7 @@ Marka.prototype.getValueInCurrency = function(value, from_currency, to_currency,
if(!to_currency){
callback(value);
}
this.retreiveExchangeRates(from_currency, to_currency, function(rate){
var converted_amount = value * rate;
callback(converted_amount);
......@@ -83,7 +116,7 @@ Marka.prototype.getValueInCurrency = function(value, from_currency, to_currency,
}
Marka.prototype.getCurrentCurrency = function(){
var currency = this.getCookie("currency");
var currency = this.getCookie("currency");
return currency;
}
......@@ -3459,4 +3492,4 @@ var currency_info = {
"code": "ZMK",
"name_plural": "Zambian kwachas"
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment