Initial commit
[yaffs-website] / node_modules / string-width / index.js
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 };