From 454fd0f1b2dddb6a26bc665756c1881d4cae4f10 Mon Sep 17 00:00:00 2001
From: Raymond Feng <enjoyjava@gmail.com>
Date: Fri, 30 Oct 2020 09:15:39 -0700
Subject: [PATCH] Fix value equality test to avoid toString

- The where statement can be something like {toString: 'not a function'}
- Avoid object string comparison

Signed-off-by: Raymond Feng <enjoyjava@gmail.com>
---
 lib/connectors/memory.js | 12 ++++++++----
 test/memory.test.js      | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js
index e821956b..6236c6fc 100644
--- a/lib/connectors/memory.js
+++ b/lib/connectors/memory.js
@@ -613,8 +613,8 @@ function applyFilter(filter) {
       return value.match(example);
     }
 
-    if (example === undefined) {
-      return undefined;
+    if (example == null) {
+      return value == null;
     }
 
     if (typeof example === 'object' && example !== null) {
@@ -682,9 +682,13 @@ function applyFilter(filter) {
         return true;
       }
     }
+
+    // compare date
+    if (example instanceof Date && value instanceof Date) {
+      return example.getTime() === value.getTime();
+    }
     // not strict equality
-    return (example !== null ? example.toString() : example) ==
-      (value != null ? value.toString() : value);
+    return example == value;
   }
 
   /**
diff --git a/test/memory.test.js b/test/memory.test.js
index aeb82116..af2b3fe5 100644
--- a/test/memory.test.js
+++ b/test/memory.test.js
@@ -339,6 +339,15 @@ describe('Memory connector', function() {
         });
     });
 
+    it('should successfully extract 1 user (Lennon) from the db by date', function(done) {
+      User.find({where: {birthday: new Date('1980-12-08')}},
+        function(err, users) {
+          should(users.length).be.equal(1);
+          should(users[0].name).be.equal('John Lennon');
+          done();
+        });
+    });
+
     it('should successfully extract 2 users from the db', function(done) {
       User.find({where: {birthday: {between: [new Date(1940, 0), new Date(1990, 0)]}}},
         function(err, users) {
@@ -565,6 +574,33 @@ describe('Memory connector', function() {
       });
     });
 
+    it('should handle constructor.prototype', function(done) {
+      User.find({where: {'constructor.prototype': {toString: 'Not a function'}}}, function(err,
+        users) {
+        should.not.exist(err);
+        users.length.should.equal(0);
+        done();
+      });
+    });
+
+    it('should handle constructor/prototype', function(done) {
+      User.find({where: {constructor: {prototype: {toString: 'Not a function'}}}}, function(err,
+        users) {
+        should.not.exist(err);
+        users.length.should.equal(0);
+        done();
+      });
+    });
+
+    it('should handle toString', function(done) {
+      User.find({where: {toString: 'Not a function'}}, function(err,
+        users) {
+        should.not.exist(err);
+        users.length.should.equal(0);
+        done();
+      });
+    });
+
     function seed(done) {
       const beatles = [
         {