Skip to content

Instantly share code, notes, and snippets.

@fonzerelly
Created September 13, 2016 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fonzerelly/f414bbbb9f2572a16e9886b04a815e06 to your computer and use it in GitHub Desktop.
Save fonzerelly/f414bbbb9f2572a16e9886b04a815e06 to your computer and use it in GitHub Desktop.
var R = require('ramda');
var
//[[key, value]] -> {key: [[key, value]]}
groupByKey = R.groupBy(R.head),
//{'':a, 'b':b} -> {'b':b}
rejectEmptyKeys = R.pickBy(function(value, key) {return key !== '';}),
//[[key, value]] -> [value | undefined]
extractValueFromPair = R.map(function(pair) {return pair[1];}),
//[a] -> [a] | a
extractValueFromSingleArray = function(a) {
return (a.length === 1) ? R.head(a) : a;
},
//[[key, value]], {key: value}
objectizePairs = R.pipe(
groupByKey,
rejectEmptyKeys,
R.map(extractValueFromPair),
R.map(extractValueFromSingleArray)
),
//String -> {a: [a] | a}
objectizeUrlSearch = R.pipe(
R.split('&'),
R.map(R.split('=')),
objectizePairs
);
module.exports = {
objectizeUrlSearch : objectizeUrlSearch
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment