refactor: move checkValidRev and isInt into separate file

This commit is contained in:
webzwo0i 2023-06-25 18:11:00 +02:00
parent 1d85bff12c
commit b33bdd3383
2 changed files with 35 additions and 28 deletions

View file

@ -33,6 +33,7 @@ const exportTxt = require('../utils/ExportTxt');
const importHtml = require('../utils/ImportHtml'); const importHtml = require('../utils/ImportHtml');
const cleanText = require('./Pad').cleanText; const cleanText = require('./Pad').cleanText;
const PadDiff = require('../utils/padDiff'); const PadDiff = require('../utils/padDiff');
const { checkValidRev, isInt } = require('../utils/checkValidRev');
/* ******************** /* ********************
* GROUP FUNCTIONS **** * GROUP FUNCTIONS ****
@ -822,9 +823,6 @@ exports.getStats = async () => {
** INTERNAL HELPER FUNCTIONS * ** INTERNAL HELPER FUNCTIONS *
**************************** */ **************************** */
// checks if a number is an int
const isInt = (value) => (parseFloat(value) === parseInt(value, 10)) && !isNaN(value);
// gets a pad safe // gets a pad safe
const getPadSafe = async (padID, shouldExist, text, authorId = '') => { const getPadSafe = async (padID, shouldExist, text, authorId = '') => {
// check if padID is a string // check if padID is a string
@ -854,31 +852,6 @@ const getPadSafe = async (padID, shouldExist, text, authorId = '') => {
return padManager.getPad(padID, text, authorId); return padManager.getPad(padID, text, authorId);
}; };
// checks if a rev is a legal number
// pre-condition is that `rev` is not undefined
const checkValidRev = (rev) => {
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 padID is part of a group // checks if a padID is part of a group
const checkGroupPad = (padID, field) => { const checkGroupPad = (padID, field) => {
// ensure this is a group pad // ensure this is a group pad

View file

@ -0,0 +1,34 @@
'use strict';
const CustomError = require('../utils/customError');
// checks if a rev is a legal number
// pre-condition is that `rev` is not undefined
const checkValidRev = (rev) => {
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
const isInt = (value) => (parseFloat(value) === parseInt(value, 10)) && !isNaN(value);
exports.isInt = isInt;
exports.checkValidRev = checkValidRev;