-
Notifications
You must be signed in to change notification settings - Fork 140
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
1 parent
ccc8f24
commit 2bf62b4
Showing
6 changed files
with
57 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { clampToRange } from '../../utils/number-utils' | ||
|
||
describe('number-utils', () => { | ||
describe('clampToRange', () => { | ||
it.each([ | ||
// [value, result, min, max, expected result, test description] | ||
['returns max when value is not a number', null, 10, 100, 100], | ||
['returns max when value is not a number', 'not-a-number', 10, 100, 100], | ||
['returns max when value is greater than max', 150, 10, 100, 100], | ||
['returns min when value is less than min', 5, 10, 100, 10], | ||
['returns the value when it is within the range', 50, 10, 100, 50], | ||
])('%s', (_description, value, min, max, expected) => { | ||
const result = clampToRange(value, min, max, 'Test Label') | ||
expect(result).toBe(expected) | ||
}) | ||
}) | ||
}) |
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
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,17 @@ | ||
import { isNumber } from './type-utils' | ||
import { logger } from './logger' | ||
|
||
export function clampToRange(value: unknown, min: number, max: number, label?: string): number { | ||
if (!isNumber(value)) { | ||
label && logger.warn(label + ' must be a number. Defaulting to max value:' + max) | ||
return max | ||
} else if (value > max) { | ||
label && logger.warn(label + ' cannot be greater than max: ' + max + '. Using max value instead.') | ||
return max | ||
} else if (value < min) { | ||
label && logger.warn(label + ' cannot be less than min: ' + min + '. Using min value instead.') | ||
return min | ||
} else { | ||
return value | ||
} | ||
} |