Skip to content

Instantly share code, notes, and snippets.

@alvieirajr
Last active July 13, 2016 13:22
Show Gist options
  • Save alvieirajr/989a8b62069eb19f1665b7daac9f3df2 to your computer and use it in GitHub Desktop.
Save alvieirajr/989a8b62069eb19f1665b7daac9f3df2 to your computer and use it in GitHub Desktop.
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));
@alvieirajr
Copy link
Author

Each function accept the same type its predecessor generated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment