Front end of the Slack clone application.

wide-truncate.js 828B

1234567891011121314151617181920212223242526
  1. 'use strict'
  2. var stringWidth = require('string-width')
  3. var stripAnsi = require('strip-ansi')
  4. module.exports = wideTruncate
  5. function wideTruncate (str, target) {
  6. if (stringWidth(str) === 0) return str
  7. if (target <= 0) return ''
  8. if (stringWidth(str) <= target) return str
  9. // We compute the number of bytes of ansi sequences here and add
  10. // that to our initial truncation to ensure that we don't slice one
  11. // that we want to keep in half.
  12. var noAnsi = stripAnsi(str)
  13. var ansiSize = str.length + noAnsi.length
  14. var truncated = str.slice(0, target + ansiSize)
  15. // we have to shrink the result to account for our ansi sequence buffer
  16. // (if an ansi sequence was truncated) and double width characters.
  17. while (stringWidth(truncated) > target) {
  18. truncated = truncated.slice(0, -1)
  19. }
  20. return truncated
  21. }