Last active
February 26, 2020 06:08
-
-
Save leodutra/1eebb61d53ff8002ea91f7222d6e3003 to your computer and use it in GitHub Desktop.
Reducer library for JS
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
const sumReducer = (sum, x) => sum + x; | |
const minReducer = (min/* =null */, x) => { | |
if (min == null) return x; | |
return min < x ? min : x; | |
}; | |
const maxReducer = (max/* =null */, x) => { | |
if (max == null) return x; | |
return max > x ? max : x; | |
}; | |
const meanReducer = (total, amount, index, array) => | |
(index === array.length - 1 | |
? (total + amount) / array.length | |
: total + amount); | |
const ascendingSort = (a, b) => { | |
if (a < b) return -1; | |
if (a > b) return 1; | |
return 0; | |
}; | |
const descendingSort = (a, b) => { | |
if (a < b) return 1; | |
if (a > b) return -1; | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment