Skip to content

Commit

Permalink
Formatting utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanBurunkov committed Jul 16, 2020
1 parent 1afa527 commit d57ee02
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions lib/utilities.js
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
));

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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
};

0 comments on commit d57ee02

Please sign in to comment.