]>
git.r.bdr.sh - rbdr/tomato-sauce/blob - lib/util.js
3 const Fs
= require('fs');
4 const Path
= require('path');
7 * Module containing utility functions
15 * Parses a 16 bit number buffer
17 * @function parse16BitBuffer
19 * @param {Array<String>} buffer the buffer to parse
20 * @return {Number} the parsed value
22 parse16BitBuffer(buffer
) {
24 return buffer
[0] * 256 + buffer
[1];
28 * Picks a random element from an array
30 * @function pickRandom
32 * @param {Array} array the array to use
33 * @return {Any} the picked element
37 return array
[Math
.floor(Math
.random() * array
.length
)];
41 * For a gi ven path, requires all of the files and returns an array
42 * with the results. If the directory contains any non-requireable
43 * files, it will fail.
47 * @param {String} path the path where the files are located
48 * @return {Array} the array of all the loaded modules
52 return new Promise((resolve
, reject
) => {
54 Fs
.readdir(path
, (err
, files
) => {
60 const loadedFiles
= [];
62 for (const file
of files
) {
63 const filePath
= Path
.join(path
, file
);
67 loadedFile
= require(filePath
);
73 loadedFiles
.push(loadedFile
);
82 module
.exports
= Util
;