Skip to content

Commit

Permalink
fix: careful now
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 24, 2024
1 parent 7816ad5 commit 9ccd713
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/utils/number-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { clampToRange } from '../../utils/number-utils'
import { logger } from '../../utils/logger'

jest.mock('../../utils/logger', () => ({
logger: {
warn: jest.fn(),
},
}))

describe('number-utils', () => {
describe('clampToRange', () => {
Expand All @@ -13,5 +20,10 @@ describe('number-utils', () => {
const result = clampToRange(value, min, max, 'Test Label')
expect(result).toBe(expected)
})

it('logs a warning when min is greater than max', () => {
expect(clampToRange(50, 100, 10, 'Test Label')).toBe(10)
expect(logger.warn).toHaveBeenCalledWith('min cannot be greater than max.')
})
})
})
5 changes: 5 additions & 0 deletions src/utils/number-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { isNumber } from './type-utils'
import { logger } from './logger'

export function clampToRange(value: unknown, min: number, max: number, label?: string): number {
if (min > max) {
logger.warn('min cannot be greater than max.')
min = max
}

if (!isNumber(value)) {
label && logger.warn(label + ' must be a number. Defaulting to max value:' + max)
return max
Expand Down

0 comments on commit 9ccd713

Please sign in to comment.