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

feat(planter filter): include partial matches in results #165

Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -19,13 +21,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 @@ -35,13 +37,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-insesnsitive 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`);
});
});