Skip to content

Instantly share code, notes, and snippets.

@gilzoide
Created October 3, 2024 16:28
Show Gist options
  • Save gilzoide/5af002658f04ed3bbfea79cab3243ac2 to your computer and use it in GitHub Desktop.
Save gilzoide/5af002658f04ed3bbfea79cab3243ac2 to your computer and use it in GitHub Desktop.
Simple SQL minification for JavaScript using regexes
/**
* 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