Repository: parse-community/Parse-SDK-JS
Pull request #437 (2017-05-26 07:10:11)

New query condition to match all strings that starts with some other given strings
Hi to all! 🎉 

This is the same contribution as the link below, but for Parse-SDK-JS
## https://github.com/parse-community/Parse-SDK-Android/pull/673

😃 

PD: @flovilmart  is ti ok to revert the _dist & lib_ folders commit or should I delete this commits?

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=h1) Report
> Merging [#437](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/05ba96379a5bc648368b7a0df7e6ce9d690ad002?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `90%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437/graphs/tree.svg?token=8nCENM0QHj&src=pr&width=650&height=150)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master    #437      +/-   ##
=========================================
+ Coverage   84.49%   84.5%   +0.01%     
=========================================
  Files          46      46              
  Lines        3696    3705       +9     
  Branches      846     847       +1     
=========================================
+ Hits         3123    3131       +8     
- Misses        573     574       +1
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseQuery.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=tree#diff-c3JjL1BhcnNlUXVlcnkuanM=) | `94.95% <90%> (-0.17%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=footer). Last update [05ba963...9393e0c](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/437?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

flovilmart: waiting on parse-community/parse-server#3864 to land before we merge.

Patch:
diff --git a/src/ParseQuery.js b/src/ParseQuery.js
index cfcc6fd..f17d484 100644
--- a/src/ParseQuery.js
+++ b/src/ParseQuery.js
@@ -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.
@@ -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.
@@ -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));
   }
 
   /**


Test patch:
diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js
index 0bc2ed9..bf88f2d 100644
--- a/src/__tests__/ParseQuery-test.js
+++ b/src/__tests__/ParseQuery-test.js
@@ -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');