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.

Revisions

  1. wasinsandiego revised this gist Feb 13, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion truncate-util.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,6 @@ const middleEllipsisTagFactory = ({ maxLength, start, end, delimiter }) => (
    })
    )


    const truncateMiddleMax10 = middleEllipsisTagFactory({ maxLength: 10 })

    truncateMiddleMax10`This is some long text that must be truncated!`
  2. wasinsandiego revised this gist Feb 13, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion truncate-util.js
    Original file line number Diff line number Diff line change
    @@ -24,5 +24,6 @@ const middleEllipsisTagFactory = ({ maxLength, start, end, delimiter }) => (


    const truncateMiddleMax10 = middleEllipsisTagFactory({ maxLength: 10 })

    truncateMiddleMax10`This is some long text that must be truncated!`
    // This...ed!
    // output --> 'This...ed!'
  3. wasinsandiego revised this gist Feb 13, 2019. 1 changed file with 22 additions and 16 deletions.
    38 changes: 22 additions & 16 deletions truncate-util.js
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,28 @@
    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.`
    )
    const warning = (maxLength, 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} > ${maxLength}). \
    This will produce a truncated string larger than your maxLength.`)
    )

    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)
    const middleEllipsis = ({ text = '', maxLength = 47, start, end, delimiter = '...' }) => {
    if (text.length < maxLength) { return text }
    const startLimit = start || Math.floor((maxLength - delimiter.length) / 1.6)
    const endLimit = end || Math.floor(maxLength - delimiter.length - startLimit)
    if (start && end && startLimit + endLimit + delimiter.length > maxLength) {
    warning(maxLength, 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
    )
    )
    const middleEllipsisTagFactory = ({ maxLength, start, end, delimiter }) => (
    (texts, ...rest) => middleEllipsis({
    text: texts.reduce((str, text, index) => `${str}${text}${rest[index] ? rest[index] : ''}`, ''),
    maxLength, start, end, delimiter
    })
    )


    const truncateMiddleMax10 = middleEllipsisTagFactory({ maxLength: 10 })
    truncateMiddleMax10`This is some long text that must be truncated!`
    // This...ed!
  4. wasinsandiego created this gist Jan 19, 2018.
    22 changes: 22 additions & 0 deletions truncate-util.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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
    )
    )