From e7e074f97b0b79b7d5de44af7138dba99576d85e Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Wed, 12 Aug 2015 17:59:55 -0700 Subject: [PATCH] Do not coerce RegExp objects to strings Queries like `{where: {name: /John.*/i}}` should work. Notice there is no `regexp` operator (ie. `{where: {name: {regexp: /John.*/i}}}`). --- lib/dao.js | 7 +++++-- test/memory.test.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index d69ae93e..dc5ee894 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -1196,8 +1196,11 @@ DataAccessObject._coerce = function (where) { } } else { if (val != null) { - if (operator === 'regexp' && val instanceof RegExp) { - // do not coerce regex literals/objects + if (operator === null && val instanceof RegExp) { + // Normalize {name: /A/} to {name: {regexp: /A/}} + operator = 'regexp'; + } else if (operator === 'regexp' && val instanceof RegExp) { + // Do not coerce regex literals/objects } else if (!((operator === 'like' || operator === 'nlike') && val instanceof RegExp)) { val = DataType(val); } diff --git a/test/memory.test.js b/test/memory.test.js index ca226e13..9280513a 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -356,6 +356,16 @@ describe('Memory connector', function() { }); }); + it('should work when a regex is provided without the regexp operator', + function(done) { + User.find({where: {name: /John.*/i}}, function(err, users) { + should.not.exist(err); + users.length.should.equal(1); + users[0].name.should.equal('John Lennon'); + done(); + }); + }); + it('should support the regexp operator with regex strings', function(done) { User.find({where: {name: {regexp: '^J'}}}, function(err, users) { should.not.exist(err);