index.js 721 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var eaw = require('eastasianwidth');
  3. function stringWidth (ambiguousCharWidth) {
  4. return function widthOf (str) {
  5. var i, code, width = 0;
  6. for(i = 0; i < str.length; i+=1) {
  7. code = eaw.eastAsianWidth(str.charAt(i));
  8. switch(code) {
  9. case 'F':
  10. case 'W':
  11. width += 2;
  12. break;
  13. case 'H':
  14. case 'Na':
  15. case 'N':
  16. width += 1;
  17. break;
  18. case 'A':
  19. width += ambiguousCharWidth;
  20. break;
  21. }
  22. }
  23. return width;
  24. };
  25. }
  26. module.exports = stringWidth(2);
  27. module.exports.narrow = stringWidth(1);