Skip to content

Instantly share code, notes, and snippets.

@wasinsandiego
Last active February 13, 2019 18:23
Show Gist options
  • Select an option

  • Save wasinsandiego/968a6116344b5204fed304ad27796528 to your computer and use it in GitHub Desktop.

Select an option

Save wasinsandiego/968a6116344b5204fed304ad27796528 to your computer and use it in GitHub Desktop.
const warning = (limit, startLimit, endLimit, delimiter) => (
console.warn(
`middleEllipsis :: The start (${startLimit}) and end (${endLimit}) plus the delimiter (${delimiter}) add up to more than the limit (${startLimit + endLimit + delimiter.length} > ${limit}). This will produce a truncated string larger than your limit.`
)
)
export const middleEllipsis = (text = '', limit = 47, start, end, delimiter = '...') => {
if (text.length < limit) { return text }
const startLimit = start || Math.floor((limit - delimiter.length) / 1.6)
const endLimit = end || Math.floor(limit - delimiter.length - startLimit)
if (start && end && startLimit + endLimit + delimiter.length > limit) {
warning(limit, startLimit, endLimit, delimiter)
}
return `${text.substring(0, startLimit)}${delimiter}${text.substring(text.length - endLimit, text.length)}`
}
export const middleEllipsisTagFactory = (limit, start, end, delimiter) => (
(texts, ...rest) => middleEllipsis(
texts.reduce((str, text, index) => `${str}${text}${rest[index] ? rest[index] : ''}`, ''),
limit, start, end, delimiter
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment