Projekt

Obecné

Profil

Stáhnout (751 Bajtů) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict';
2
const stripAnsi = require('strip-ansi');
3
const isFullwidthCodePoint = require('is-fullwidth-code-point');
4
const emojiRegex = require('emoji-regex')();
5
6
module.exports = input => {
7
	input = input.replace(emojiRegex, '  ');
8
9
	if (typeof input !== 'string' || input.length === 0) {
10
		return 0;
11
	}
12
13
	input = stripAnsi(input);
14
15
	let width = 0;
16
17
	for (let i = 0; i < input.length; i++) {
18
		const code = input.codePointAt(i);
19
20
		// Ignore control characters
21
		if (code <= 0x1F || (code >= 0x7F && code <= 0x9F)) {
22
			continue;
23
		}
24
25
		// Ignore combining characters
26
		if (code >= 0x300 && code <= 0x36F) {
27
			continue;
28
		}
29
30
		// Surrogates
31
		if (code > 0xFFFF) {
32
			i++;
33
		}
34
35
		width += isFullwidthCodePoint(code) ? 2 : 1;
36
	}
37
38
	return width;
39
};