All files / lib Helpers.js

97.44% Statements 38/39
95% Branches 19/20
100% Functions 12/12
97.44% Lines 38/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 1707x                             4x 4x 4x     4x                       571x                       5903x     5903x 434x 2x     432x 2x       5899x                   1451x                     195x     195x 101x     94x 166x 165x       94x                         98x 2x     96x 96x   109x 90x       6x                   2x 2x 2x         7x                           7x                       84x 4447x 1x     4447x       7x  
const fs = require('fs');
 
/**
 * Various internal helper functions
 *
 * @private
 */
class Helpers {
	/**
	 * Get the contents of a file
	 *
	 * @param {string} file - The path to the file
	 * @return {Promise<string>} - Promise resolving to the contents of the file
	 */
	static readFile (file) {
		return new Promise((resolve, reject) => {
			fs.readFile(file, (err, data) => {
				Iif (err) {
					return reject(err);
				}
				return resolve(Buffer.from(data).toString());
			});
		});
	}
 
	/**
	 * Wrap String.prototype.trim in a way that is easily mappable
	 *
	 * @param {String} str - The string to trim
	 * @return {String} - The trimmed string
	 */
	static stringTrim (str) {
		return str.trim();
	}
 
	/**
	 * Get the type of the variable passed
	 *
	 * @see https://techblog.badoo.com/blog/2013/11/01/type-checking-in-javascript/
	 * @see http://toddmotto.com/understanding-javascript-types-and-reliable-type-checking/
	 * @param {mixed} o - Object to type check
	 * @return {String} - Type of the object
	 */
	static type (o) {
		const type = Object.prototype.toString.call(o).slice(8, -1).toLowerCase();
 
		// handle NaN and Infinity
		if (type === 'number') {
			if (isNaN(o)) {
				return 'nan';
			}
 
			if (!isFinite(o)) {
				return 'infinity';
			}
		}
 
		return type;
	}
 
	/**
	 * Determine whether an object is scalar
	 *
	 * @param {mixed} obj - Object to test
	 * @return {boolean} - Is object scalar
	 */
	static isScalar (obj) {
		return ['string', 'number', 'boolean'].includes(Helpers.type(obj));
	}
 
	/**
	 * Get a list of values with a common key from an array of objects
	 *
	 * @param {Array} arr - The array of objects to search
	 * @param {String} key - The key of the object to get
	 * @return {Array} - The new array of plucked values
	 */
	static arrayPluck (arr, key) {
		const output = [];
 
		// Empty case
		if (arr.length === 0) {
			return output;
		}
 
		arr.forEach(obj => {
			if (!Helpers.isUndefined(obj[key])) {
				output.push(obj[key]);
			}
		});
 
		return output;
	}
 
	/**
	 * Determine if a value matching the passed regular expression is
	 * in the passed array
	 *
	 * @param {Array} arr - The array to search
	 * @param {RegExp} pattern - The pattern to match
	 * @return {Boolean} - If an array item matches the pattern
	 */
	static regexInArray (arr, pattern) {
		// Empty case(s)
		if (!Helpers.isArray(arr) || arr.length === 0) {
			return false;
		}
 
		const l = arr.length;
		for (let i = 0; i < l; i++) {
			// Short circuit if any items match
			if (pattern.test(arr[i])) {
				return true;
			}
		}
 
		return false;
	}
 
	/**
	 * Make the first constter of the string uppercase
	 *
	 * @param {String} str - The string to modify
	 * @return {String} - The modified string
	 */
	static upperCaseFirst (str) {
		str += '';
		const first = str.charAt(0).toUpperCase();
		return first + str.substr(1);
	}
}
 
// Define an 'is' method for each type
const types = [
	'Null',
	'Undefined',
	'Object',
	'Array',
	'String',
	'Number',
	'Boolean',
	'Function',
	'RegExp',
	'NaN',
	'Infinite',
	'Promise'
];
types.forEach(t => {
	/**
	 * Determine whether a variable is of the type specified in the
	 * function name, eg isNumber
	 *
	 * Types available are Null, Undefined, Object, Array, String, Number,
	 * Boolean, Function, RegExp, NaN, Infinite, Promise
	 *
	 * @private
	 * @param {mixed} o - The object to check its type
	 * @return {Boolean} - If the type matches
	 */
	Helpers[`is${t}`] = function (o) {
		if (t.toLowerCase() === 'infinite') {
			t = 'infinity';
		}
 
		return Helpers.type(o) === t.toLowerCase();
	};
});
 
module.exports = Helpers;