2016-11-22 16:03:46 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2016-01-26 19:29:12 -05:00
|
|
|
/**
|
|
|
|
* Various internal helper functions
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
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) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(Buffer.from(data).toString());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:53:16 -04:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Wrap String.prototype.trim in a way that is easily mappable
|
|
|
|
*
|
|
|
|
* @param {String} str - The string to trim
|
|
|
|
* @return {String} - The trimmed string
|
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
static stringTrim (str) {
|
|
|
|
return str.trim();
|
|
|
|
}
|
|
|
|
|
2014-10-20 16:56:45 -04:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* 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
|
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
static type (o) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const type = Object.prototype.toString.call(o).slice(8, -1).toLowerCase();
|
2014-10-23 10:53:16 -04:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
// handle NaN and Infinity
|
|
|
|
if (type === 'number') {
|
|
|
|
if (isNaN(o)) {
|
|
|
|
return 'nan';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isFinite(o)) {
|
|
|
|
return 'infinity';
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
return type;
|
2016-11-22 16:03:46 -05:00
|
|
|
}
|
|
|
|
|
2014-10-20 16:56:45 -04:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Determine whether an object is scalar
|
|
|
|
*
|
|
|
|
* @param {mixed} obj - Object to test
|
2018-02-09 17:29:26 -05:00
|
|
|
* @return {boolean} - Is object scalar
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
static isScalar (obj) {
|
2018-02-09 17:29:26 -05:00
|
|
|
return ['string', 'number', 'boolean'].includes(Helpers.type(obj));
|
2016-11-22 16:03:46 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 17:08:56 -05:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* 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
|
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
static arrayPluck (arr, key) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const output = [];
|
2014-11-05 17:08:56 -05:00
|
|
|
|
|
|
|
// Empty case
|
2015-12-08 10:40:52 -05:00
|
|
|
if (arr.length === 0) {
|
|
|
|
return output;
|
|
|
|
}
|
2014-11-05 17:08:56 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
arr.forEach(obj => {
|
2016-11-22 16:03:46 -05:00
|
|
|
if (!Helpers.isUndefined(obj[key])) {
|
2014-11-05 17:08:56 -05:00
|
|
|
output.push(obj[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
2016-11-22 16:03:46 -05:00
|
|
|
}
|
|
|
|
|
2014-11-05 17:08:56 -05:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* 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
|
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
static regexInArray (arr, pattern) {
|
2014-11-05 17:08:56 -05:00
|
|
|
// Empty case(s)
|
2016-11-22 16:03:46 -05:00
|
|
|
if (!Helpers.isArray(arr) || arr.length === 0) {
|
2015-12-07 17:03:36 -05:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-05 17:08:56 -05:00
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
const l = arr.length;
|
|
|
|
for (let i = 0; i < l; i++) {
|
2014-11-05 17:08:56 -05:00
|
|
|
// Short circuit if any items match
|
2015-12-07 17:03:36 -05:00
|
|
|
if (pattern.test(arr[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-05 17:08:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-11-22 16:03:46 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 13:01:31 -05:00
|
|
|
/**
|
2018-02-09 17:29:26 -05:00
|
|
|
* Make the first constter of the string uppercase
|
2015-12-03 20:43:42 -05:00
|
|
|
*
|
|
|
|
* @param {String} str - The string to modify
|
|
|
|
* @return {String} - The modified string
|
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
static upperCaseFirst (str) {
|
2015-12-02 13:01:31 -05:00
|
|
|
str += '';
|
2018-02-09 17:29:26 -05:00
|
|
|
const first = str.charAt(0).toUpperCase();
|
2015-12-02 13:01:31 -05:00
|
|
|
return first + str.substr(1);
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
2016-11-22 16:03:46 -05:00
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
|
|
|
|
// Define an 'is' method for each type
|
2018-02-09 17:29:26 -05:00
|
|
|
const types = [
|
2015-12-07 17:03:36 -05:00
|
|
|
'Null',
|
|
|
|
'Undefined',
|
|
|
|
'Object',
|
|
|
|
'Array',
|
|
|
|
'String',
|
|
|
|
'Number',
|
|
|
|
'Boolean',
|
|
|
|
'Function',
|
|
|
|
'RegExp',
|
|
|
|
'NaN',
|
2016-11-22 16:03:46 -05:00
|
|
|
'Infinite',
|
|
|
|
'Promise'
|
2015-12-07 17:03:36 -05:00
|
|
|
];
|
2015-12-03 20:43:42 -05:00
|
|
|
types.forEach(t => {
|
2014-10-23 13:50:11 -04:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Determine whether a variable is of the type specified in the
|
|
|
|
* function name, eg isNumber
|
|
|
|
*
|
2016-03-09 15:05:38 -05:00
|
|
|
* Types available are Null, Undefined, Object, Array, String, Number,
|
2016-11-22 16:03:46 -05:00
|
|
|
* Boolean, Function, RegExp, NaN, Infinite, Promise
|
2015-12-03 20:43:42 -05:00
|
|
|
*
|
2016-01-26 19:29:12 -05:00
|
|
|
* @private
|
|
|
|
* @param {mixed} o - The object to check its type
|
|
|
|
* @return {Boolean} - If the type matches
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-22 16:03:46 -05:00
|
|
|
Helpers[`is${t}`] = function (o) {
|
2015-12-07 17:03:36 -05:00
|
|
|
if (t.toLowerCase() === 'infinite') {
|
|
|
|
t = 'infinity';
|
|
|
|
}
|
2014-10-28 16:46:48 -04:00
|
|
|
|
2016-11-22 16:03:46 -05:00
|
|
|
return Helpers.type(o) === t.toLowerCase();
|
2016-09-14 16:50:32 -04:00
|
|
|
};
|
2014-10-23 10:53:16 -04:00
|
|
|
});
|
|
|
|
|
2016-11-22 16:03:46 -05:00
|
|
|
module.exports = Helpers;
|