Last active
February 13, 2019 18:23
-
-
Save wasinsandiego/968a6116344b5204fed304ad27796528 to your computer and use it in GitHub Desktop.
Revisions
-
wasinsandiego revised this gist
Feb 13, 2019 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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!` -
wasinsandiego revised this gist
Feb 13, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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!` // output --> 'This...ed!' -
wasinsandiego revised this gist
Feb 13, 2019 . 1 changed file with 22 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,28 @@ 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.`) ) 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)}` } 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! -
wasinsandiego created this gist
Jan 19, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 ) )