Skip to content

Commit

Permalink
improves query, allowing multiple words
Browse files Browse the repository at this point in the history
  • Loading branch information
carvilsi committed Sep 4, 2024
1 parent eb9d324 commit d5fc7c7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pwyll",
"version": "5.0.1",
"version": "5.0.2",
"description": "A snippet manager service",
"main": "./lib/index.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/snippets_controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from '../util';
import { logger, buildRegExp } from '../util';
import { getCollection } from '../db/mongo';
import config from 'config';
import { findUserByID } from './users_controller';
Expand Down Expand Up @@ -45,21 +45,21 @@ export async function findSnippetByQuery(
if (userID != null) {
user = await findUserByID(userID);
}
const regExp = new RegExp(`${search}`, 'im');
const regExp = buildRegExp(search);
let mongoQuery;
if (user != null) {
mongoQuery = {
$or: [
{ snippet: { $regex: regExp } },
{ description: { $regex: regExp } },
{ snippet: { $regex: regExp, $options: 'ixm' } },
{ description: { $regex: regExp, $options: 'ixm' } },
],
$and: [{ user: user }],
};
} else {
mongoQuery = {
$or: [
{ snippet: { $regex: regExp } },
{ description: { $regex: regExp } },
{ snippet: { $regex: regExp, $options: 'ixm' } },
{ description: { $regex: regExp, $options: 'ixm' } },
],
};
}
Expand Down
11 changes: 11 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ export async function userExistenceCheck(username: string): Promise<boolean> {
throw `User ${username} already exists, please choose a different one`;
return true;
}

export function buildRegExp(search: string): RegExp {
let regExp;
const splitted = search.trim().replace(/\s+/g, ' ').split(' ');
if (splitted.length) {
regExp = new RegExp(`${splitted.join('|')}`);
} else {
regExp = new RegExp(`${search}`);
}
return regExp;
}
10 changes: 10 additions & 0 deletions test/snippets_read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ describe('snippets read (find)', () => {
expect(response.length).toBeGreaterThanOrEqual(2);
});

test('should find a snippet with different words', async () => {
const res = await request(testGlobals.__PYWLL_SERVER_URL__)
.get('/snippet/find')
.query({ q: 'dev typescript' })
.set('Accept', 'application/json');
expect(res.statusCode).toBe(200);
const response = JSON.parse(res.text);
expect(response.length).toBeGreaterThanOrEqual(1);
});

test('should find a snippet restricted to first user', async () => {
const res = await request(testGlobals.__PYWLL_SERVER_URL__)
.get('/snippet/find')
Expand Down

0 comments on commit d5fc7c7

Please sign in to comment.