Skip to content

Commit

Permalink
utils: add isUndefined and alternate to merge, defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlcaDesign committed Sep 9, 2016
1 parent 73ca972 commit 12d7e82
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var self = module.exports = {
// Return the second value if the first value is undefined..
get: (obj1, obj2) => { return typeof obj1 === "undefined" ? obj2 : obj1; },
get: (obj1, obj2) => { return self.isUndefined(obj1) ? obj2 : obj1; },

// Value is a boolean..
isBoolean: (obj) => { return obj === true || obj === false || toString.call(obj) === "[object Boolean]"; },
Expand All @@ -17,6 +17,9 @@ var self = module.exports = {
// Value is null..
isNull: (obj) => { return obj === null; },

// Value is undefined..
isUndefined: (obj) => { return typeof obj === "undefined"; },

// Value is a regex..
isRegex: (str) => { return /[\|\\\^\$\*\+\?\:\#]/.test(str); },

Expand Down Expand Up @@ -111,6 +114,17 @@ var self = module.exports = {
return obj1;
},

// Merge two objects without replacing..
defaults: (obj1, obj2) => {
for (var p in obj2) {
try {
if (obj2[p].constructor == Object) { obj1[p] = self.defaults(obj1[p], obj2[p]); }
else if(self.isUndefined(obj1[p])) { obj1[p] = obj2[p]; }
} catch(e) { obj1[p] = obj2[p]; }
}
return obj1;
},

// Split a line but don't cut a word in half..
splitLine: (input, length) => {
var lastSpace = input.substring(0, length).lastIndexOf(" ");
Expand Down

0 comments on commit 12d7e82

Please sign in to comment.