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:
35
client/src/utils/curriculum-helpers.js
Normal file
35
client/src/utils/curriculum-helpers.js
Normal 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
|
||||
};
|
Reference in New Issue
Block a user