Minifies a GROQ-query by reducing unnecessary whitespace.
Note: This module is not using full GROQ-parser, so it doesn't know where whitespace is "required". Thus, it can only reduce the amount of whitespace, not fully remove it. It does however make sure to not minify whitespace inside of GROQ strings.
Still early days - please report any bugs you find.
$ npm install --save minify-groq
You can either use this module as a tagged template literal, or as a function. Placeholders used inside of a template will be JSON-encoded automatically.
import groq from 'minify-groq'
const tag = 'fantasy'
const query = groq`
*[_type == "book" && ${tag} in tags] {
title,
publishDate
}
`
// Output (roughly speaking):
// *[_type == "book" && "fantasy" in tags] { title, publishDate }
... or, as a function:
import minifyGroq from 'minify-groq'
const query = minifyGroq(`
*[
_type == "author" &&
birthYear > 1950
] {
name,
birthYear,
"books": books[]->{
title
}
} | order (birthYear asc) | [ 0 ... 50 ]
`)
console.log(query)
// Output (roughly speaking):
// *[ _type == "author" && birthYear > 1950] { name, birthYear, "books": books[]->{ title } } | order(birthYear asc) | [0 ... 50]
Internet Explorer 9 and up!
MIT licensed. See LICENSE.