truncate-middle.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.truncateMiddle = void 0;
  4. /**
  5. * Date: 2015-10-05
  6. * Author: Kasper Søfren <soefritz@gmail.com> (https://github.com/kafoso)
  7. *
  8. * A truncation feature, where the ellipsis will be placed in the dead-center of the URL.
  9. *
  10. * @param {String} url A URL.
  11. * @param {Number} truncateLen The maximum length of the truncated output URL string.
  12. * @param {String} ellipsisChars The characters to place within the url, e.g. "..".
  13. * @return {String} The truncated URL.
  14. */
  15. function truncateMiddle(url, truncateLen, ellipsisChars) {
  16. if (url.length <= truncateLen) {
  17. return url;
  18. }
  19. var ellipsisLengthBeforeParsing;
  20. var ellipsisLength;
  21. if (ellipsisChars == null) {
  22. ellipsisChars = '&hellip;';
  23. ellipsisLengthBeforeParsing = 8;
  24. ellipsisLength = 3;
  25. }
  26. else {
  27. ellipsisLengthBeforeParsing = ellipsisChars.length;
  28. ellipsisLength = ellipsisChars.length;
  29. }
  30. var availableLength = truncateLen - ellipsisLength;
  31. var end = '';
  32. if (availableLength > 0) {
  33. end = url.substr(-1 * Math.floor(availableLength / 2));
  34. }
  35. return (url.substr(0, Math.ceil(availableLength / 2)) + ellipsisChars + end).substr(0, availableLength + ellipsisLengthBeforeParsing);
  36. }
  37. exports.truncateMiddle = truncateMiddle;
  38. //# sourceMappingURL=truncate-middle.js.map