Skip to content

Instantly share code, notes, and snippets.

@JesterXL
Last active March 3, 2019 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JesterXL/039752bc65abf34d43087cb6b1d5b285 to your computer and use it in GitHub Desktop.
Save JesterXL/039752bc65abf34d43087cb6b1d5b285 to your computer and use it in GitHub Desktop.
Example of nested Arrow functions.
'use strict'
const Validation = require('folktale/validation')
const { Success, Failure } = Validation
const {
isString,
some,
identity,
inRange,
isInteger
} = require('lodash/fp')
const validValidValues = o =>
legitValidValues(o)
? Success(o)
: Failure([`Invalid validValues, supposed to be a non-empty String, but you sent: ${o}`])
// -- ValidValues ----
const nonEmptyString = o =>
isString(o) && o !== '' && o.trim() !== ''
const notBlank = o =>
o !== ''
const safeSplit = o =>
nonEmptyString(o)
? o.trim().split(',').filter(notBlank)
: []
const safeContains = string => searchString =>
isString(string)
&& string.indexOf(searchString) > -1
const validValuesRuleUnsafe = value => validValues =>
some(safeContains(value)(safeSplit(validValues)))
const legitValue = nonEmptyString
const validValue = o =>
legitValue(o)
? Success(o)
: Failure([`Invalid value, supposed to be a non-empty String, but you sent: ${o}`])
const validValuesRule = value => validValues =>
validValue(value)
.concat(validValidValues(validValues))
.matchWith({
Failure: identity,
Success: () =>
validValuesRuleUnsafe(value)(validValues)
? Success({ value, validValues })
: Failure([`validValues failed, couldn't find any of these ${validValues} values contained in ${value}`])
})
// -- Range -----
const legitRangeValue = isInteger
const legitRangeMin = isInteger
const legitRangeMax = isInteger
const maxIsLargerThanMin = min => max =>
max > min
const validRangeValue = o =>
legitRangeValue(o)
? Success(o)
: Failure([`Invalid range value, supposed to be an integer, but you sent: ${o}`])
const validRangeMinValue = o =>
legitRangeMin(o)
? Success(o)
: Failure([`Invalid rangeMin, supposed to be an integer, but you sent: ${o}`])
const validRangeMaxValue = o =>
legitRangeMax(o)
? Success(o)
: Failure([`Invalid rangeMax, supposed to be an integer, but you sent: ${o}`])
const validMaxIsLargerThanMin = min => max =>
maxIsLargerThanMin(min)(max)
? Success({ min, max })
: Failure([`Invalid min and max, the max ${max} is supposed to be larger than the min ${min}.`])
const isInRangeUnsafe = value => min => max =>
inRange(min, max + 1, value)
? Success({ value, min, max })
: Failure([`value ${value} is not equal to or greater than ${min} and less than or equal to ${max}`])
const rangeRule = value => min => max =>
validRangeValue(value)
.concat(validRangeMinValue(min))
.concat(validRangeMaxValue(max))
.matchWith({
Failure: identity,
Success: () =>
validMaxIsLargerThanMin(min)(max),
})
.matchWith({
Failure: identity,
Success: () =>
isInRangeUnsafe(value)(min)(max),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment