Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
28 lines (28 sloc)
1.1 KB
This file contains 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
| 'use strict' | |
| /** | |
| * There are cases where important information is at the end of the string and truncating the end isn't helpful. | |
| * This function solves that. | |
| * | |
| * @param {string} str String to be truncated | |
| * @param {number} frontLen Number of characters to be remained in front. | |
| * @param {number} backLen Number of characters to be remained at the back. | |
| * @param {string} truncateStr String that is replaced the truncated portion | |
| * @return {string} Truncated string. Defaults to '…' if unspecified. | |
| */ | |
| module.exports = function (str, frontLen, backLen, truncateStr) { | |
| if (str === null) { | |
| return '' | |
| } | |
| var strLen = str.length | |
| // Setting default values | |
| frontLen = ~~frontLen // will cast to integer | |
| backLen = ~~backLen | |
| truncateStr = truncateStr || '…' | |
| if (frontLen === 0 && backLen === 0 || frontLen >= strLen || backLen >= strLen || (frontLen + backLen) >= strLen) { | |
| return str | |
| } else if (backLen === 0) { | |
| return str.slice(0, frontLen) + truncateStr | |
| } else { | |
| return str.slice(0, frontLen) + truncateStr + str.slice(strLen - backLen) | |
| } | |
| } |