Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/lib/Filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export const Constraints = {
composable: true,
comparable: true,
},
contains: {
name: 'contains substring',
field: 'String',
composable: true,
comparable: true,
},
before: {
name: 'is before',
field: 'Date',
Expand Down Expand Up @@ -185,7 +191,18 @@ export const FieldConstraints = {
Pointer: ['exists', 'dne', 'eq', 'neq', 'starts', 'containedIn', 'unique'],
Boolean: ['exists', 'dne', 'eq', 'neq', 'containedIn', 'unique'],
Number: ['exists', 'dne', 'eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'containedIn', 'unique'],
String: ['exists', 'dne', 'eq', 'neq', 'starts', 'ends', 'matches', 'containedIn', 'unique'],
String: [
'exists',
'dne',
'eq',
'neq',
'starts',
'ends',
'matches',
'contains',
'containedIn',
'unique',
],
Date: [
'exists',
'dne',
Expand Down
3 changes: 3 additions & 0 deletions src/lib/queryFromFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ export function addConstraintFromValues(query, field, constraint, compareTo, mod
case 'matches':
query.matches(field, String(compareTo), modifiers);
break;
case 'contains':
query.contains(field, String(compareTo));
break;
case 'keyExists':
query.exists(field + '.' + compareTo);
break;
Expand Down
37 changes: 37 additions & 0 deletions src/lib/tests/queryFromFilters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
jest.dontMock('../queryFromFilters');

const { addConstraintFromValues } = require('../queryFromFilters');
const Parse = require('parse');

function whereFor(field, constraint, compareTo, modifiers) {
const query = new Parse.Query('Test');
addConstraintFromValues(query, field, constraint, compareTo, modifiers);
return query.toJSON().where;
}

describe('addConstraintFromValues contains constraint', () => {
it('matches a plain substring literally', () => {
expect(whereFor('name', 'contains', 'foo')).toEqual({
name: { $regex: '\\Qfoo\\E' },
});
});

it('escapes regex metacharacters so they match literally', () => {
expect(whereFor('name', 'contains', 'a.b(c)')).toEqual({
name: { $regex: '\\Qa.b(c)\\E' },
});
});

it('keeps matches as a raw, unescaped regex with modifiers', () => {
expect(whereFor('name', 'matches', 'a.b(c)', 'i')).toEqual({
name: { $regex: 'a.b(c)', $options: 'i' },
});
});
});
Loading