UNPKG

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