ESM: Ported case converters, generic beautifier and syntax highlighting

This commit is contained in:
Matt C 2018-05-26 18:04:53 +01:00
parent 176e83a79f
commit 905bc6699e
6 changed files with 428 additions and 0 deletions

31
src/core/lib/Code.mjs Normal file
View file

@ -0,0 +1,31 @@
/**
* Code functions.
*
* @author n1474335 [n1474335@gmail.com]
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*
*/
/**
* This tries to rename variable names in a code snippet according to a function.
*
* @param {string} input
* @param {function} replacer - this function will be fed the token which should be renamed.
* @returns {string}
*/
export function replaceVariableNames(input, replacer) {
const tokenRegex = /\\"|"(?:\\"|[^"])*"|(\b[a-z0-9\-_]+\b)/ig;
return input.replace(tokenRegex, (...args) => {
const match = args[0],
quotes = args[1];
if (!quotes) {
return match;
} else {
return replacer(match);
}
});
}