Searches with regex control characters should not crash (#1579)

Fix #1578
This commit is contained in:
Paul Melnikow
2018-03-17 16:52:41 -04:00
committed by GitHub
parent 6c2ce413b1
commit 3a47bb38ac
3 changed files with 25 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
import escapeStringRegexp from 'escape-string-regexp';
export function exampleMatchesRegex(example, regex) {
const { title, keywords } = example;
const haystack = [title].concat(keywords).join(' ');
@@ -6,7 +8,8 @@ export function exampleMatchesRegex(example, regex) {
export function predicateFromQuery(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);
} else {
return () => true;

View 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);
});
});

View File

@@ -25,6 +25,7 @@
"camp": "~17.2.1",
"chrome-web-store-item-property": "~1.1.2",
"dot": "~1.1.2",
"escape-string-regexp": "^1.0.5",
"gm": "^1.23.0",
"json-autosave": "~1.1.2",
"jsonpath": "~1.0.0",