patternjavascriptMinor
Module to validate and sum a string containing no negative numbers
Viewed 0 times
validatecontainingnumbersmodulenegativesumandstring
Problem
To learn the language of JavaScript, I did a dojo exercise by Roy Osherove. Is there a better way to organize this so that the only method exported is
add while keeping it in one single module?module.exports = {
add: function (string) {
this._checkForError(string);
this._checkForNegatives(string);
return this._result(string);
},
_result: function (string) {
var numbers = this._numbers(string);
var result = numbers.reduce(function (sum, number) {
return sum + number;
});
return result;
},
_numbers: function (string) {
var modded = this._modded(string);
var numbers = modded.map(function (number) {
return parseInt(number, 10) || 0;
});
return numbers;
},
_checkForError: function (string) {
if (string.match(/\n$/)) {
throw 'Expression ending in newline!';
}
},
_negatives: function (string) {
var numbers = this._numbers (string);
var negatives = numbers.filter(function (number) {
return number 0) {
throw 'Negatives not allowed: ' + negatives.join(', ');
}
},
_modded: function (string) {
var delimiter = this._delimiter(string);
return string.replace(/\n/, delimiter)
.split(delimiter);
}
};Solution
module.exports is just an object. Set properties on it that you want to expose.exports.add = add
This is also equivalent in this case.
module.exports = {add: add}
Now, when you require this module, you'll get an object with one property:
add.There are a few other small issues:
Nothing about your design depends on accessing methods via
this. This moduleis clearer without bringing
this into the mix.function add (string) {
_checkForError(string)
_checkForNegatives(string)
return _result(string)
}
This code is quite easy to follow already, but It would be even better if you
had your methods in the order they are called so the code reads like an article:
broad info at the top, specific details later on.
I'd also leave out semicolons where they will be handled by automatic semicolon
insertion, but that's a style choice.
`function _checkForError (string) {
if (string.match(/\n$/)) {
throw 'Expression ending in newline!'
}
}
function _checkForNegatives (string) {
var negatives = _negatives(string)
if (negatives.length > 0) {
throw 'Negatives not allowed: ' + negatives.join(', ')
}
}
function _negatives (string) {
var numbers = _numbers(string)
var negatives = numbers.filter(function (number) {
return number
Context
StackExchange Code Review Q#52236, answer score: 5
Revisions (0)
No revisions yet.