Skip to content
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

Add more operator to query. #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 19 additions & 20 deletions src/firestore-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down