Last active
November 22, 2017 09:52
-
-
Save mrwithersea/374575d4658134f8ec359901838b44bb to your computer and use it in GitHub Desktop.
Safe Number
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
import R from 'ramda'; | |
const safeNumber = | |
R.when( | |
R.test(/^(?!\s*$)-?(0|[1-9]\d*)?(\.\d+)?$/), | |
Number | |
); |
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
import R from 'ramda'; | |
import safeNumber from './safe-number'; | |
const safeValue = | |
R.compose( | |
R.cond([ | |
[R.equals(0), R.always(0)], | |
[R.equals(false), R.always(false)], | |
[R.isNil, R.always(undefined)], | |
[R.T, R.identity], | |
]), | |
safeNumber | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment