Skip to content

Instantly share code, notes, and snippets.

@erikeldridge
Created May 9, 2010 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erikeldridge/395070 to your computer and use it in GitHub Desktop.
Save erikeldridge/395070 to your computer and use it in GitHub Desktop.
a YUI 3 module for string utilities
// a YUI 3 module for string utilities
// ref: http://developer.yahoo.com/yui/3/yui/#yuiadd
YUI.add( 'string', function(Y) {
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
// Source: http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
// Retrieved: 5/10/10
function parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
return uri;
};
parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
// credit: http://javascript.crockford.com/remedial.html
var supplant = function ( string, params ) {
return string.replace(/{([^{}]*)}/g,
function (a, b) {
var r = params[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
// convenience references to pre-existing string functions
// ref: http://developer.yahoo.com/yui/3/api/QueryString.html
var toQueryString = function ( obj, sep, eq ) {
return Y.QueryString.stringify ( obj , sep , eq );
};
var fromQueryString = function ( str ) {
return Y.QueryString.parse( str );
};
// ref: http://developer.yahoo.com/yui/3/api/Lang.html
var trim = function ( str ) {
return Y.Lang.trim( str );
};
// just grab the fns above if you don't want a yui module
Y.namespace('String');
Y.String.parseUri = parseUri;
Y.String.supplant = supplant;
Y.String.toQueryString = toQueryString;
Y.String.fromQueryString = fromQueryString;
Y.String.trim = trim;
}, { requires:['querystring'] } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment