Created
May 16, 2016 22:08
-
-
Save JDMcKinstry/8d3ccae2868e7d6dd265bb45663828f8 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
/* String.prototype.matchUrl */ | |
;(function() { // quick and ez access to regex match on url string | |
function matchUrl(url) { | |
var regMatch = void 0, | |
araLabels = "url scheme authority path query fragment".split(" "), | |
regUrl = /^(([^\:\/\?\#]+)\:)?(\/\/([^\/\?\#]*))?([^\?\#]*)(\?([^\#]*))?(\#(.*))?/, | |
retVal = { | |
url: null, | |
scheme: null, authority: null, | |
path: null, query: null, | |
fragment: null, valid: null | |
}; | |
"string" === typeof url && "" != url && (regMatch = url.match(regUrl)); | |
if ("object" === typeof regMatch) for (x in regMatch) araLabels[x] && (retVal[araLabels[x]] = regMatch[x]); | |
retVal.scheme && retVal.authority && (retVal.valid = !0); | |
return retVal | |
}; | |
// add as window variable | |
window.hasOwnProperty("matchUrl")||(window.matchUrl=matchUrl); | |
// add as method of a String ( exp: "string".matchUrl(); ) | |
var name = 'matchUrl'; | |
function method() { return matchUrl(this.valueOf()); } | |
Object['defineProperty'] && !String.prototype.hasOwnProperty(name) | |
? Object.defineProperty(String.prototype, name, { value: method }) : String.prototype[name] = method; | |
// add as a jQuery extension | |
if (window.hasOwnProperty('jQuery')) { | |
jQuery.matchUrl || (jQuery.extend({ | |
matchUrl: 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 matchUrl.apply(this, args); | |
} | |
}), | |
jQuery.fn.extend({ | |
matchUrl: function() { | |
var args = Array.prototype.slice.call(arguments, 0); | |
return jQuery.matchUrl.apply(this, args.concat([jQuery(this)])) | |
} | |
})) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment