Last active
November 22, 2015 08:56
-
-
Save jorinvo/d670befc7d67008285aa to your computer and use it in GitHub Desktop.
formatExcerpt - to ramda or not to ramda
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
export function formatExcerpt (text) { | |
if (!text) return | |
if (text.length < config.textExcerptLength) { | |
return text | |
} | |
// cut length and remove last word | |
return text | |
.substr(0, config.textExcerptLength) | |
.split(' ') | |
.slice(0, -1) | |
.join(' ') + ' …' | |
} | |
const isShort = pipe(length, lt(__, config.textExcerptLength)) | |
const cutLength = pipe(split(''), slice(0, config.textExcerptLength), join('')) | |
const withoutLastWord = pipe(split(' '), slice(0, -1), join(' ')) | |
export const formatExcerpt = unless( | |
either(isNil, isShort), | |
pipe(cutLength, withoutLastWord, add(__, ' …')) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment