Skip to content

New query condition to match all strings that starts with some other given strings #437

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
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
30 changes: 29 additions & 1 deletion src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ class ParseQuery {
return this;
}

/**
* Converts string for regular expression at the beginning
*/
_regexStartWith(string: string): String {
return '^' + quote(string);
}

/**
* Returns a JSON representation of this query.
* @return {Object} The JSON representation of the query.
Expand Down Expand Up @@ -864,6 +871,27 @@ class ParseQuery {
return this._addCondition(key, '$all', values);
}

/**
* Adds a constraint to the query that requires a particular key's value to
* contain each one of the provided list of values starting with given strings.
* @method containsAllStartingWith
* @param {String} key The key to check. This key's value must be an array.
* @param {Array<String>} values The string values that will match as starting string.
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
containsAllStartingWith(key: string, values: Array<string>): ParseQuery {
var _this = this;
if (!Array.isArray(values)) {
values = [values];
}

values = values.map(function (value) {
return {"$regex": _this._regexStartWith(value)};
});

return this.containsAll(key, values);
}

/**
* Adds a constraint for finding objects that contain the given key.
* @param {String} key The key that should exist.
Expand Down Expand Up @@ -1033,7 +1061,7 @@ class ParseQuery {
if (typeof value !== 'string') {
throw new Error('The value being searched for must be a string.');
}
return this._addCondition(key, '$regex', '^' + quote(value));
return this._addCondition(key, '$regex', this._regexStartWith(value));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@ describe('ParseQuery', () => {
});
});

it('can generate contains-all-starting-with queries', () => {
var q = new ParseQuery('Item');
q.containsAllStartingWith('tags', ['ho', 'out']);
expect(q.toJSON()).toEqual({
where: {
tags: {
$all: [
{$regex: '^\\Qho\\E'},
{$regex: '^\\Qout\\E'}
]
}
}
});

q.containsAllStartingWith('tags', ['sal', 'ne']);
expect(q.toJSON()).toEqual({
where: {
tags: {
$all: [
{$regex: '^\\Qsal\\E'},
{$regex: '^\\Qne\\E'}
]
}
}
});
});

it('can generate exists queries', () => {
var q = new ParseQuery('Item');
q.exists('name');
Expand Down