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';
|
'use strict';
|
||||||
|
var escapeStringRegexp = require('escape-string-regexp');
|
||||||
var ansiStyles = require('ansi-styles');
|
var ansiStyles = require('ansi-styles');
|
||||||
var stripAnsi = require('strip-ansi');
|
var stripAnsi = require('strip-ansi');
|
||||||
var supportsColor = require('supports-color');
|
var supportsColor = require('supports-color');
|
||||||
|
|
@ -11,6 +12,8 @@ var styles = (function () {
|
||||||
ansiStyles.grey = ansiStyles.gray;
|
ansiStyles.grey = ansiStyles.gray;
|
||||||
|
|
||||||
Object.keys(ansiStyles).forEach(function (key) {
|
Object.keys(ansiStyles).forEach(function (key) {
|
||||||
|
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
||||||
|
|
||||||
ret[key] = {
|
ret[key] = {
|
||||||
get: function () {
|
get: function () {
|
||||||
this._styles.push(key);
|
this._styles.push(key);
|
||||||
|
|
@ -22,19 +25,6 @@ var styles = (function () {
|
||||||
return ret;
|
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() {
|
function init() {
|
||||||
var ret = {};
|
var ret = {};
|
||||||
|
|
||||||
|
|
@ -50,12 +40,11 @@ function init() {
|
||||||
|
|
||||||
return self._styles.reduce(function (str, name) {
|
return self._styles.reduce(function (str, name) {
|
||||||
var code = ansiStyles[name];
|
var code = ansiStyles[name];
|
||||||
return str ?
|
return str ? code.open +
|
||||||
// Replace any instances already present with a re-opening code
|
// 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
|
// otherwise only the part of the string until said closing code
|
||||||
// simply 'plain'.
|
// will be colored, and the rest will simply be 'plain'.
|
||||||
code.open + str.replace(code.closeRe, code.open) + code.close :
|
str.replace(code.closeRe, code.open) + code.close : '';
|
||||||
'';
|
|
||||||
}, str);
|
}, str);
|
||||||
}, styles);
|
}, styles);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ansi-styles": "^1.1.0",
|
"ansi-styles": "^1.1.0",
|
||||||
|
"escape-string-regexp": "^1.0.0",
|
||||||
"strip-ansi": "^0.2.0",
|
"strip-ansi": "^0.2.0",
|
||||||
"supports-color": "^0.2.0"
|
"supports-color": "^0.2.0"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
2
test.js
2
test.js
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var chalk = require('./index');
|
var chalk = require('./');
|
||||||
|
|
||||||
describe('chalk', function () {
|
describe('chalk', function () {
|
||||||
it('should style string', function () {
|
it('should style string', function () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue