Searches with regex control characters should not crash (#1579)
Fix #1578
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import escapeStringRegexp from 'escape-string-regexp';
|
||||||
|
|
||||||
export function exampleMatchesRegex(example, regex) {
|
export function exampleMatchesRegex(example, regex) {
|
||||||
const { title, keywords } = example;
|
const { title, keywords } = example;
|
||||||
const haystack = [title].concat(keywords).join(' ');
|
const haystack = [title].concat(keywords).join(' ');
|
||||||
@@ -6,7 +8,8 @@ export function exampleMatchesRegex(example, regex) {
|
|||||||
|
|
||||||
export function predicateFromQuery(query) {
|
export function predicateFromQuery(query) {
|
||||||
if (query) {
|
if (query) {
|
||||||
const regex = new RegExp(query, 'i'); // Case-insensitive.
|
const escaped = escapeStringRegexp(query);
|
||||||
|
const regex = new RegExp(escaped, 'i'); // Case-insensitive.
|
||||||
return example => exampleMatchesRegex(example, regex);
|
return example => exampleMatchesRegex(example, regex);
|
||||||
} else {
|
} else {
|
||||||
return () => true;
|
return () => true;
|
||||||
|
|||||||
20
frontend/lib/prepare-examples.spec.js
Normal file
20
frontend/lib/prepare-examples.spec.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { test, given, forCases } from 'sazerac';
|
||||||
|
import { predicateFromQuery } from './prepare-examples';
|
||||||
|
|
||||||
|
describe('Badge example functions', function() {
|
||||||
|
const exampleMatchesQuery =
|
||||||
|
(example, query) => predicateFromQuery(query)(example);
|
||||||
|
|
||||||
|
test(exampleMatchesQuery, () => {
|
||||||
|
forCases([
|
||||||
|
given({ title: 'node version' }, 'npm'),
|
||||||
|
]).expect(false);
|
||||||
|
|
||||||
|
forCases([
|
||||||
|
given({ title: 'node version', keywords: ['npm'] }, 'node'),
|
||||||
|
given({ title: 'node version', keywords: ['npm'] }, 'npm'),
|
||||||
|
// https://github.com/badges/shields/issues/1578
|
||||||
|
given({ title: 'c++ is the best language' }, 'c++'),
|
||||||
|
]).expect(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
"camp": "~17.2.1",
|
"camp": "~17.2.1",
|
||||||
"chrome-web-store-item-property": "~1.1.2",
|
"chrome-web-store-item-property": "~1.1.2",
|
||||||
"dot": "~1.1.2",
|
"dot": "~1.1.2",
|
||||||
|
"escape-string-regexp": "^1.0.5",
|
||||||
"gm": "^1.23.0",
|
"gm": "^1.23.0",
|
||||||
"json-autosave": "~1.1.2",
|
"json-autosave": "~1.1.2",
|
||||||
"jsonpath": "~1.0.0",
|
"jsonpath": "~1.0.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user