node-query/lib/helpers.js

145 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-12-07 17:03:36 -05:00
'use strict';
2014-10-20 16:56:45 -04:00
/**
* Various internal helper functions
*
* @private
*/
let helpers = {
2014-10-23 10:53:16 -04:00
/**
* Wrap String.prototype.trim in a way that is easily mappable
*
* @param {String} str - The string to trim
* @return {String} - The trimmed string
*/
stringTrim: str => str.trim(),
2014-10-20 16:56:45 -04: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
*/
type: o => {
2015-12-07 17:03:36 -05:00
let 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;
2014-10-20 16:56:45 -04:00
},
/**
* Determine whether an object is scalar
*
* @param {mixed} obj - Object to test
* @return {bool} - Is object scalar
*/
isScalar: obj => {
let scalar = ['string', 'number', 'boolean'];
return scalar.indexOf(helpers.type(obj)) !== -1;
},
/**
* 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
*/
arrayPluck: (arr, key) => {
let output = [];
// Empty case
2015-12-08 10:40:52 -05:00
if (arr.length === 0) {
return output;
}
arr.forEach(obj => {
2015-12-07 17:03:36 -05:00
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
*/
regexInArray: (arr, pattern) => {
// Empty case(s)
2015-12-07 17:03:36 -05:00
if (! helpers.isArray(arr) || arr.length === 0) {
return false;
}
let i, l = arr.length;
2015-12-07 17:03:36 -05:00
for (i = 0; i < l; i++) {
// Short circuit if any items match
2015-12-07 17:03:36 -05:00
if (pattern.test(arr[i])) {
return true;
}
}
return false;
},
/**
* Make the first letter of the string uppercase
*
* @param {String} str - The string to modify
* @return {String} - The modified string
*/
upperCaseFirst: str => {
str += '';
let first = str.charAt(0).toUpperCase();
return first + str.substr(1);
2015-12-07 17:03:36 -05:00
},
2014-10-20 16:56:45 -04:00
};
2014-10-23 10:53:16 -04:00
// Define an 'is' method for each type
2015-12-07 17:03:36 -05:00
let types = [
'Null',
'Undefined',
'Object',
'Array',
'String',
'Number',
'Boolean',
'Function',
'RegExp',
'NaN',
'Infinite',
];
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 and Infinite
*
* @private
* @param {mixed} o - The object to check its type
* @return {Boolean} - If the type matches
*/
2015-12-07 17:03:36 -05:00
helpers[`is${t}`] = function(o) {
if (t.toLowerCase() === 'infinite') {
t = 'infinity';
}
return helpers.type(o) === t.toLowerCase();
};
2014-10-23 10:53:16 -04:00
});
module.exports = helpers;