Skip to content

Instantly share code, notes, and snippets.

@andfaulkner
Created June 15, 2015 13:46
Show Gist options
  • Save andfaulkner/38a22c74a4bffcfd8922 to your computer and use it in GitHub Desktop.
Save andfaulkner/38a22c74a4bffcfd8922 to your computer and use it in GitHub Desktop.
/**
* Custom Lodash functions for transforming http GET requests
*/
_.mixin({
//Shifts an item in an array from one location in an array to another
move: function (array, fromIndex, toIndex) {
array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
return array;
},
//Unescape an array (for easier use in chaining)
unescape: function(array){
return (unescape(array))
},
//Remove any GET request (query string) parameter ending in a certain character or set of characters
rmEndsWith: function(array, endString){
return _.filter(array, function(v) {
return ((_.takeRight(v, _.size(endString)).join("") === endString) ?
false : v);
})
},
/**
* Preps a URL grabbed from a page for usage in Javascript code. Unescapes characters,
* removes spaces, splits it into components (from the query string), and gets rid of
* query string parameters that contain no values.
*/
cleanURL: function(array) {
return _.chain(array)
.unescape()
.replace(/\ /g, "")
.words(/([^\&]+(?=\&))\&|[^&]*[\w]+$/gi)
.rmEndsWith("=&").value();
return array;
},
/**
* Find query parameter that returns a hit on regular expression 'matcher', then replace the part of the
* parameter matching string 'to' with string 'from'.
*/
replaceItem: function(array, matcher, to, from){
return _.map(array, function(val){
return ((val.toString().match(matcher) !== -1) ?
val.toString().replace(((from) ? from : matcher), to) :
val.toString());
})
},
/**
* Convenience function to remove a semicolon at the end of the last item in an http "GET" request
*/
rmEndSemicolon: function(coll){
var inter = ((_.last(_.last(coll)) === ";") ?
(_.union(_.initial(coll),
[_.initial( _.last(coll) ).join('')])) :
coll);
if (_.isArray(coll)) return inter
else return inter.join('');
},
/**
* Get a given query parameter. Returns a 'pair' containing the parameter name and its value.
* (note: poorly tested).
*/
getQueryParam: function(url, paramName) {
var query = url;
if (query.match("\%20") !== -1) unescape(query);
var params = query.split("&");
for (var i=0;i<params.length;i++) {
var paramAndValue = params[i].split("=");
if(paramAndValue[0] === query){
return paramAndValue[1];
}
}
return(url);
},
unshift: function(array, itemToIns){
array.unshift(itemToIns);
return array;
},
//Ins itemToIns if matcher hits any text from any arr item (1 insert total). Rm any item ret a match (full item, not just matched text).
unshiftIfMatchAndRm: function(arr, matcher, itemToIns){
var rmArr = _.remove(arr, function(v){
return (v.match(matcher)) ? true : false;
});
if (_.size(rmArr) > 0) _.without(arr.unshift(itemToIns), rmArr);
return arr;
},
/**
* Convert a string to a URI (needs a bit of work).
*/
convertToURI: function(string){
string = _.unescape(string);
string = string.replace(/\ /g, "");
return string;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment