Skip to content

Instantly share code, notes, and snippets.

@jorinvo
Last active November 22, 2015 08:56
Show Gist options
  • Save jorinvo/d670befc7d67008285aa to your computer and use it in GitHub Desktop.
Save jorinvo/d670befc7d67008285aa to your computer and use it in GitHub Desktop.
formatExcerpt - to ramda or not to ramda
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