Newer
Older
CVSS_3.0_GUI / dist / CVSS_3.0_Calc-1.0.0-linux-x64 / node_modules / nwjs-builder-phoenix / node_modules / yargs / node_modules / string-width / index.js
root on 7 May 2019 741 bytes Initial commit
  1. 'use strict';
  2. var stripAnsi = require('strip-ansi');
  3. var codePointAt = require('code-point-at');
  4. var isFullwidthCodePoint = require('is-fullwidth-code-point');
  5.  
  6. // https://github.com/nodejs/io.js/blob/cff7300a578be1b10001f2d967aaedc88aee6402/lib/readline.js#L1345
  7. module.exports = function (str) {
  8. if (typeof str !== 'string' || str.length === 0) {
  9. return 0;
  10. }
  11.  
  12. var width = 0;
  13.  
  14. str = stripAnsi(str);
  15.  
  16. for (var i = 0; i < str.length; i++) {
  17. var code = codePointAt(str, i);
  18.  
  19. // ignore control characters
  20. if (code <= 0x1f || (code >= 0x7f && code <= 0x9f)) {
  21. continue;
  22. }
  23.  
  24. // surrogates
  25. if (code >= 0x10000) {
  26. i++;
  27. }
  28.  
  29. if (isFullwidthCodePoint(code)) {
  30. width += 2;
  31. } else {
  32. width++;
  33. }
  34. }
  35.  
  36. return width;
  37. };
Buy Me A Coffee