Skip to content

Commit 0fbc604

Browse files
eduardboschdplewis
authored andcommitted
New query condition to match all strings that starts with some other given strings (#437)
* feat: Search $all strings starting with given strings * feat: Include dist & lib folders * Revert "feat: Include dist & lib folders" This reverts commit 4494b86.
1 parent a395c5b commit 0fbc604

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
@@ -273,6 +273,13 @@ class ParseQuery {
273273
return this;
274274
}
275275

276+
/**
277+
* Converts string for regular expression at the beginning
278+
*/
279+
_regexStartWith(string: string): String {
280+
return '^' + quote(string);
281+
}
282+
276283
/**
277284
* Returns a JSON representation of this query.
278285
* @return {Object} The JSON representation of the query.
@@ -864,6 +871,27 @@ class ParseQuery {
864871
return this._addCondition(key, '$all', values);
865872
}
866873

874+
/**
875+
* Adds a constraint to the query that requires a particular key's value to
876+
* contain each one of the provided list of values starting with given strings.
877+
* @method containsAllStartingWith
878+
* @param {String} key The key to check. This key's value must be an array.
879+
* @param {Array<String>} values The string values that will match as starting string.
880+
* @return {Parse.Query} Returns the query, so you can chain this call.
881+
*/
882+
containsAllStartingWith(key: string, values: Array<string>): ParseQuery {
883+
var _this = this;
884+
if (!Array.isArray(values)) {
885+
values = [values];
886+
}
887+
888+
values = values.map(function (value) {
889+
return {"$regex": _this._regexStartWith(value)};
890+
});
891+
892+
return this.containsAll(key, values);
893+
}
894+
867895
/**
868896
* Adds a constraint for finding objects that contain the given key.
869897
* @param {String} key The key that should exist.
@@ -1033,7 +1061,7 @@ class ParseQuery {
10331061
if (typeof value !== 'string') {
10341062
throw new Error('The value being searched for must be a string.');
10351063
}
1036-
return this._addCondition(key, '$regex', '^' + quote(value));
1064+
return this._addCondition(key, '$regex', this._regexStartWith(value));
10371065
}
10381066

10391067
/**

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)