Skip to content

Instantly share code, notes, and snippets.

@ferclaverino
Created May 16, 2013 18:41
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 ferclaverino/5594014 to your computer and use it in GitHub Desktop.
Save ferclaverino/5594014 to your computer and use it in GitHub Desktop.
(function () {
"use strict";
function parseBenefit(text) {
var type = "otro";
var value = "";
if (text != "") {
var numberPattern = /\d+/g;
var result = text.match(numberPattern);
if (result) {
if (text.indexOf("%") > 0) {
type = "descuento";
value = result[0] + "%";
} else if (text.indexOf("cuotas") > 0){
type = "cuota";
value = result[result.length - 1];
}
}
}
return { type: type, value: value, text: text };
}
// Export public methods
WinJS.Namespace.define("Benefit",
{
parseBenefit: parseBenefit
}
);
})();
var Benefit;
var WinJS = {};
WinJS.Namespace = {};
WinJS.Namespace.define = function(name, object) {
if (name == "Benefit") {
Benefit = object;
}
}
test("empty text return otro type", function() {
var benefit = Benefit.parseBenefit("");
deepEqual( benefit, { type: "otro", value: "", text: "" } );
});
test("'20% de descuento' return descuento type", function() {
var benefit = Benefit.parseBenefit("20% de descuento en ropa");
deepEqual( benefit, { type: "descuento", value: "20%", text: "20% de descuento en ropa" } );
});
test("'30% de descuento' return 30", function() {
var benefit = Benefit.parseBenefit("30% de descuento en ropa");
deepEqual( benefit, { type: "descuento", value: "30%", text: "30% de descuento en ropa" } );
});
test("'30% de descuento' return 30", function() {
var benefit = Benefit.parseBenefit("30% de descuento en ropa");
deepEqual( benefit, { type: "descuento", value: "30%", text: "30% de descuento en ropa" } );
});
test("'Los martes todos salen a cenar' return otro type", function() {
var benefit = Benefit.parseBenefit("Los martes todos salen a cenar");
deepEqual( benefit, { type: "otro", value: "", text: "Los martes todos salen a cenar" } );
});
test("'30 cuotas sin interés sábados y domingos' return cuota type", function() {
var benefit = Benefit.parseBenefit("30 cuotas sin interés sábados y domingos");
deepEqual( benefit, { type: "cuota", value: "30", text: "30 cuotas sin interés sábados y domingos" } );
});
test("'Vení a comer 3 días' return otro type", function() {
var benefit = Benefit.parseBenefit("Vení a comer 3 días");
deepEqual( benefit, { type: "otro", value: "", text: "Vení a comer 3 días" } );
});
test("'10, 20 y hasta 30 cuotas sin interés sábados y domingos' return cuota type", function() {
var benefit = Benefit.parseBenefit("10, 20 y hasta 30 cuotas sin interés sábados y domingos");
deepEqual( benefit, { type: "cuota", value: "30", text: "10, 20 y hasta 30 cuotas sin interés sábados y domingos" } );
});
test("'10% de descuento y hasta 12 cuotas sin interés' return cuota type", function() {
var benefit = Benefit.parseBenefit("10% de descuento y hasta 12 cuotas sin interés");
deepEqual( benefit, { type: "descuento", value: "10%", text: "10% de descuento y hasta 12 cuotas sin interés" } );
});
// 10, 20 y hasta 30 cuotas sin interés sábados y domingos
// 10% de descuento y hasta 12 cuotas sin interés
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment