From f1ba3b6128c66f30e83c2514b9fd8ae2a4d77a6d Mon Sep 17 00:00:00 2001 From: Terkel Gjervig Date: Thu, 20 Oct 2016 21:00:18 -0700 Subject: [PATCH] feat: Added maxAdd and minSub --- src/math-toolbox.js | 2 ++ src/max-add.js | 13 +++++++++++++ src/min-sub.js | 13 +++++++++++++ test/max-add.test.js | 11 +++++++++++ test/min-sub.test.js | 11 +++++++++++ 5 files changed, 50 insertions(+) create mode 100755 src/max-add.js create mode 100755 src/min-sub.js create mode 100644 test/max-add.test.js create mode 100644 test/min-sub.test.js diff --git a/src/math-toolbox.js b/src/math-toolbox.js index a33c1cb..ddf49fe 100755 --- a/src/math-toolbox.js +++ b/src/math-toolbox.js @@ -18,3 +18,5 @@ export { fuzzyCeil } from './fuzzy-ceil' export { fuzzyEqual } from './fuzzy-equal' export { fuzzyGreaterThan } from './fuzzy-greater-than' export { fuzzyLessThan } from './fuzzy-less-than' +export { maxAdd } from './max-add' +export { minSub } from './min-sub' diff --git a/src/max-add.js b/src/max-add.js new file mode 100755 index 0000000..1129d47 --- /dev/null +++ b/src/max-add.js @@ -0,0 +1,13 @@ +/** + * Adds the given amount to the value, but never lets the value go over the specified maximum. + * + * @param {number} value - The value to add the amount to. + * @param {number} amount - The amount to add to the value. + * @param {number} max - The maximum the value is allowed to be. + * @return {number} The new value. + */ +function maxAdd (value, amount, max) { + return Math.min(value + amount, max) +} + +export { maxAdd } diff --git a/src/min-sub.js b/src/min-sub.js new file mode 100755 index 0000000..9b28710 --- /dev/null +++ b/src/min-sub.js @@ -0,0 +1,13 @@ +/** + * Subtracts the given amount from the value, but never lets the value go below the specified minimum. + * + * @param {number} value - The base value. + * @param {number} amount - The amount to subtract from the base value. + * @param {number} min - The minimum the value is allowed to be. + * @return {number} The new value. + */ +function minSub (value, amount, min) { + return Math.max(value - amount, min) +} + +export { minSub } diff --git a/test/max-add.test.js b/test/max-add.test.js new file mode 100644 index 0000000..f2d3f43 --- /dev/null +++ b/test/max-add.test.js @@ -0,0 +1,11 @@ +import { maxAdd } from '../src/math-toolbox' + +describe('Adds the given amount to the value, but never lets the value go over the specified maximum', () => { + it('Add 10 to 30 with max 35, expect to it to return 35', () => { + expect(maxAdd(10, 30, 35)).toBe(35) + }) + + it('Add 10 to 30 with max 45, expect to it to return 40', () => { + expect(maxAdd(10, 30, 45)).toBe(40) + }) +}) diff --git a/test/min-sub.test.js b/test/min-sub.test.js new file mode 100644 index 0000000..8e96bed --- /dev/null +++ b/test/min-sub.test.js @@ -0,0 +1,11 @@ +import { minSub } from '../src/math-toolbox' + +describe('Subtracts the given amount from the value, but never lets the value go below the specified minimum', () => { + it('Substract 10 from 30 with min 25, expect to it to return 25', () => { + expect(minSub(10, 30, 25)).toBe(25) + }) + + it('Substract 10 from 30 with min 35, expect to it to return 35', () => { + expect(minSub(10, 30, 35)).toBe(35) + }) +})