Sha256: 7d88178fb35726264fa3a8500abeae44d5b585491edf3cd5b0ecf4dec1bbb299

Contents?: true

Size: 741 Bytes

Versions: 126

Compression:

Stored size: 741 Bytes

Contents

'use strict';
var stripAnsi = require('strip-ansi');
var codePointAt = require('code-point-at');
var isFullwidthCodePoint = require('is-fullwidth-code-point');

// https://github.com/nodejs/io.js/blob/cff7300a578be1b10001f2d967aaedc88aee6402/lib/readline.js#L1345
module.exports = function (str) {
	if (typeof str !== 'string' || str.length === 0) {
		return 0;
	}

	var width = 0;

	str = stripAnsi(str);

	for (var i = 0; i < str.length; i++) {
		var code = codePointAt(str, i);

		// ignore control characters
		if (code <= 0x1f || (code >= 0x7f && code <= 0x9f)) {
			continue;
		}

		// surrogates
		if (code >= 0x10000) {
			i++;
		}

		if (isFullwidthCodePoint(code)) {
			width += 2;
		} else {
			width++;
		}
	}

	return width;
};

Version data entries

126 entries across 90 versions & 18 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/string-width/index.js
disco_app-0.18.0 test/dummy/node_modules/string-width/index.js
disco_app-0.18.0 test/dummy/node_modules/fsevents/node_modules/string-width/index.js
disco_app-0.18.2 test/dummy/node_modules/string-width/index.js
disco_app-0.16.1 test/dummy/node_modules/gauge/node_modules/string-width/index.js
disco_app-0.15.2 test/dummy/node_modules/gauge/node_modules/string-width/index.js
disco_app-0.18.4 test/dummy/node_modules/gauge/node_modules/string-width/index.js
disco_app-0.18.1 test/dummy/node_modules/gauge/node_modules/string-width/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/gauge/node_modules/string-width/index.js
disco_app-0.14.0 test/dummy/node_modules/gauge/node_modules/string-width/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/gauge/node_modules/string-width/index.js
tang-0.2.1 spec/tang_app/node_modules/gauge/node_modules/string-width/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/gauge/node_modules/string-width/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/gauge/node_modules/string-width/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/gauge/node_modules/string-width/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/gauge/node_modules/string-width/index.js
tang-0.2.0 spec/tang_app/node_modules/gauge/node_modules/string-width/index.js
tang-0.1.0 spec/tang_app/node_modules/gauge/node_modules/string-width/index.js
tang-0.0.9 spec/tang_app/node_modules/gauge/node_modules/string-width/index.js
enju_library-0.3.8 spec/dummy/node_modules/fsevents/node_modules/string-width/index.js