You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
57 lines
1.3 KiB
import testRange from './testRange'; |
|
import toNumber from '../toNumber'; |
|
|
|
const buildNumberTestBatch: BuildInputTestBatchFunction = ( |
|
inputName, |
|
onSuccess, |
|
{ isRequired, onFinishBatch, ...defaults } = {}, |
|
onIntTestFailure?, |
|
onFloatTestFailure?, |
|
onRangeTestFailure?, |
|
) => { |
|
const tests: InputTest[] = []; |
|
|
|
if (onIntTestFailure) { |
|
tests.push({ |
|
onFailure: (...args) => { |
|
onIntTestFailure(<>{inputName} must be a valid integer.</>, ...args); |
|
}, |
|
test: ({ value }) => Number.isSafeInteger(toNumber(value)), |
|
}); |
|
} else if (onFloatTestFailure) { |
|
tests.push({ |
|
onFailure: (...args) => { |
|
onFloatTestFailure( |
|
<>{inputName} must be a valid floating-point number.</>, |
|
...args, |
|
); |
|
}, |
|
test: ({ value }) => Number.isFinite(toNumber(value, 'parseFloat')), |
|
}); |
|
} |
|
|
|
if (onRangeTestFailure) { |
|
tests.push({ |
|
onFailure: (...args) => { |
|
const { displayMax, displayMin } = args[0]; |
|
|
|
onRangeTestFailure( |
|
<> |
|
{inputName} is expected to be between {displayMin} and {displayMax}. |
|
</>, |
|
...args, |
|
); |
|
}, |
|
test: testRange, |
|
}); |
|
} |
|
|
|
return { |
|
defaults: { ...defaults, onSuccess }, |
|
isRequired, |
|
onFinishBatch, |
|
tests, |
|
}; |
|
}; |
|
|
|
export default buildNumberTestBatch;
|
|
|