Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |