3 var defaults = require('defaults')
4 var combining = require('./combining')
11 module.exports = function wcwidth(str) {
12 return wcswidth(str, DEFAULTS)
15 module.exports.config = function(opts) {
16 opts = defaults(opts || {}, DEFAULTS)
17 return function wcwidth(str) {
18 return wcswidth(str, opts)
23 * The following functions define the column width of an ISO 10646
24 * character as follows:
25 * - The null character (U+0000) has a column width of 0.
26 * - Other C0/C1 control characters and DEL will lead to a return value
28 * - Non-spacing and enclosing combining characters (general category
29 * code Mn or Me in the
30 * Unicode database) have a column width of 0.
31 * - SOFT HYPHEN (U+00AD) has a column width of 1.
32 * - Other format characters (general category code Cf in the Unicode
33 * database) and ZERO WIDTH
34 * SPACE (U+200B) have a column width of 0.
35 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
36 * have a column width of 0.
37 * - Spacing characters in the East Asian Wide (W) or East Asian
38 * Full-width (F) category as
39 * defined in Unicode Technical Report #11 have a column width of 2.
40 * - All remaining characters (including all printable ISO 8859-1 and
41 * WGL4 characters, Unicode control characters, etc.) have a column
43 * This implementation assumes that characters are encoded in ISO 10646.
46 function wcswidth(str, opts) {
47 if (typeof str !== 'string') return wcwidth(str, opts)
50 for (var i = 0; i < str.length; i++) {
51 var n = wcwidth(str.charCodeAt(i), opts)
59 function wcwidth(ucs, opts) {
60 // test for 8-bit control characters
61 if (ucs === 0) return opts.nul
62 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) return opts.control
64 // binary search in table of non-spacing characters
65 if (bisearch(ucs)) return 0
67 // if we arrive here, ucs is not a combining or C0/C1 control character
70 (ucs <= 0x115f || // Hangul Jamo init. consonants
71 ucs == 0x2329 || ucs == 0x232a ||
72 (ucs >= 0x2e80 && ucs <= 0xa4cf &&
73 ucs != 0x303f) || // CJK ... Yi
74 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
75 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compatibility Ideographs
76 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
77 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compatibility Forms
78 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
79 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
80 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
81 (ucs >= 0x30000 && ucs <= 0x3fffd)));
84 function bisearch(ucs) {
86 var max = combining.length - 1
89 if (ucs < combining[0][0] || ucs > combining[max][1]) return false
92 mid = Math.floor((min + max) / 2)
93 if (ucs > combining[mid][1]) min = mid + 1
94 else if (ucs < combining[mid][0]) max = mid - 1