Skip to content

Commit 05898ed

Browse files
authored
Merge pull request #1874 from strongloop/mem-connector-fix
Fix value equality test to avoid toString
2 parents 1e9ac69 + 454fd0f commit 05898ed

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/connectors/memory.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ function applyFilter(filter) {
613613
return value.match(example);
614614
}
615615

616-
if (example === undefined) {
617-
return undefined;
616+
if (example == null) {
617+
return value == null;
618618
}
619619

620620
if (typeof example === 'object' && example !== null) {
@@ -682,9 +682,13 @@ function applyFilter(filter) {
682682
return true;
683683
}
684684
}
685+
686+
// compare date
687+
if (example instanceof Date && value instanceof Date) {
688+
return example.getTime() === value.getTime();
689+
}
685690
// not strict equality
686-
return (example !== null ? example.toString() : example) ==
687-
(value != null ? value.toString() : value);
691+
return example == value;
688692
}
689693

690694
/**

test/memory.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,15 @@ describe('Memory connector', function() {
339339
});
340340
});
341341

342+
it('should successfully extract 1 user (Lennon) from the db by date', function(done) {
343+
User.find({where: {birthday: new Date('1980-12-08')}},
344+
function(err, users) {
345+
should(users.length).be.equal(1);
346+
should(users[0].name).be.equal('John Lennon');
347+
done();
348+
});
349+
});
350+
342351
it('should successfully extract 2 users from the db', function(done) {
343352
User.find({where: {birthday: {between: [new Date(1940, 0), new Date(1990, 0)]}}},
344353
function(err, users) {
@@ -565,6 +574,33 @@ describe('Memory connector', function() {
565574
});
566575
});
567576

577+
it('should handle constructor.prototype', function(done) {
578+
User.find({where: {'constructor.prototype': {toString: 'Not a function'}}}, function(err,
579+
users) {
580+
should.not.exist(err);
581+
users.length.should.equal(0);
582+
done();
583+
});
584+
});
585+
586+
it('should handle constructor/prototype', function(done) {
587+
User.find({where: {constructor: {prototype: {toString: 'Not a function'}}}}, function(err,
588+
users) {
589+
should.not.exist(err);
590+
users.length.should.equal(0);
591+
done();
592+
});
593+
});
594+
595+
it('should handle toString', function(done) {
596+
User.find({where: {toString: 'Not a function'}}, function(err,
597+
users) {
598+
should.not.exist(err);
599+
users.length.should.equal(0);
600+
done();
601+
});
602+
});
603+
568604
function seed(done) {
569605
const beatles = [
570606
{

0 commit comments

Comments
 (0)