Skip to content

Commit 7f01c53

Browse files
Eduard Bosch BertranEduard Bosch Bertran
Eduard Bosch Bertran
authored and
Eduard Bosch Bertran
committed
feat: Search $all strings starting with given strings
1 parent 05ba963 commit 7f01c53

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/ParseQuery.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ export default class ParseQuery {
229229
return this;
230230
}
231231

232+
/**
233+
* Converts string for regular expression at the beginning
234+
*/
235+
_regexStartWith(string: string): String {
236+
return '^' + quote(string);
237+
}
238+
232239
/**
233240
* Returns a JSON representation of this query.
234241
* @method toJSON
@@ -682,6 +689,27 @@ export default class ParseQuery {
682689
return this._addCondition(key, '$all', values);
683690
}
684691

692+
/**
693+
* Adds a constraint to the query that requires a particular key's value to
694+
* contain each one of the provided list of values starting with given strings.
695+
* @method containsAllStartingWith
696+
* @param {String} key The key to check. This key's value must be an array.
697+
* @param {Array<String>} values The string values that will match as starting string.
698+
* @return {Parse.Query} Returns the query, so you can chain this call.
699+
*/
700+
containsAllStartingWith(key: string, values: Array<string>): ParseQuery {
701+
var _this = this;
702+
if (!Array.isArray(values)) {
703+
values = [values];
704+
}
705+
706+
values = values.map(function (value) {
707+
return {"$regex": _this._regexStartWith(value)};
708+
});
709+
710+
return this.containsAll(key, values);
711+
}
712+
685713
/**
686714
* Adds a constraint for finding objects that contain the given key.
687715
* @method exists
@@ -826,7 +854,7 @@ export default class ParseQuery {
826854
if (typeof value !== 'string') {
827855
throw new Error('The value being searched for must be a string.');
828856
}
829-
return this._addCondition(key, '$regex', '^' + quote(value));
857+
return this._addCondition(key, '$regex', this._regexStartWith(value));
830858
}
831859

832860
/**

src/__tests__/ParseQuery-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,33 @@ describe('ParseQuery', () => {
264264
});
265265
});
266266

267+
it('can generate contains-all-starting-with queries', () => {
268+
var q = new ParseQuery('Item');
269+
q.containsAllStartingWith('tags', ['ho', 'out']);
270+
expect(q.toJSON()).toEqual({
271+
where: {
272+
tags: {
273+
$all: [
274+
{$regex: '^\\Qho\\E'},
275+
{$regex: '^\\Qout\\E'}
276+
]
277+
}
278+
}
279+
});
280+
281+
q.containsAllStartingWith('tags', ['sal', 'ne']);
282+
expect(q.toJSON()).toEqual({
283+
where: {
284+
tags: {
285+
$all: [
286+
{$regex: '^\\Qsal\\E'},
287+
{$regex: '^\\Qne\\E'}
288+
]
289+
}
290+
}
291+
});
292+
});
293+
267294
it('can generate exists queries', () => {
268295
var q = new ParseQuery('Item');
269296
q.exists('name');

0 commit comments

Comments
 (0)