X-Git-Url: https://git.r.bdr.sh/rbdr/tomato-sauce/blobdiff_plain/c7b4bd19a006d61c3b4979e884370d123cec7524..a0666be3ab58ed83ad6d622cfe2b8293c40dffbb:/lib/util.js?ds=sidebyside diff --git a/lib/util.js b/lib/util.js index 3234d3e..4c8c3c1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -3,48 +3,80 @@ const Fs = require('fs'); const Path = require('path'); -// Module containing utility functions. +/** + * Module containing utility functions + * + * @name Util + * @type Object + */ const Util = { - // Parses a 16 bit number buffer. - parse16BitBuffer: function (buffer) { - return buffer[0] * 256 + buffer[1]; - }, - - // Picks a random element from an array. - pickRandom: function (array) { - return array[Math.floor(Math.random() * array.length)]; - }, - - // For a given path, requires all of the files and returns an array - // with the results. If the directory contains any non-requireable file, - // it will fail. - loadFiles: function (path) { - return new Promise(function (resolve, reject) { - Fs.readdir(path, function (err, files) { - if (err) { - return reject(err); - } - - let loadedFiles = []; - - for (let file of files) { - let filePath = Path.join(path, file); - let loadedFile; - - try { - loadedFile = require(filePath); - } catch (err) { - return reject(err); - } - - loadedFiles.push(loadedFile); - } - - resolve(loadedFiles); - }); - }); - } + /** + * Parses a 16 bit number buffer + * + * @function parse16BitBuffer + * @memberof Util + * @param {Array} buffer the buffer to parse + * @return {Number} the parsed value + */ + parse16BitBuffer(buffer) { + + return buffer[0] * 256 + buffer[1]; + }, + + /** + * Picks a random element from an array + * + * @function pickRandom + * @memberof Util + * @param {Array} array the array to use + * @return {Any} the picked element + */ + pickRandom(array) { + + return array[Math.floor(Math.random() * array.length)]; + }, + + /** + * For a gi ven path, requires all of the files and returns an array + * with the results. If the directory contains any non-requireable + * files, it will fail. + * + * @function loadFiles + * @memberof Util + * @param {String} path the path where the files are located + * @return {Array} the array of all the loaded modules + */ + loadFiles(path) { + + return new Promise((resolve, reject) => { + + Fs.readdir(path, (err, files) => { + + if (err) { + return reject(err); + } + + const loadedFiles = []; + + for (const file of files) { + const filePath = Path.join(path, file); + let loadedFile; + + try { + loadedFile = require(filePath); + } + catch (err) { + return reject(err); + } + + loadedFiles.push(loadedFile); + } + + resolve(loadedFiles); + }); + }); + } }; module.exports = Util;