diff --git a/src/index.js b/src/index.js index a1c9be6..13145cf 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,8 @@ export { download } from './general/download' // Http export { get } from './http/get' +// Math +export { sum } from './math/operations.js' // Strings export { parseURL } from './strings/parseURL' export { capitalize, capitalizeList } from './strings/capitalize' diff --git a/src/math/operations.js b/src/math/operations.js new file mode 100644 index 0000000..33da189 --- /dev/null +++ b/src/math/operations.js @@ -0,0 +1,12 @@ +/** + * Get numbers and return their sum. + * @param numbers + * @returns {number} + */ +export function sum (...numbers) { + if (numbers.some(number => typeof number !== 'number')) { + throw TypeError('Parameters must be numbers.') + } + + return numbers.reduce((a, b) => a + b, 0) +} diff --git a/test/test.js b/test/test.js index 1c60398..3f383ac 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,16 @@ const tape = require('tape') const utils = require('../dist/utils') +/** Math module tests **/ + +tape('Operation functions', function (test) { + test.equal(utils.sum(1, 1, 2), 4) + + test.throws(() => utils.sum(1, 2, 'hello')) + + test.end() +}) + /** String module tests **/ tape('Capitalize functions', function (test) {