Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searches with regex control characters should not crash #1579

Merged
merged 1 commit into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion frontend/lib/prepare-examples.js
Original file line number Diff line number Diff line change
@@ -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(' ');
Expand All @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions frontend/lib/prepare-examples.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down