feat(client, learn): add helper functions for common validation operations (#38605)

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
This commit is contained in:
Hassaan Pasha
2020-09-17 19:08:01 +05:00
committed by GitHub
parent aecbc28798
commit 80438cac3e
54 changed files with 326 additions and 157 deletions

View File

@@ -0,0 +1,35 @@
import { parse } from '@babel/parser';
import generate from '@babel/generator';
const removeHtmlComments = str => str.replace(/<!--(.|\s)*?-->/g, '');
const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, '');
const removeJSComments = codeStr => {
// Note: removes trailing new lines and tailing spaces at end of lines
const options = {
comments: false,
retainLines: true,
compact: false,
concise: false,
minified: false
};
try {
const ast = parse(codeStr);
const { code } = generate(ast, options, codeStr);
return code;
} catch (err) {
return codeStr;
}
};
const removeWhiteSpace = (str = '') => {
return str.replace(/\s/g, '');
};
export default {
removeHtmlComments,
removeCssComments,
removeJSComments,
removeWhiteSpace
};