From b33bdd338318fe83d0cef100cff6ac8eb62b3f8d Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sun, 25 Jun 2023 18:11:00 +0200 Subject: [PATCH] refactor: move checkValidRev and isInt into separate file --- src/node/db/API.js | 29 +--------------------------- src/node/utils/checkValidRev.js | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 src/node/utils/checkValidRev.js diff --git a/src/node/db/API.js b/src/node/db/API.js index 9b2ecadc7..58a1573cc 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -33,6 +33,7 @@ const exportTxt = require('../utils/ExportTxt'); const importHtml = require('../utils/ImportHtml'); const cleanText = require('./Pad').cleanText; const PadDiff = require('../utils/padDiff'); +const { checkValidRev, isInt } = require('../utils/checkValidRev'); /* ******************** * GROUP FUNCTIONS **** @@ -822,9 +823,6 @@ exports.getStats = async () => { ** INTERNAL HELPER FUNCTIONS * **************************** */ -// checks if a number is an int -const isInt = (value) => (parseFloat(value) === parseInt(value, 10)) && !isNaN(value); - // gets a pad safe const getPadSafe = async (padID, shouldExist, text, authorId = '') => { // check if padID is a string @@ -854,31 +852,6 @@ const getPadSafe = async (padID, shouldExist, 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 const checkGroupPad = (padID, field) => { // ensure this is a group pad diff --git a/src/node/utils/checkValidRev.js b/src/node/utils/checkValidRev.js new file mode 100644 index 000000000..862c6a2bd --- /dev/null +++ b/src/node/utils/checkValidRev.js @@ -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;