All files / libs/tty stripAnsi.js

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                1x                                 1x 434x                                   1x  
/**
 * @module      libs/tty/stripAnsi
 * @createdAt   2016-07-17
 *
 * @copyright   Copyright (c) 2016 Zhonglei Qiu
 * @license     Licensed under the MIT license.
 */
 
var gre = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g
 
/**
 * 去掉字符串中的 ansi escape 字符
 *
 * @param  {*} str      需要去除 ansi escape code 的字符串,如果 str 不是字符串,则返回它本身
 * @return {String}     去除 ansi escape code 之后的字符串
 *
 * @see [ansi escape code from wiki]{@link https://en.wikipedia.org/wiki/ANSI_escape_code}
 * @see [strip-ansi@3.0.1]{@link https://github.com/chalk/strip-ansi/tree/v3.0.1}
 *
 * @example
 * stripAnsi('\u001b[31ma\u001b[0m') // 'a'
 *
 * @author Zhonglei Qiu
 * @since 2.0.0
 */
module.exports = function(str) {
  return typeof str === 'string' ? str.replace(gre, '') : str
}
 
/**
 * 匹配 ansi escape code 的正则表达式
 *
 * 注意,此正则带有 global modifier
 *
 * **说明:**
 *
 * - `re` 或以 `re` 开头(如 reAnsi)表示__不__带 global modifier 的正则表达式
 * - `gre` 或以 `gre` 开头(如 greAnsi) 表示带 global modifier 的正则表达式
 *
 * @type {RegExp}
 * @see [ansi-regexp@2.0.0]{@link https://github.com/chalk/ansi-regex/tree/2.0.0}
 * @author Zhonglei Qiu
 * @since 2.0.0
 */
module.exports.gre = gre