Skip to content

Commit d57ee02

Browse files
committed
Formatting utilities
1 parent 1afa527 commit d57ee02

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

lib/utilities.js

+23-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5-
const Readable = require('stream').Readable;
5+
const { Readable } = require('stream');
66

77
// Parameters for safe file name parsing.
88
const SAFE_FILE_NAME_REGEX = /[^\w-]/g;
@@ -27,17 +27,17 @@ const debugLog = (options, msg) => {
2727
};
2828

2929
/**
30-
* Generates unique temporary file name like: tmp-5000-156788789789.
30+
* Generates unique temporary file name. e.g. tmp-5000-156788789789.
3131
* @param {string} prefix - a prefix for generated unique file name.
3232
* @returns {string}
3333
*/
34-
const getTempFilename = (prefix) => {
34+
const getTempFilename = (prefix = TEMP_PREFIX) => {
3535
tempCounter = tempCounter >= TEMP_COUNTER_MAX ? 1 : tempCounter + 1;
36-
return `${prefix || TEMP_PREFIX}-${tempCounter}-${Date.now()}`;
36+
return `${prefix}-${tempCounter}-${Date.now()}`;
3737
};
3838

3939
/**
40-
* isFunc- check if argument is a function.
40+
* isFunc: Checks if argument is a function.
4141
* @returns {boolean} - Returns true if argument is a function.
4242
*/
4343
const isFunc = func => func && func.constructor && func.call && func.apply ? true: false;
@@ -60,7 +60,7 @@ const promiseCallback = (resolve, reject) => {
6060
* Builds instance options from arguments objects(can't be arrow function).
6161
* @returns {Object} - result options.
6262
*/
63-
const buildOptions = function(){
63+
const buildOptions = function() {
6464
const result = {};
6565
[...arguments].forEach(options => {
6666
if (!options || typeof options !== 'object') return;
@@ -107,17 +107,18 @@ const checkAndMakeDir = (fileUploadOptions, filePath) => {
107107
// Check whether folder for the file exists.
108108
if (!filePath) return false;
109109
const parentPath = path.dirname(filePath);
110-
// Create folder if it is not exists.
110+
// Create folder if it doesn't exist.
111111
if (!fs.existsSync(parentPath)) fs.mkdirSync(parentPath, { recursive: true });
112112
// Checks folder again and return a results.
113113
return fs.existsSync(parentPath);
114114
};
115115

116116
/**
117-
* Delete file.
117+
* Deletes a file.
118118
* @param {string} file - Path to the file to delete.
119+
* @param {Function} callback
119120
*/
120-
const deleteFile = (file, callback) => fs.unlink(file, err => err ? callback(err) : callback());
121+
const deleteFile = (file, callback) => fs.unlink(file, callback);
121122

122123
/**
123124
* Copy file via streams
@@ -147,15 +148,15 @@ const copyFile = (src, dst, callback) => {
147148
};
148149

149150
/**
150-
* moveFile - moves the file from src to dst.
151+
* moveFile: moves the file from src to dst.
151152
* Firstly trying to rename the file if no luck copying it to dst and then deleteing src.
152153
* @param {string} src - Path to the source file
153154
* @param {string} dst - Path to the destination file.
154155
* @param {Function} callback - A callback function.
155156
*/
156-
const moveFile = (src, dst, callback) => fs.rename(src, dst, err => (!err
157-
? callback()
158-
: copyFile(src, dst, err => err ? callback(err) : deleteFile(src, callback))
157+
const moveFile = (src, dst, callback) => fs.rename(src, dst, err => (err
158+
? copyFile(src, dst, err => err ? callback(err) : deleteFile(src, callback))
159+
: callback()
159160
));
160161

161162
/**
@@ -176,7 +177,7 @@ const saveBufferToFile = (buffer, filePath, callback) => {
176177
};
177178
// Setup file system writable stream.
178179
let fstream = fs.createWriteStream(filePath);
179-
fstream.on('error', error => callback(error));
180+
fstream.on('error', err => callback(err));
180181
fstream.on('close', () => callback());
181182
// Copy file via piping streams.
182183
readStream.pipe(fstream);
@@ -252,18 +253,18 @@ const parseFileName = (opts, fileName) => {
252253
};
253254

254255
module.exports = {
255-
debugLog,
256256
isFunc,
257+
debugLog,
258+
copyFile, // For testing purpose.
259+
moveFile,
257260
errorFunc,
258-
promiseCallback,
259-
buildOptions,
261+
deleteFile, // For testing purpose.
260262
buildFields,
261-
checkAndMakeDir,
262-
deleteFile, // For testing purpose.
263-
copyFile, // For testing purpose.
264-
moveFile,
265-
saveBufferToFile,
263+
buildOptions,
266264
parseFileName,
267265
getTempFilename,
266+
promiseCallback,
267+
checkAndMakeDir,
268+
saveBufferToFile,
268269
uriDecodeFileName
269270
};

0 commit comments

Comments
 (0)