Skip to content

Commit

Permalink
feat(math): mapRange
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Strauß committed Jan 23, 2018
1 parent ed4f320 commit b435668
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/math/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
goog.module('clulib.math');

/**
* Maps a value from one range to another range.
*
* @param {number} x
* @param {number} inMin
* @param {number} inMax
* @param {number} outMin
* @param {number} outMax
* @returns {number}
*/
function mapRange (x, inMin, inMax, outMin, outMax) {
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}

exports = {mapRange};
22 changes: 22 additions & 0 deletions test/math/math_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
goog.module('test.clulib.math');

const {mapRange} = goog.require('clulib.math');

exports = function () {
describe('clulib.math', () => {
describe('mapRange', () => {
it('should map a value from one range to another range', () => {
expect(mapRange(1, 0, 10, 0, 100)).toBe(10);
expect(mapRange(5, 0, 10, 0, 100)).toBe(50);

expect(mapRange(11, 0, 10, 0, 100)).toBe(110);
expect(mapRange(15, 0, 10, 0, 100)).toBe(150);

expect(mapRange(-1, 0, 10, 0, 100)).toBe(-10);
expect(mapRange(-5, 0, 10, 0, 100)).toBe(-50);

expect(mapRange(-5, -10, 0, -100, 0)).toBe(-50);
});
});
});
};
2 changes: 2 additions & 0 deletions test/test_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ const cmMain = goog.require('test.clulib.cm');
const domMain = goog.require('test.clulib.dom');
const functionsMain = goog.require('test.clulib.functions');
const httpRequestMain = goog.require('test.clulib.net.http_request');
const mathMain = goog.require('test.clulib.math');

arrayMain();
asyncCompleterMain();
cmMain();
domMain();
functionsMain();
httpRequestMain();
mathMain();

0 comments on commit b435668

Please sign in to comment.