Last active
September 21, 2016 21:12
-
-
Save fonzerelly/f49c56a9b136d672f06157bbf9d0a078 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Rx = require('rx'); | |
var | |
observablePerPair = function(string) { | |
return Rx.Observable.from(string.split('&')); | |
}, | |
splitEqual = function(pairString) { | |
return pairString.split('='); | |
}, | |
key = function (pair) { | |
return pair[0]; | |
}, | |
value = function (pair) { | |
return pair[1]; | |
}, | |
objectFromKey = function(key) { | |
var obj = {}; | |
obj[key] = null; | |
return obj; | |
}, | |
scanArray = function(array, value) { | |
array.push(value); | |
return array; | |
}, | |
extractValueFromSingleArray = function(a) { | |
return (a.length === 1) ? a[0] : a; | |
}, | |
getOnlyKey = function(obj) { | |
keys = Object.keys(obj); | |
console.assert(keys.length === 1, 'Too many Keys'); | |
return keys[0]; | |
}, | |
scanObject = function(obj, value){ | |
obj[getOnlyKey(obj)] = value; | |
return obj; | |
}, | |
objectizeUrlSearch = function(urlString) { | |
var result={}; | |
Rx.Observable.just(urlString) | |
.flatMap(observablePerPair) | |
.map(splitEqual) | |
.groupBy(key, value) | |
.subscribe(function(obs) { Rx.Observable.just(objectFromKey(obs.key)).merge( | |
obs | |
.scan(scanArray, []) | |
.map(extractValueFromSingleArray) | |
) | |
.scan(scanObject) | |
.subscribe( | |
function(keyObject) { | |
result = Object.assign(result, keyObject); | |
} | |
); | |
}); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment