From f1429adddbf1e9bde1ed2f8a4156c11621f309b1 Mon Sep 17 00:00:00 2001 From: Ejiah Date: Tue, 30 Oct 2018 02:54:48 +0700 Subject: [PATCH 1/2] Add more operator to query. --- src/firestore-query.js | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/firestore-query.js b/src/firestore-query.js index e2b275db..16c31c3d 100644 --- a/src/firestore-query.js +++ b/src/firestore-query.js @@ -125,30 +125,29 @@ MockFirestoreQuery.prototype.stream = function () { MockFirestoreQuery.prototype.where = function (property, operator, value) { var query; - // check if unsupported operator - if (operator !== '==') { + const operatorAllowedArray = ['=', '==', '<', '>', '<=', '>=']; + + let isOperatorAllowed = _.indexOf(operatorAllowedArray, operator) > -1 ; + + if(!isOperatorAllowed){ console.warn('Using unsupported where() operator for firebase-mock, returning entire dataset'); - return this; - } else { - if (_.size(this.data) !== 0) { - var results = {}; + return this; + } + + let results = null; + + if (_.size(this.data) !== 0) { + results = {}; _.forEach(this.data, function(data, key) { - switch (operator) { - case '==': - if (_.isEqual(_.get(data, property), value)) { - results[key] = _.cloneDeep(data); - } - break; - default: - results[key] = _.cloneDeep(data); - break; - } + let dataValue = _.get(data, property); + let conditionString = dataValue + ' ' + operator + value; + + let isTrue = eval(conditionString); + if (isTrue) + results[key] = _.cloneDeep(data); }); - return new MockFirestoreQuery(this.path, results, this.parent, this.id); - } else { - return new MockFirestoreQuery(this.path, null, this.parent, this.id); - } } + return new MockFirestoreQuery(this.path, results, this.parent, this.id); }; MockFirestoreQuery.prototype.orderBy = function (property, direction) { @@ -190,3 +189,5 @@ function extractName(path) { } module.exports = MockFirestoreQuery; +Octotree is enabled on this page. Click this button or press cmd shift s (or ctrl shift s) to show it. +Support us • Feedback? From 1d5f5944d4a15292a1678c1dc9b6e1368f0863d7 Mon Sep 17 00:00:00 2001 From: Ejiah Date: Tue, 30 Oct 2018 05:00:52 +0700 Subject: [PATCH 2/2] Fix firestore-query.js Fix mistake ending line. --- src/firestore-query.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/firestore-query.js b/src/firestore-query.js index 16c31c3d..60d1b5b9 100644 --- a/src/firestore-query.js +++ b/src/firestore-query.js @@ -189,5 +189,3 @@ function extractName(path) { } module.exports = MockFirestoreQuery; -Octotree is enabled on this page. Click this button or press cmd shift s (or ctrl shift s) to show it. -Support us • Feedback?