Last active
August 29, 2015 14:00
-
-
Save JasonCust/11261429 to your computer and use it in GitHub Desktop.
Collection of simple helper functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clone(object) { | |
switch (getObjectType(object)){ | |
case 'Object': | |
object = Object.keys(object).reduce(function(obj, key) { | |
obj[key] = clone(object[key]); | |
return obj; | |
}, {}); | |
break; | |
case 'Array': | |
object = object.map(function (value) { | |
return clone(value); | |
}); | |
break; | |
case 'Date': | |
object = new Date(object); | |
break; | |
case 'RegExp': | |
object = new RegExp(object); | |
break; | |
} | |
return object; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getObjType(obj) { | |
return Object.prototype.toString.call(obj).slice(8,-1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function merge() { | |
var objects = Array.prototype.slice.call(arguments, 0).reverse(); | |
return objects.reduce(function (seedObj, object) { | |
return Object.keys(seedObj).concat(Object.keys(object)).reduce(function (tempObj, key) { | |
tempObj[key] = seedObj.hasOwnProperty(key) ? seedObj[key] : object[key]; | |
return tempObj; | |
}, {}); | |
}, objects.shift()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function objectIsType(obj, type) { | |
return (getObjType(obj) === type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment