Last active
May 16, 2016 22:01
-
-
Save JDMcKinstry/83fbe7cccfa90a2e8bbaa3b23b676838 to your computer and use it in GitHub Desktop.
This file contains hidden or 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() { | |
/* see also: http://jsfiddle.net/SpYk3/uUyrc/ */ | |
function joinObj() { | |
var obj = [], str = "", ret = [], | |
args = Array.prototype.slice.call(arguments, 0); | |
for (var x in args) switch (typeof args[x]) { | |
case 'object': | |
obj[x] = args[x]; | |
break; | |
case 'string': | |
str = args[x]; | |
break; | |
} | |
for (var y in obj) { | |
var ob = obj[y] | |
for (var z in obj[y]) if (obj[y].hasOwnProperty(z)) switch (typeof obj[y][z]) { | |
case "string": | |
ret.push(obj[y][z]); | |
break; | |
case "object": | |
var a = joinObj(obj[y][z], str); | |
if (a) ret.push(a); | |
break; | |
default: | |
if (obj[y][z]['valueOf']) ret.push(obj[y][z].valueOf()); | |
} | |
} | |
return ret ? ret.join(str) : ""; | |
} | |
// add as window variable | |
window.hasOwnProperty("joinObj")||(window.joinObj=joinObj); | |
// add as method of an object ( exp: obj.join("str"); ) | |
var name = 'join'; | |
function method(str) { return joinObj(this,str); } | |
Object['defineProperty'] && !Object.prototype.hasOwnProperty(name) | |
? Object.defineProperty(Object.prototype, name, { value: method }) : Object.prototype[name] = method; | |
// add as a jQuery extension | |
if (window.hasOwnProperty('jQuery')) { | |
jQuery.joinObj || (jQuery.extend({ | |
joinObj: function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
for (var x in args) if (args[x] instanceof jQuery && args[x][0] && args[x][0] instanceof Element) args[x] = args[x].text(); | |
return joinObj.apply(this, args); | |
} | |
}), | |
jQuery.fn.extend({ | |
joinObj: function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
return jQuery.joinObj.apply(this, args.concat([$(this)])) | |
} | |
})) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment