From d57ee0225c2bfd34776a9c5c6abaf8461a2371d1 Mon Sep 17 00:00:00 2001 From: RomanBurunkov Date: Thu, 16 Jul 2020 11:14:19 +0000 Subject: [PATCH] Formatting utilities --- lib/utilities.js | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/utilities.js b/lib/utilities.js index ced0863..9f12820 100644 --- a/lib/utilities.js +++ b/lib/utilities.js @@ -2,7 +2,7 @@ const fs = require('fs'); const path = require('path'); -const Readable = require('stream').Readable; +const { Readable } = require('stream'); // Parameters for safe file name parsing. const SAFE_FILE_NAME_REGEX = /[^\w-]/g; @@ -27,17 +27,17 @@ const debugLog = (options, msg) => { }; /** - * Generates unique temporary file name like: tmp-5000-156788789789. + * Generates unique temporary file name. e.g. tmp-5000-156788789789. * @param {string} prefix - a prefix for generated unique file name. * @returns {string} */ -const getTempFilename = (prefix) => { +const getTempFilename = (prefix = TEMP_PREFIX) => { tempCounter = tempCounter >= TEMP_COUNTER_MAX ? 1 : tempCounter + 1; - return `${prefix || TEMP_PREFIX}-${tempCounter}-${Date.now()}`; + return `${prefix}-${tempCounter}-${Date.now()}`; }; /** - * isFunc- check if argument is a function. + * isFunc: Checks if argument is a function. * @returns {boolean} - Returns true if argument is a function. */ const isFunc = func => func && func.constructor && func.call && func.apply ? true: false; @@ -60,7 +60,7 @@ const promiseCallback = (resolve, reject) => { * Builds instance options from arguments objects(can't be arrow function). * @returns {Object} - result options. */ -const buildOptions = function(){ +const buildOptions = function() { const result = {}; [...arguments].forEach(options => { if (!options || typeof options !== 'object') return; @@ -107,17 +107,18 @@ const checkAndMakeDir = (fileUploadOptions, filePath) => { // Check whether folder for the file exists. if (!filePath) return false; const parentPath = path.dirname(filePath); - // Create folder if it is not exists. + // Create folder if it doesn't exist. if (!fs.existsSync(parentPath)) fs.mkdirSync(parentPath, { recursive: true }); // Checks folder again and return a results. return fs.existsSync(parentPath); }; /** - * Delete file. + * Deletes a file. * @param {string} file - Path to the file to delete. + * @param {Function} callback */ -const deleteFile = (file, callback) => fs.unlink(file, err => err ? callback(err) : callback()); +const deleteFile = (file, callback) => fs.unlink(file, callback); /** * Copy file via streams @@ -147,15 +148,15 @@ const copyFile = (src, dst, callback) => { }; /** - * moveFile - moves the file from src to dst. + * moveFile: moves the file from src to dst. * Firstly trying to rename the file if no luck copying it to dst and then deleteing src. * @param {string} src - Path to the source file * @param {string} dst - Path to the destination file. * @param {Function} callback - A callback function. */ -const moveFile = (src, dst, callback) => fs.rename(src, dst, err => (!err - ? callback() - : copyFile(src, dst, err => err ? callback(err) : deleteFile(src, callback)) +const moveFile = (src, dst, callback) => fs.rename(src, dst, err => (err + ? copyFile(src, dst, err => err ? callback(err) : deleteFile(src, callback)) + : callback() )); /** @@ -176,7 +177,7 @@ const saveBufferToFile = (buffer, filePath, callback) => { }; // Setup file system writable stream. let fstream = fs.createWriteStream(filePath); - fstream.on('error', error => callback(error)); + fstream.on('error', err => callback(err)); fstream.on('close', () => callback()); // Copy file via piping streams. readStream.pipe(fstream); @@ -252,18 +253,18 @@ const parseFileName = (opts, fileName) => { }; module.exports = { - debugLog, isFunc, + debugLog, + copyFile, // For testing purpose. + moveFile, errorFunc, - promiseCallback, - buildOptions, + deleteFile, // For testing purpose. buildFields, - checkAndMakeDir, - deleteFile, // For testing purpose. - copyFile, // For testing purpose. - moveFile, - saveBufferToFile, + buildOptions, parseFileName, getTempFilename, + promiseCallback, + checkAndMakeDir, + saveBufferToFile, uriDecodeFileName };