Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Last active May 7, 2018 21:44
Show Gist options
  • Save miladvafaeifard/6c0e0d692203d10323d6850c7e1d504e to your computer and use it in GitHub Desktop.
Save miladvafaeifard/6c0e0d692203d10323d6850c7e1d504e to your computer and use it in GitHub Desktop.
Recover from a Nothing with the "alt" method (Crocks, Ramda libraries)
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);
@miladvafaeifard
Copy link
Author

learn more about crocks and ramda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment