-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[groq] Add groq package for use with tagged template literals (#1109)
- Loading branch information
Showing
7 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage | ||
.grunt | ||
|
||
# Dependency directories | ||
node_modules | ||
|
||
# Compiled code | ||
lib |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# GROQ tagged template literal | ||
|
||
This module exports a single function that can be called with an ES2015 [template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) to signal that it represents a GROQ-query. | ||
|
||
The result will be the exact same string as the input, - this is currently helpful for getting syntax highlighting in editors, but in the future it might also parse and validate queries, strip unncessary whitespace and similar. | ||
|
||
Pairs well with [vscode-sanity](https://github.com/sanity-io/vscode-sanity)! | ||
|
||
## Installing | ||
|
||
``` | ||
npm install --save groq | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import groq from 'groq' | ||
|
||
const query = groq`*[_type == 'products'][0...10]` | ||
``` | ||
|
||
## What is Sanity? What is GROQ? | ||
|
||
[Sanity](https://www.sanity.io) is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches. | ||
|
||
To get started with Sanity, please head over to our [getting started guide](https://sanity.io/docs/introduction/getting-started) | ||
|
||
## License | ||
|
||
MIT-licensed. See LICENSE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "groq", | ||
"version": "0.137.0", | ||
"description": "Tagged template literal for Sanity.io GROQ-queries", | ||
"main": "lib/groq.js", | ||
"directories": { | ||
"lib": "lib", | ||
"test": "test" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "node test/groq.test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/sanity-io/sanity.git" | ||
}, | ||
"keywords": [ | ||
"groq", | ||
"tagged", | ||
"template", | ||
"literal", | ||
"string", | ||
"sanity", | ||
"cms", | ||
"headless", | ||
"realtime", | ||
"content" | ||
], | ||
"author": "Sanity.io <hello@sanity.io>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/sanity-io/sanity/issues" | ||
}, | ||
"homepage": "https://www.sanity.io/" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* eslint-disable import/no-commonjs */ | ||
module.exports = function groq(strings, ...keys) { | ||
const lastIndex = strings.length - 1 | ||
return ( | ||
strings.slice(0, lastIndex).reduce((acc, str, i) => acc + str + keys[i], '') + | ||
strings[lastIndex] | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* eslint-disable import/no-commonjs */ | ||
const assert = require('assert') | ||
const groq = require('../src/groq') | ||
|
||
assert.equal(groq`foo${'bar'}`, `foo${'bar'}`) | ||
assert.equal(groq`${'bar'}`, `${'bar'}`) | ||
assert.equal(groq``, ``) | ||
assert.equal(groq`${'foo'}`, `${'foo'}`) | ||
assert.equal(groq`${/foo/}bar`, `${/foo/}bar`) | ||
assert.equal(groq`${'foo'}bar${347}`, `${'foo'}bar${347}`) | ||
assert.equal(groq`${'foo'}bar${347}${/qux/}`, `${'foo'}bar${347}${/qux/}`) | ||
assert.equal(groq`${'foo'}${347}qux`, `${'foo'}${347}qux`) |