Use a Map and some minor regex tweaks

This commit is contained in:
Sindre Sorhus 2017-08-07 17:41:16 +02:00
parent 38f641a222
commit a9f0c77142
2 changed files with 16 additions and 16 deletions

View file

@ -47,7 +47,7 @@
"devDependencies": { "devDependencies": {
"ava": "*", "ava": "*",
"coveralls": "^2.11.2", "coveralls": "^2.11.2",
"execa": "^0.7.0", "execa": "^0.8.0",
"import-fresh": "^2.0.0", "import-fresh": "^2.0.0",
"matcha": "^0.7.0", "matcha": "^0.7.0",
"nyc": "^11.0.2", "nyc": "^11.0.2",

View file

@ -1,28 +1,28 @@
'use strict'; 'use strict';
const TEMPLATE_REGEX = /(?:\\(u[a-f0-9]{4}|x[a-f0-9]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi; const TEMPLATE_REGEX = /(?:\\(u[a-f\d]{4}|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g; const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/; const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
const ESCAPE_REGEX = /\\(u[0-9a-f]{4}|x[0-9a-f]{2}|.)|([^\\])/gi; const ESCAPE_REGEX = /\\(u[a-f\d]{4}|x[a-f\d]{2}|.)|([^\\])/gi;
const ESCAPES = { const ESCAPES = new Map([
n: '\n', ['n', '\n'],
r: '\r', ['r', '\r'],
t: '\t', ['t', '\t'],
b: '\b', ['b', '\b'],
f: '\f', ['f', '\f'],
v: '\v', ['v', '\v'],
0: '\0', ['0', '\0'],
'\\': '\\', ['\\', '\\'],
e: '\u001b', ['e', '\u001B'],
a: '\u0007' ['a', '\u0007']
}; ]);
function unescape(c) { function unescape(c) {
if ((c[0] === 'u' && c.length === 5) || (c[0] === 'x' && c.length === 3)) { if ((c[0] === 'u' && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
return String.fromCharCode(parseInt(c.slice(1), 16)); return String.fromCharCode(parseInt(c.slice(1), 16));
} }
return ESCAPES[c] || c; return ESCAPES.get(c) || c;
} }
function parseArguments(name, args) { function parseArguments(name, args) {