add jsdoc linting, address issues
[squeep-mystery-box] / lib / common.js
1 'use strict';
2
3 const path = require('path');
4 const { randomBytes } = require('crypto');
5 const { promisify } = require('util');
6 const randomBytesAsync = promisify(randomBytes);
7
8
9 /**
10 * Return a function which combines a part of the filename with a scope, for use in logging.
11 * @param {string} filename
12 */
13 const fileScope = (filename) => {
14 let fScope = path.basename(filename, '.js');
15 if (fScope === 'index') {
16 fScope = path.basename(path.dirname(filename));
17 }
18 return (scope) => `${fScope}:${scope}`;
19 }
20
21
22 /**
23 * Return an array containing x if x is something and not an array
24 * @param {*} x
25 */
26 const ensureArray = (x) => {
27 if (x === undefined) {
28 return [];
29 }
30 if (!Array.isArray(x)) {
31 return Array(x);
32 }
33 return x;
34 };
35
36
37 module.exports = {
38 ensureArray,
39 fileScope,
40 randomBytesAsync,
41 };