refactor and extract the RegExp escaping
This commit is contained in:
parent
1956792293
commit
763b167349
3 changed files with 9 additions and 19 deletions
25
index.js
25
index.js
|
|
@ -1,4 +1,5 @@
|
|||
'use strict';
|
||||
var escapeStringRegexp = require('escape-string-regexp');
|
||||
var ansiStyles = require('ansi-styles');
|
||||
var stripAnsi = require('strip-ansi');
|
||||
var supportsColor = require('supports-color');
|
||||
|
|
@ -11,6 +12,8 @@ var styles = (function () {
|
|||
ansiStyles.grey = ansiStyles.gray;
|
||||
|
||||
Object.keys(ansiStyles).forEach(function (key) {
|
||||
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
||||
|
||||
ret[key] = {
|
||||
get: function () {
|
||||
this._styles.push(key);
|
||||
|
|
@ -22,19 +25,6 @@ var styles = (function () {
|
|||
return ret;
|
||||
})();
|
||||
|
||||
// enrich each ansiStyle object with regular expression matching
|
||||
// all instances of the corresponding code.close
|
||||
(function () {
|
||||
var _matchUnescapedCharacters = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
|
||||
function escapeStr(str) {
|
||||
return str.replace(_matchUnescapedCharacters, '\\$&');
|
||||
}
|
||||
|
||||
Object.keys(ansiStyles).forEach(function (key) {
|
||||
ansiStyles[key].closeRe = new RegExp(escapeStr(ansiStyles[key].close), 'g');
|
||||
});
|
||||
})();
|
||||
|
||||
function init() {
|
||||
var ret = {};
|
||||
|
||||
|
|
@ -50,12 +40,11 @@ function init() {
|
|||
|
||||
return self._styles.reduce(function (str, name) {
|
||||
var code = ansiStyles[name];
|
||||
return str ?
|
||||
return str ? code.open +
|
||||
// Replace any instances already present with a re-opening code
|
||||
// otherwise only the part of the string until said closing code will be coloured, and the rest will be
|
||||
// simply 'plain'.
|
||||
code.open + str.replace(code.closeRe, code.open) + code.close :
|
||||
'';
|
||||
// otherwise only the part of the string until said closing code
|
||||
// will be colored, and the rest will simply be 'plain'.
|
||||
str.replace(code.closeRe, code.open) + code.close : '';
|
||||
}, str);
|
||||
}, styles);
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@
|
|||
],
|
||||
"dependencies": {
|
||||
"ansi-styles": "^1.1.0",
|
||||
"escape-string-regexp": "^1.0.0",
|
||||
"strip-ansi": "^0.2.0",
|
||||
"supports-color": "^0.2.0"
|
||||
},
|
||||
|
|
|
|||
2
test.js
2
test.js
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
var assert = require('assert');
|
||||
var chalk = require('./index');
|
||||
var chalk = require('./');
|
||||
|
||||
describe('chalk', function () {
|
||||
it('should style string', function () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue