minify-groq
v1.0.0
Published
Minifies GROQ-queries, removing unnecessary whitespace
Downloads
2,003
Readme
minify-groq
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.
Disclaimer
Still early days - please report any bugs you find.
Installation
$ npm install --save minify-groq
Usage
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]
Browser support
Internet Explorer 9 and up!
License
MIT licensed. See LICENSE.