Skip to content

Commit

Permalink
feat(planter filter): include partial matches in results (#165)
Browse files Browse the repository at this point in the history
* feat(planter filter): include partial matches in results

* refactor(planter filter): move stringToSearchRegExp function to utilities

* doc: fix typo in comment

Co-authored-by: Nick Charlton <[email protected]>
  • Loading branch information
Mloweedgar and nmcharlton authored Sep 24, 2021
1 parent 3bc15bc commit 90d493e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/models/FilterPlanter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/*
import { stringToSearchRegExp } from '../utilities';

/**
* A simple model for planter filter
*/

Expand All @@ -22,13 +24,13 @@ export default class Filter {

if (this.firstName) {
where.firstName = {
ilike: this.firstName,
regexp: stringToSearchRegExp(this.firstName),
};
}

if (this.lastName) {
where.lastName = {
ilike: this.lastName,
regexp: stringToSearchRegExp(this.lastName),
};
}

Expand All @@ -40,13 +42,13 @@ export default class Filter {

if (this.email) {
where.email = {
ilike: this.email,
regexp: stringToSearchRegExp(this.email),
};
}

if (this.phone) {
where.phone = {
ilike: this.phone,
regexp: stringToSearchRegExp(this.phone),
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @function
* @name stringToSearchRegExp
* @description Converts a string to a case-insensitive regular expression
* that can be used to search for string patterns.
*
* @param {string} value The string to convert.
* @returns {string} A regular expression string
*/
export const stringToSearchRegExp = (value) => `/.*${value}.*/i`;
8 changes: 8 additions & 0 deletions src/utilities/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { stringToSearchRegExp } from './';

describe('converts string to regexp', () => {
it('it should convert string to regexp', () => {
const result = stringToSearchRegExp('test');
expect(result).toEqual(`/.*test.*/i`);
});
});

0 comments on commit 90d493e

Please sign in to comment.