Created
May 16, 2013 18:41
-
-
Save ferclaverino/5594014 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
} | |
); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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