Skip to content

Commit f31929b

Browse files
authored
Add modifiers to startsWith / endsWith (#1306)
1 parent 1abb841 commit f31929b

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

integration/test/ParseQueryTest.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ describe('Parse Query', () => {
11581158
});
11591159
});
11601160

1161-
it('can test case insensitive regex', done => {
1161+
it('can test case insensitive matches', done => {
11621162
const obj = new TestObject();
11631163
obj.set('myString', 'hockey');
11641164
obj
@@ -1175,6 +1175,32 @@ describe('Parse Query', () => {
11751175
});
11761176
});
11771177

1178+
it('can test case insensitive startsWith', async () => {
1179+
const obj = new TestObject();
1180+
obj.set('myString', 'basketball');
1181+
await obj.save();
1182+
1183+
const query = new Parse.Query(TestObject);
1184+
query.startsWith('myString', 'baSKet', 'i');
1185+
const results = await query.find();
1186+
1187+
assert.strictEqual(results.length, 1);
1188+
assert.strictEqual(results[0].get('myString'), 'basketball');
1189+
});
1190+
1191+
it('can test case insensitive endsWith', async () => {
1192+
const obj = new TestObject();
1193+
obj.set('myString', 'basketball');
1194+
await obj.save();
1195+
1196+
const query = new Parse.Query(TestObject);
1197+
query.endsWith('myString', 'tBAll', 'i');
1198+
const results = await query.find();
1199+
1200+
assert.strictEqual(results.length, 1);
1201+
assert.strictEqual(results[0].get('myString'), 'basketball');
1202+
});
1203+
11781204
it('fails for invalid regex options', done => {
11791205
const query = new Parse.Query(TestObject);
11801206
query.matches('myString', 'football', 'some invalid thing');

src/ParseQuery.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,13 +1573,14 @@ class ParseQuery {
15731573
*
15741574
* @param {string} key The key that the string to match is stored in.
15751575
* @param {string} prefix The substring that the value must start with.
1576+
* @param {string} modifiers The regular expression mode.
15761577
* @returns {Parse.Query} Returns the query, so you can chain this call.
15771578
*/
1578-
startsWith(key: string, prefix: string): ParseQuery {
1579+
startsWith(key: string, prefix: string, modifiers: string): ParseQuery {
15791580
if (typeof prefix !== 'string') {
15801581
throw new Error('The value being searched for must be a string.');
15811582
}
1582-
return this._addCondition(key, '$regex', this._regexStartWith(prefix));
1583+
return this.matches(key, this._regexStartWith(prefix), modifiers);
15831584
}
15841585

15851586
/**
@@ -1588,13 +1589,14 @@ class ParseQuery {
15881589
*
15891590
* @param {string} key The key that the string to match is stored in.
15901591
* @param {string} suffix The substring that the value must end with.
1592+
* @param {string} modifiers The regular expression mode.
15911593
* @returns {Parse.Query} Returns the query, so you can chain this call.
15921594
*/
1593-
endsWith(key: string, suffix: string): ParseQuery {
1595+
endsWith(key: string, suffix: string, modifiers: string): ParseQuery {
15941596
if (typeof suffix !== 'string') {
15951597
throw new Error('The value being searched for must be a string.');
15961598
}
1597-
return this._addCondition(key, '$regex', quote(suffix) + '$');
1599+
return this.matches(key, quote(suffix) + '$', modifiers);
15981600
}
15991601

16001602
/**

0 commit comments

Comments
 (0)