Skip to content

Commit d1e7af7

Browse files
author
Lance Welsh
committed
Introduce starts_with query filter
1 parent bcef924 commit d1e7af7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/query.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module.exports = (function() {
1212
'ilike': function(a, b) { return a.toLowerCase().indexOf(b.toLowerCase()) > -1; },
1313
'like': function(a, b) { return a.indexOf(b) > -1; },
1414
'in': function(a, b) { return b.indexOf(a) > -1; },
15-
'not_in': function(a, b) { return b.indexOf(a) === -1; }
15+
'not_in': function(a, b) { return b.indexOf(a) === -1; },
16+
'starts_with': function(a, b) { const re = new RegExp('^' + b, ''); return !!a.match(re); },
17+
'istarts_with': function(a, b) { const re = new RegExp('^' + b, 'i'); return !!a.match(re); }
1618
};
1719

1820
class Query {

test/runner.js

+22
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,28 @@ describe('Test Suite', function() {
394394

395395
});
396396

397+
it('should filter starts_with', function() {
398+
399+
expect(nc.query().filter({name__starts_with: 'Keith'}).units().length).to.equal(1);
400+
expect(nc.query().filter({name__starts_with: 'Keith'}).first()).to.equal(nc.find(1));
401+
402+
expect(nc.query().filter({name__starts_with: 'K'}).units().length).to.equal(2);
403+
expect(nc.query().filter({name__starts_with: 'K'}).first()).to.equal(nc.find(1));
404+
expect(nc.query().filter({name__starts_with: 'K'}).units()[1]).to.equal(nc.find(4));
405+
406+
});
407+
408+
it('should filter istarts_with', function() {
409+
410+
expect(nc.query().filter({name__istarts_with: 'keith'}).units().length).to.equal(1);
411+
expect(nc.query().filter({name__istarts_with: 'keith'}).first()).to.equal(nc.find(1));
412+
413+
expect(nc.query().filter({name__istarts_with: 'k'}).units().length).to.equal(2);
414+
expect(nc.query().filter({name__istarts_with: 'k'}).first()).to.equal(nc.find(1));
415+
expect(nc.query().filter({name__istarts_with: 'k'}).units()[1]).to.equal(nc.find(4));
416+
417+
});
418+
397419
it('should exclude as expected', function() {
398420

399421
expect(nc.query().exclude({name__is: 'Keith'}).first()).to.equal(nc.find(2));

0 commit comments

Comments
 (0)