Last active
January 14, 2021 18:43
-
-
Save dinocarl/55f4c6a71d9c8b2a9a3e4fe70186743e 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
const isntEmpty = complement(isEmpty); | |
const isntNil = complement(isNil); | |
const isntNaN = complement(isNaN); | |
const pageLens = lensProp('page'); | |
const numberValidations = [ | |
isntNaN, | |
isntNil, | |
isntEmpty, | |
is(Number), | |
]; | |
const numberValidationsGT0 = concat(numberValidations, [lt(0)]); | |
const integerValidations = [Number.isInteger]; | |
const integerValidationsGT0 = concat(integerValidations, [lt(0)]); | |
const calcPageOffset = compose( | |
apply(multiply), | |
values, | |
over(pageLens, dec), | |
pick(['page', 'limit']), | |
); | |
const validNumberGT0At = (keyName) => compose( | |
allPass(numberValidationsGT0), | |
prop(keyName), | |
); | |
const validIntegerGT0At = (keyName) => compose( | |
allPass(integerValidationsGT0), | |
prop(keyName), | |
); | |
const hasValidPageOffsetData = allPass([ | |
validNumberGT0At('page'), | |
validNumberGT0At('limit') | |
]); | |
const hasValidOffset = validIntegerGT0At('offset'); | |
const getOffset = cond([ | |
[hasValidPageOffsetData, calcPageOffset], | |
[hasValidOffset, prop('offset')], | |
[T, always(0)] | |
]); | |
const data = { | |
page: 25, | |
offset: 5, | |
limit: 10, | |
}; | |
getOffset(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment