| •1 |
var punycode = require('punycode/') |
| 2 |
|
| 3 |
function ansiRegex () { |
| 4 |
const pattern = [ |
| 5 |
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', |
| 6 |
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))' |
| 7 |
].join('|') |
| 8 |
|
| 9 |
return new RegExp(pattern, 'g') |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
var REGEX_SYMBOLS = /([\0-\u02FF\u0370-\u1DBF\u1E00-\u20CF\u2100-\uD7FF\uDC00-\uFE1F\uFE30-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF])([\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE20-\uFE2F]+)/g |
| 15 |
|
| 16 |
exports.get = function (input) { |
| 17 |
if (input == null) { |
| 18 |
throw new Error('Missing input') |
| 19 |
} |
| 20 |
|
| 21 |
if (typeof input !== 'string') { |
| 22 |
throw new Error('Invalid input: ' + input) |
| 23 |
} |
| 24 |
|
| 25 |
input = input.replace(ansiRegex(), '') |
| 26 |
|
| 27 |
const stripped = input.replace(REGEX_SYMBOLS, function ($0, symbol, combiningMarks) { |
| 28 |
return symbol |
| 29 |
}) |
| 30 |
|
| 31 |
return punycode.ucs2.decode(stripped).length |
| 32 |
} |
| 33 |
|
| 34 |
if (process.env['NODE_DEV'] === 'TEST') { |
| 35 |
exports.ansiRegex = ansiRegex |
| 36 |
} |