Created
March 17, 2016 16:15
-
-
Save zandroid/e58a6093a0caaf9190f5 to your computer and use it in GitHub Desktop.
Especially for reselect (https://github.com/reactjs/reselect). Reducing of number of arguments in selector with help of spread operator.
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 xSelector = () => 11; | |
var ySelector = () => 22; | |
var zSelector = () => ({ z1: 31, z2: 32 }); | |
var mainSelector = createSelector( | |
xSelector, ySelector, zSelector, | |
(...all) => { | |
let [x, y, { z1, z2 }] = all; | |
return { | |
x, | |
y, | |
z1, | |
z2 | |
}; | |
} | |
); | |
// test result | |
console.log(mainSelector()); // => { x: 11, y: 22, z1: 31, z2: 32 } | |
// fake createSelector | |
function createSelector(...args) { | |
return function() { | |
let factory = args.pop(); | |
return factory.apply(null, args.map(selector => selector())); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment