Created
October 3, 2024 16:28
-
-
Save gilzoide/5af002658f04ed3bbfea79cab3243ac2 to your computer and use it in GitHub Desktop.
Simple SQL minification for JavaScript using regexes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Minify a SQL query. | |
* SQL minification removes comments and unnecessary whitespace. | |
* WARNING: this method will condense whitespace from string literals, beware! | |
* @param {string} sql SQL raw query | |
* @returns {string} Minified SQL query | |
*/ | |
function minifySql(sql) { | |
return sql | |
.replace(/--[^\n]*/g, '') // remove line comments | |
.replace(/\s+/g, ' ') // condense all whitespace into single spaces | |
.replace(/(?<=[(]) /g, '') // remove spacing after parentheses | |
.replace(/ (?=[)])/g, '') // remove spacing before parentheses | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment