Last active
May 7, 2018 21:44
-
-
Save miladvafaeifard/6c0e0d692203d10323d6850c7e1d504e to your computer and use it in GitHub Desktop.
Recover from a Nothing with the "alt" method (Crocks, Ramda libraries)
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 crocks = require('crocks'); | |
const { and, compose, isEmpty, isString, Maybe, not, prop, safe } = crocks; | |
const { join, split, toLower } = require('ramda'); | |
const isNonEmptyString = and(not(isEmpty), isString); | |
const createUrlSlug = compose(join('-'), split(' '), toLower); | |
const createUrl = slug => `localhost:3000/${slug}`; | |
const article = { | |
id: 1, | |
name: 'Learn FP with this one weird trick' | |
}; | |
const getArticleName = obj => | |
prop('name', obj) | |
.chain(safe(isNonEmptyString)) | |
.alt(Maybe.of('Page Not Found')); | |
const url = getArticleName(article) | |
.map(compose(createUrl, createUrlSlug)); | |
console.log(url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
learn more about crocks and ramda