forked from looker/viz-radial_gauge-marketplace
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: some unit tests for some more utility functions. (#12)
- Loading branch information
Showing
6 changed files
with
95 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,19 @@ | ||
/** | ||
* Maps a number between a certain range offset by minAllowed. | ||
* | ||
* @param {number} n The number to map to a range. | ||
* @param {number} minAllowed The minimum allowed for the range. | ||
* @param {number} maxAllowed The maximum allowed for the range. | ||
* @param {number} min The minimum value for the number. | ||
* @param {number} max The maximum value for the number. | ||
* @return {number} A number within the projected range. | ||
*/ | ||
export function mapBetween(n, minAllowed, maxAllowed, min, max) { | ||
// TODO: This function has been refactored and moved into this file as is | ||
// It has yet to be checked for accuracy and validation has to be added. | ||
const numerator = ((maxAllowed - minAllowed) * (n - min)); | ||
const denominator = (max - min); | ||
return ( | ||
numerator / denominator + minAllowed | ||
); | ||
} |
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,26 @@ | ||
import { mapBetween } from './math'; | ||
|
||
describe('mapBetween', () => { | ||
test('should map the number within the allowable range', () => { | ||
expect(mapBetween(4, 0, 5, 1, 3)).toEqual(7.5); | ||
|
||
expect(mapBetween(4, 2, 5, 1, 3)).toEqual(6.5); | ||
expect(mapBetween(4, 0, 4, 1, 3)).toEqual(6); | ||
expect(mapBetween(4, 0, 5, 4, 3)).toEqual(0); | ||
}); | ||
|
||
test('should map the number within the allowable range', () => { | ||
expect(mapBetween(1, 0, 5, 1, 3)).toEqual(0); | ||
expect(mapBetween(2, 0, 5, 1, 3)).toEqual(2.5); | ||
expect(mapBetween(3, 0, 5, 1, 3)).toEqual(5); | ||
expect(mapBetween(4, 0, 5, 1, 3)).toEqual(7.5); | ||
expect(mapBetween(5, 0, 5, 1, 3)).toEqual(10); | ||
}); | ||
|
||
// FIXME: Are we sure this function maps a number within an allowable range correctly? | ||
test('should map the number within the allowable range (bad cases?)', () => { | ||
expect(mapBetween(-2, 0, 5, 1, 3)).toEqual(-7.5); | ||
expect(mapBetween(-1, 0, 5, 1, 3)).toEqual(-5); | ||
expect(mapBetween(0, 0, 5, 1, 3)).toEqual(-2.5); | ||
}); | ||
}); |
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
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