utilsxo
Version:
A small, portable library full of utilities, functions, and neat tricks for your project/application
52 lines (51 loc) • 1.53 kB
JavaScript
/**
* Created by Curtis Gervais on 4/16/2017.
* License: MIT
*
* @desc Misc tools for Utils X module
*/
module.exports = {
count: function(mixedVar, mode) {
var key, cnt=0;
if(mixedVar === null || typeof(mixedVar) === 'undefined') {
return 0;
}
else if(mixedVar.constructor !== Array && mixedVar.constructor !== Object) {
return 1;
}
if(mode === 'COUNT_RECURSIVE') {
mode = 1;
}
if(mode !== 1) {
mode = 0;
}
for(key in mixedVar) {
if(mixedVar.hasOwnProperty(key)) {
cnt++;
if(mode === 1 && mixedVar[key] && (mixedVar[key].constructor === Array || mixedVar[key].constructor === Object)) {
cnt += this.count(mixedVar[key], 1);
}
}
}
return cnt;
},
create_function: function(args, code) {
try {
return Function.apply(null, args.split(',').concat(code));
} catch(e) {
return false;
}
},
index: function(s, sep) {
return (s + '').indexOf(sep);
},
lastIndex: function(s, sep) {
return parseInt(s.lastIndexOf(sep), 10) >> 0;
},
capwords: function(str) {
var pattern = /^([a-z\u00E0-\u00FC])|\s+([a-z\u00E0-\u00FC])/g;
return (str + '').replace(pattern, function($1) {
return $1.toUpperCase();
});
}
};