Skip to content

Instantly share code, notes, and snippets.

@digiguru
Last active December 19, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digiguru/5935858 to your computer and use it in GitHub Desktop.
Save digiguru/5935858 to your computer and use it in GitHub Desktop.
We have a budget planner app, and the first thing we ask the user is "What is your wedding budget?". Given an input of various types, I wanted to calculate the intention of the user without having to ask them any further questions. Originally we just did a simple parseInt(input, 10). After a while we noticed users getting the input incorrect. A …
/*jslint plusplus: true */
/*jslint nomen: true*/
/*global $:false, test:false, ok:false, equal:false */
/* brackets-xunit: qunit */
function convertStringToBudget(input) {
"use strict";
var pointReg = /\./g,
commaReg = /\,/g,
initCurrReg = /[£$R€₹]/g,
endCurrReg = /[pc]/g,
pointMatch = input.match(pointReg),
commaMatch = input.match(commaReg),
multiply1000 = false,
returnValue = 0;
//Strip Spaces
input = input.trim();
//Strip starting currency
input = input.replace(initCurrReg, "");
//Strip ending currency
input = input.replace(endCurrReg, "");
if (input[input.length - 1] === "k" || input[input.length - 1] === "K") {
input = input.replace("k", "").replace("K", "");
multiply1000 = true;
}
if (pointMatch) {
if (pointMatch.length >= 2) { //"multiple decimal points, possibly european"
input = input.replace(pointReg, "");
} else if (input.match(pointReg).length === 1) { //"1 decimal point, check decimal places"
if (input.split(".")[1].length > 2) { //over 2 decimal places, it's european
input = input.replace(pointReg, "");
}
}
}
if (commaMatch) {
if (commaMatch.length >= 2) { //"multiple commas, must be UK"
input = input.replace(commaReg, "");
} else if (input.match(commaReg).length === 1) { //"1 comma, check decimal places"
if (input.split(",")[1].length < 3) { //less than 3 decimal places, it's european
input = input.replace(commaReg, ".");
} else {
input = input.replace(commaReg, "");
}
}
}
returnValue = parseFloat(input, 10);
if (multiply1000) {
returnValue = returnValue * 1000;
}
return returnValue;
}
(function () {
"use strict";
test("Simple numbers", function () {
equal(convertStringToBudget("1"), 1, "1 is 1 works for simple numbers");
equal(convertStringToBudget("1.1"), 1.1, "1.1 is 1.1 works for decimals");
equal(convertStringToBudget("1000"), 1000, "1000 is 1000");
equal(convertStringToBudget("1k"), 1000, "1k is 1000");
equal(convertStringToBudget("1.1k"), 1100, "1.1k is 1100");
equal(convertStringToBudget("1.12K"), 1120, "1.12K is 1120");
});
test("Currency Numbers", function () {
equal(convertStringToBudget("£1"), 1, "£1 is 1");
equal(convertStringToBudget("£1.1"), 1.1, "£1.1 is 1.1");
equal(convertStringToBudget("£1000"), 1000, "£1000 is 1000");
equal(convertStringToBudget("£1k"), 1000, "£1k is 1000");
equal(convertStringToBudget("£1.1k"), 1100, "£1.1k is 1100");
equal(convertStringToBudget("€1"), 1, "€1 is 1");
equal(convertStringToBudget("$1"), 1, "$1 is 1");
equal(convertStringToBudget("R1"), 1, "R1 is 1");
equal(convertStringToBudget("$1.01c"), 1.01, "$1.01c is 1.01");
equal(convertStringToBudget("£1.01p"), 1.01, "£1.01p is 1.01");
equal(convertStringToBudget("€1,01c"), 1.01, "€1,01c is 1.01");
equal(convertStringToBudget("₹1,01"), 1.01, "₹1,01 is 1.01");
});
test("Spaced Numbers", function () {
equal(convertStringToBudget(" £1 "), 1, "1 is 1");
equal(convertStringToBudget(" £1.1 "), 1.1, "1.1 is 1.1");
equal(convertStringToBudget(" £1000 "), 1000, "1000 is 1000");
equal(convertStringToBudget(" £1k "), 1000, "1k is 1000");
equal(convertStringToBudget(" £1.1k "), 1100, "1.1k is 1100");
});
test("Confusing Numbers", function () {
equal(convertStringToBudget("1.100.000"), 1100000, "1.100.000 is 1100000 - european style");
equal(convertStringToBudget("1.100.00"), 110000, "1.100.00 is 110000 - european style");
equal(convertStringToBudget("1.100.0"), 11000, "1.100.00 is 110000 - european style");
equal(convertStringToBudget("1.100"), 1100, "1.100 is 1100 - european style");
equal(convertStringToBudget("1.10"), 1.1, "1.10 is 1.1 - UK style");
equal(convertStringToBudget("1.1"), 1.1, "1.1 is 1.1 - UK style");
equal(convertStringToBudget("1,100"), 1100, "1,100 is 1100 - UK style");
equal(convertStringToBudget("1,100.00"), 1100, "1,100.00 is 1100 - UK style");
equal(convertStringToBudget("1.100.00"), 110000, "1.100.00 is 110000 - european style");
equal(convertStringToBudget("1,1"), 1.1, "1,1 is 1.1 - european style");
equal(convertStringToBudget("1,10"), 1.1, "1,10 is 1.1 - european style");
equal(convertStringToBudget("1,100"), 1100, "1,1 is 1.1 - UK style");
equal(convertStringToBudget("1.100,00"), 1100, "1.100,00 is 1100 - european style");
equal(convertStringToBudget("12,34,56.01"), 123456.01, "12,34,56.01 is 123456.01 - Indian style");
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment