Created
September 14, 2011 11:52
-
-
Save ydarias/1216384 to your computer and use it in GitHub Desktop.
StringCalculator with and without underscore.js
This file contains 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
StringCalculator = { | |
add : function(input) { | |
checkIfContainsNegativeNumbers(input); | |
var parser = new Parser(input); | |
var numbers = parser.parse(); | |
return numbers.getSummatory(); | |
} | |
} | |
function checkIfContainsNegativeNumbers(input) { | |
negativeNumbers = input.getNegativeNumbers(); | |
if (negativeNumbers) | |
throw "Negative numbers are not supported (" + negativeNumbers.toStringList() + ")"; | |
} | |
function Parser(input) { | |
var _input = input; | |
this.parse = function() { | |
var separator = parseSeparator(_input); | |
var numbers = parseNumbers(_input); | |
return numbers.replace(separator, ",").split(/,|\n/g); | |
} | |
function parseNumbers(input) { | |
if (input.hasCustomDelimiter()) | |
return input.getNumbersAfterDelimiterDefinition(); | |
return input; | |
} | |
function parseSeparator(input) { | |
if (input.hasCustomDelimiter()) | |
return input.getCustomDelimiter(); | |
return ","; | |
} | |
} | |
/* Extending Javascript types to symplify code legibility */ | |
Array.prototype.getSummatory = function() { | |
var result = 0; | |
for (var current = 0; current < this.length; current++) | |
result += parseInt(this[current] || 0); | |
return result; | |
} | |
Array.prototype.toStringList = function() { | |
var message = ""; | |
for (var current = 0; current < this.length; current++) { | |
message += this[current]; | |
if (current < this.lastElementPosition()) | |
message += ", "; | |
} | |
return message; | |
} | |
Array.prototype.lastElementPosition = function() { | |
return this.length - 1; | |
} | |
String.prototype.getNegativeNumbers = function() { | |
return this.match(/-\d/g); | |
} | |
String.prototype.hasCustomDelimiter = function() { | |
return this.match("^\/\/"); | |
} | |
String.prototype.getCustomDelimiter = function() { | |
return this.charAt(2); | |
} | |
String.prototype.getNumbersAfterDelimiterDefinition = function() { | |
var matcher = this.match("(//;\n)?(.*)"); | |
return matcher[2]; | |
} |
This file contains 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
StringCalculator = { | |
add : function(input) { | |
checkIfContainsNegativeNumbers(input); | |
var numbers = new Parser(input).parse(); | |
return _(numbers).reduce(this.summatory); | |
}, | |
summatory: function(summatory, element) { | |
return summatory + element; | |
} | |
} | |
function checkIfContainsNegativeNumbers(input) { | |
negativeNumbers = input.getNegativeNumbers(); | |
if (negativeNumbers) | |
throw "Negative numbers are not supported (" + negativeNumbers.toString() + ")"; | |
} | |
function Parser(input) { | |
var _input = input; | |
this.parse = function() { | |
var separator = parseSeparator(_input); | |
var numbers = parseNumbers(_input); | |
numbers = numbers.replace(separator, ",").split(/,|\n/g); | |
return _(numbers).map(parseToIntNumber); | |
} | |
function parseNumbers(input) { | |
if (input.hasCustomDelimiter()) | |
return input.getNumbersAfterDelimiterDefinition(); | |
return input; | |
} | |
function parseSeparator(input) { | |
if (input.hasCustomDelimiter()) | |
return input.getCustomDelimiter(); | |
return ","; | |
} | |
var parseToIntNumber = function(element) { | |
return parseInt(element || 0); | |
} | |
} | |
/* Extending Javascript types to symplify code legibility */ | |
String.prototype.getNegativeNumbers = function() { | |
return this.match(/-\d/g); | |
} | |
String.prototype.hasCustomDelimiter = function() { | |
return this.match("^\/\/"); | |
} | |
String.prototype.getCustomDelimiter = function() { | |
return this.charAt(2); | |
} | |
String.prototype.getNumbersAfterDelimiterDefinition = function() { | |
var matcher = this.match("(//;\n)?(.*)"); | |
return matcher[2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment