UNPKG

threeify-glsl-transpiler

Version:
28 lines (23 loc) 959 B
export function stripComments(source: string): string { const commentRegex = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm; // https://stackoverflow.com/a/15123777 return source.replace(commentRegex, ''); } export function stripUnnecessaryLineEndings(source: string): string { return source.replace(/[\r\n]+/g, '\n'); } export function stripUnnecessarySpaces(source: string): string { const specialChars = '(),=;+-*/&|%~.:[]?'.split(''); // remove duplicated spaces source = source.replace(/[ \t]+/g, ' '); // remove spaces between symbols that do not need to be separated for (let i = 0; i < specialChars.length; i++) { let lastLength = 0; // TODO: Fix this horribly inefficient algorithm while (lastLength !== source.length) { lastLength = source.length; source = source.replace(specialChars[i] + ' ', specialChars[i]); source = source.replace(' ' + specialChars[i], specialChars[i]); } } return source; }