1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
|
|
const ansiEscapeCode = '[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]' |
4
|
|
|
, zeroWidthCharacterExceptNewline = '\u0000-\u0008\u000B-\u0019\u001b\u009b\u00ad\u200b\u2028\u2029\ufeff\ufe00-\ufe0f' |
5
|
|
|
, zeroWidthCharacter = '\n' + zeroWidthCharacterExceptNewline |
6
|
|
|
, zeroWidthCharactersExceptNewline = new RegExp ('(?:' + ansiEscapeCode + ')|[' + zeroWidthCharacterExceptNewline + ']', 'g') |
7
|
|
|
, zeroWidthCharacters = new RegExp ('(?:' + ansiEscapeCode + ')|[' + zeroWidthCharacter + ']', 'g') |
8
|
|
|
, partition = new RegExp ('((?:' + ansiEscapeCode + ')|[\t' + zeroWidthCharacter + '])?([^\t' + zeroWidthCharacter + ']*)', 'g') |
9
|
|
|
|
10
|
|
|
module.exports = { |
11
|
|
|
|
12
|
|
|
zeroWidthCharacters, |
13
|
|
|
|
14
|
|
|
ansiEscapeCodes: new RegExp (ansiEscapeCode, 'g'), |
15
|
|
|
|
16
|
|
|
strlen: s => Array.from (s.replace (zeroWidthCharacters, '')).length, // Array.from solves the emoji problem as described here: http://blog.jonnew.com/posts/poo-dot-length-equals-two |
17
|
|
|
|
18
|
|
|
isBlank: s => s.replace (zeroWidthCharacters, '') |
19
|
|
|
.replace (/\s/g, '') |
20
|
|
|
.length === 0, |
21
|
|
|
|
22
|
|
|
blank: s => Array.from (s.replace (zeroWidthCharactersExceptNewline, '')) // Array.from solves the emoji problem as described here: http://blog.jonnew.com/posts/poo-dot-length-equals-two |
23
|
|
|
.map (x => ((x === '\t') || (x === '\n')) ? x : ' ') |
24
|
|
|
.join (''), |
25
|
|
|
|
26
|
|
|
partition (s) { |
27
|
|
|
for (var m, spans = []; (partition.lastIndex !== s.length) && (m = partition.exec (s));) { spans.push ([m[1] || '', m[2]]) } |
28
|
|
|
partition.lastIndex = 0 // reset |
29
|
|
|
return spans |
30
|
|
|
}, |
31
|
|
|
|
32
|
|
|
first (s, n) { |
33
|
|
|
|
34
|
|
|
let result = '', length = 0 |
35
|
|
|
|
36
|
|
|
for (const [nonPrintable, printable] of module.exports.partition (s)) { |
37
|
|
|
const text = Array.from (printable).slice (0, n - length) // Array.from solves the emoji problem as described here: http://blog.jonnew.com/posts/poo-dot-length-equals-two |
38
|
|
|
result += nonPrintable + text.join ('') |
39
|
|
|
length += text.length |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return result |
43
|
|
|
} |
44
|
|
|
} |