Skip to content

Add modifiers to startsWith / endsWith #1306

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

Merged
merged 1 commit into from
Feb 23, 2021
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
28 changes: 27 additions & 1 deletion integration/test/ParseQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ describe('Parse Query', () => {
});
});

it('can test case insensitive regex', done => {
it('can test case insensitive matches', done => {
const obj = new TestObject();
obj.set('myString', 'hockey');
obj
Expand All @@ -1175,6 +1175,32 @@ describe('Parse Query', () => {
});
});

it('can test case insensitive startsWith', async () => {
const obj = new TestObject();
obj.set('myString', 'basketball');
await obj.save();

const query = new Parse.Query(TestObject);
query.startsWith('myString', 'baSKet', 'i');
const results = await query.find();

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].get('myString'), 'basketball');
});

it('can test case insensitive endsWith', async () => {
const obj = new TestObject();
obj.set('myString', 'basketball');
await obj.save();

const query = new Parse.Query(TestObject);
query.endsWith('myString', 'tBAll', 'i');
const results = await query.find();

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].get('myString'), 'basketball');
});

it('fails for invalid regex options', done => {
const query = new Parse.Query(TestObject);
query.matches('myString', 'football', 'some invalid thing');
Expand Down
10 changes: 6 additions & 4 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1573,13 +1573,14 @@ class ParseQuery {
*
* @param {string} key The key that the string to match is stored in.
* @param {string} prefix The substring that the value must start with.
* @param {string} modifiers The regular expression mode.
* @returns {Parse.Query} Returns the query, so you can chain this call.
*/
startsWith(key: string, prefix: string): ParseQuery {
startsWith(key: string, prefix: string, modifiers: string): ParseQuery {
if (typeof prefix !== 'string') {
throw new Error('The value being searched for must be a string.');
}
return this._addCondition(key, '$regex', this._regexStartWith(prefix));
return this.matches(key, this._regexStartWith(prefix), modifiers);
}

/**
Expand All @@ -1588,13 +1589,14 @@ class ParseQuery {
*
* @param {string} key The key that the string to match is stored in.
* @param {string} suffix The substring that the value must end with.
* @param {string} modifiers The regular expression mode.
* @returns {Parse.Query} Returns the query, so you can chain this call.
*/
endsWith(key: string, suffix: string): ParseQuery {
endsWith(key: string, suffix: string, modifiers: string): ParseQuery {
if (typeof suffix !== 'string') {
throw new Error('The value being searched for must be a string.');
}
return this._addCondition(key, '$regex', quote(suffix) + '$');
return this.matches(key, quote(suffix) + '$', modifiers);
}

/**
Expand Down