Skip to content

Esercitazione Fabrizio #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
28 changes: 25 additions & 3 deletions lib/string-calculator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
exports.add = function (string) {
let numbers = string.split(/,|\n/).map(Number)
return numbers.reduce((a, b) => a + b)
module.exports = { add, parseCustomDelimiter }

function add (string) {
if (string.length === 0) {
return 0
}
let customDelimiter = parseCustomDelimiter(string)
let sum = 0
let numbers
if (customDelimiter) {
string = string.substr(3, string.length)
numbers = string.split(customDelimiter)
} else {
numbers = string.split(/,|\n/g)
}
for (let i = 0; i < numbers.length; i++) {
sum += parseInt(numbers[i])
}
return sum
}

function parseCustomDelimiter (string) {
if (string.length > 2 && string[0] === '/' && string[1] === '/') {
return string[2]
}
}
47 changes: 35 additions & 12 deletions test/string-calculator-test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
const assert = require('assert')
const calculator = require('../lib/string-calculator')
const {equal} = require('assert')

describe('string-calculator', function () {
describe('add', function () {
it('is 0 for an empty string', function () {
assert.equal(calculator.add(''), 0)
describe('StringCalculator', function () {
const StringCalculator = require('../lib/string-calculator')

describe('#add', function () {
const {add} = StringCalculator

it('returns 0 on empty string', function () {
equal(add(''), 0)
})

it('returns number for a single number string', function () {
equal(add('1'), 1)
})

it('returns sum for a comma separated string with two values', function () {
equal(add('1,2'), 3)
})

it('is a number for a string with only a number', function () {
assert.equal(calculator.add('1'), 1)
it('returns sum for an unknown number of comma separated values', function () {
equal(add('1,2,3,4'), 10)
})

it('is the sum of the numbers for a string of any amount of comma-separated numbers', function () {
assert.equal(calculator.add('1,2,3'), 6)
it('returns sum for an unknown number of an end-of-line separated values', function () {
equal(add('1\n2'), 3)
})

it('returns sum for custom delimited values', function () {
equal(add('//x\n1x2'), 3)
})
})

describe('#parseCustomDelimiter', function () {
const {parseCustomDelimiter: parse} = StringCalculator

it('returns undefined if no custom delimiter is specified', function () {
equal(parse('1,2'), undefined)
})

it('handles also \\n as number delimiter', function () {
assert.equal(calculator.add('1\n2'), 3)
it('returns a custom delimiter', function () {
equal(parse('//x\n1x2'), 'x')
})
})
})