Last active
July 13, 2016 13:22
-
-
Save alvieirajr/989a8b62069eb19f1665b7daac9f3df2 to your computer and use it in GitHub Desktop.
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
var compose = function() { | |
var funcs = arguments; | |
return function() { | |
var args = arguments; | |
for (var i = funcs.length; i --> 0;) { | |
args = [funcs[i].apply(this, args)]; | |
} | |
return args[0]; | |
}; | |
}; | |
var after = function(chr) { | |
return function(str) { // String ==> String | |
var index = str.indexOf(chr); | |
return (index < 0) ? "" : str.substring(index + 1); | |
}; | |
}; | |
var splitOn = function(chr) { | |
return function(str) { // String ==> [String] | |
return str.split(chr); | |
}; | |
}; | |
var makeObj = function(desc) { // [String] ==> Object | |
return desc.reduce(function(obj, str) { | |
var parts = str.split("="); | |
obj[parts[0]] = parts[1]; | |
return obj; | |
}, {}); | |
}; | |
var url = "http://example.com/fetch?product=widget&color=red&size=6"; | |
var getConfig = compose(makeObj, splitOn("&"), after("?")); | |
console.log(getConfig(url)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Each function accept the same type its predecessor generated