Skip to content

Commit dc0163a

Browse files
authored
[1.0] Add tests for parsing regex args + fix bug (#1267)
* Add tests for parsing regex args + fix bug * fix old test
1 parent a80ebd6 commit dc0163a

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Prepare regex for Sift.js handles flags regex 1`] = `/blue/i`;
4+
5+
exports[`Prepare regex for Sift.js handles simple regex 1`] = `/blue/`;
6+
7+
exports[`Prepare regex for Sift.js handles slashes 1`] = `/bl\\\\/ue/i`;

packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe(`GraphQL Input args`, () => {
283283
nodes,
284284
`
285285
{
286-
allNode(filter: {name: { regex: "/^the.*wax/i/" }}) {
286+
allNode(filter: {name: { regex: "/^the.*wax/i" }}) {
287287
edges { node { name }}
288288
}
289289
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const prepareRegex = require(`../prepare-regex`)
2+
3+
describe(`Prepare regex for Sift.js`, () => {
4+
it(`handles simple regex`, () => {
5+
expect(prepareRegex(`/blue/`)).toMatchSnapshot()
6+
})
7+
it(`handles flags regex`, () => {
8+
expect(prepareRegex(`/blue/i`)).toMatchSnapshot()
9+
})
10+
it(`handles slashes`, () => {
11+
expect(prepareRegex(`/bl\/ue/i`)).toMatchSnapshot()
12+
})
13+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const _ = require(`lodash`)
2+
3+
module.exports = str => {
4+
const exploded = str.split(`/`)
5+
const regex = new RegExp(exploded.slice(1, -1).join(`/`), _.last(exploded))
6+
return regex
7+
}

packages/gatsby/src/schema/run-sift.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const _ = require(`lodash`)
44
const { connectionFromArray } = require(`graphql-skip-limit`)
55
const { store } = require(`../redux/`)
66
const { createPageDependency } = require(`../redux/actions/add-page-dependency`)
7+
const prepareRegex = require(`./prepare-regex`)
78

89
type Node = {
910
id: String,
@@ -23,9 +24,7 @@ module.exports = ({ args, nodes, connection = false, path = `` }) => {
2324
} else {
2425
// Compile regex first.
2526
if (k === `regex`) {
26-
const exploded = v.split(`/`)
27-
const regex = new RegExp(exploded[1], exploded[2])
28-
newObject[`$regex`] = regex
27+
newObject[`$regex`] = prepareRegex(v)
2928
} else if (k === `glob`) {
3029
const Minimatch = require(`minimatch`).Minimatch
3130
const mm = new Minimatch(v)

0 commit comments

Comments
 (0)