"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/string-width/index.js" (8 Mar 2017, 741 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 };