Created
September 20, 2022 10:51
-
-
Save mpecka/13f0cfc780809cfd7a1b6b3f4619ded3 to your computer and use it in GitHub Desktop.
JS Truncate String Function
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 characters
| function truncateString(str, len, suffix) { | |
| var suffix = suffix || "..."; | |
| var len = len || 10; | |
| if (str.length <= len) { | |
| return str; | |
| } else { | |
| return str.slice(0, len - suffix.length) + suffix; | |
| } | |
| } | |
| /* Examples */ | |
| truncateString("1234567890abcdefghij1234567890abcdefghij"); | |
| truncateString("1234567890abcdefghij1234567890abcdefghij",20); | |
| truncateString("1234567890abcdefghij1234567890abcdefghij",11,"(truncated)"); | |
| truncateString("1234567890abcdefghij1234567890abcdefghij",20,"(truncated)"); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And this is GTM Custom Javascript variable: