Created
July 22, 2018 18:51
-
-
Save rolandpeelen/436541f0e195dfa747c241033016903e to your computer and use it in GitHub Desktop.
Function composition that takes some ms, converts it to hours, check if there are more than one, rounds it and postfixes it with an 'h'
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
| import {compose, curry} from "ramda"; | |
| // Take a function and a value, return the function with the applied value if the value is evaluated as true | |
| const falseOr = curry((fn, value) => value && fn(value)); | |
| // Take a function and a value, return the value if the function returns true | |
| const valueIf = curry((fn, value) => fn(value) && value); | |
| // Take a duration in ms and convert it to either Hours, minutes or seconds; | |
| const msToHours = (duration) => duration / 60 / 60 / 1000; | |
| // Check if a value evaluates to true and if it is over one | |
| const overOne = (value) => isNumber(value) && value > 1; | |
| // Postfix function that takes a postfix and a value and adds it to the back of the value | |
| const addPostFix = curry((postfix, value) => `${value}${postfix}`); | |
| const postFixHours = addPostFix('h'); | |
| // The compositions. Take a value, convert it to a specific unit, check if its over one, round it and postfix it. | |
| const asHours = compose(falseOr(postFixHours), falseOr(Math.round), valueIf(overOne), msToHours); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment