Skip to content

Commit

Permalink
feat: Added maxAdd and minSub
Browse files Browse the repository at this point in the history
  • Loading branch information
terkelg committed Oct 21, 2016
1 parent 90fff42 commit f1ba3b6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/math-toolbox.js
Expand Up @@ -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'
13 changes: 13 additions & 0 deletions 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 }
13 changes: 13 additions & 0 deletions 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 }
11 changes: 11 additions & 0 deletions 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)
})
})
11 changes: 11 additions & 0 deletions 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)
})
})

0 comments on commit f1ba3b6

Please sign in to comment.