2023-06-26 18:17:06 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const CustomError = require('../utils/customError');
|
|
|
|
|
|
|
|
// checks if a rev is a legal number
|
|
|
|
// pre-condition is that `rev` is not undefined
|
2024-02-05 21:13:02 +01:00
|
|
|
const checkValidRev = (rev: number|string) => {
|
2023-06-26 18:17:06 +01:00
|
|
|
if (typeof rev !== 'number') {
|
|
|
|
rev = parseInt(rev, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if rev is a number
|
|
|
|
if (isNaN(rev)) {
|
|
|
|
throw new CustomError('rev is not a number', 'apierror');
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure this is not a negative number
|
|
|
|
if (rev < 0) {
|
|
|
|
throw new CustomError('rev is not a negative number', 'apierror');
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure this is not a float value
|
|
|
|
if (!isInt(rev)) {
|
|
|
|
throw new CustomError('rev is a float value', 'apierror');
|
|
|
|
}
|
|
|
|
|
|
|
|
return rev;
|
|
|
|
};
|
|
|
|
|
|
|
|
// checks if a number is an int
|
2024-02-05 21:13:02 +01:00
|
|
|
const isInt = (value:number) => (parseFloat(String(value)) === parseInt(String(value), 10)) && !isNaN(value);
|
2023-06-26 18:17:06 +01:00
|
|
|
|
|
|
|
exports.isInt = isInt;
|
|
|
|
exports.checkValidRev = checkValidRev;
|