Skip to content

Commit 9b37a6d

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

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

lib/query.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ 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) { return a.substr(0, b.length) === b; }
1617
};
1718

1819
class Query {

test/runner.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ 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+
397408
it('should exclude as expected', function() {
398409

399410
expect(nc.query().exclude({name__is: 'Keith'}).first()).to.equal(nc.find(2));
@@ -418,11 +429,14 @@ describe('Test Suite', function() {
418429
graph.createNode('person', {id: 4, name: 'Kelly'});
419430
graph.createNode('person', {id: 5, name: 'Trevor'});
420431
graph.createNode('person', {id: 6, name: 'Arthur'});
432+
graph.createNode('person', {id: 7, name: 'Lance'});
421433

422434
graph.createNode('food', {id: 1, name: 'Pizza'});
423435
graph.createNode('food', {id: 2, name: 'Kale'});
424436
graph.createNode('food', {id: 3, name: 'Ice Cream'});
425437
graph.createNode('food', {id: 4, name: 'Meatballs'});
438+
graph.createNode('food', {id: 5, name: 'Cilantro Salad'});
439+
graph.createNode('food', {id: 6, name: 'Chocolate Bacon'});
426440

427441
graph.createEdge('likes').link(graph.nodes('person').find(1), graph.nodes('food').find(1));
428442
graph.createEdge('likes').link(graph.nodes('person').find(1), graph.nodes('food').find(4));
@@ -442,6 +456,15 @@ describe('Test Suite', function() {
442456
// graph.createEdge('likes').link(graph.nodes('person').find(6), graph.nodes('food').find(3));
443457
graph.createEdge('likes').link(graph.nodes('person').find(6), graph.nodes('food').find(4));
444458

459+
graph.createEdge('dislikes').link(graph.nodes('person').find(7), graph.nodes('food').find(5));
460+
graph.createEdge('dislikes').link(graph.nodes('person').find(7), graph.nodes('food').find(6));
461+
462+
graph.createEdge('dislikes').link(graph.nodes('person').find(6), graph.nodes('food').find(5));
463+
graph.createEdge('dislikes').link(graph.nodes('person').find(6), graph.nodes('food').find(6));
464+
465+
graph.createEdge('dislikes').link(graph.nodes('person').find(5), graph.nodes('food').find(5));
466+
graph.createEdge('dislikes').link(graph.nodes('person').find(5), graph.nodes('food').find(6));
467+
445468
describe('Graph#trace', function() {
446469

447470
let nodes = [
@@ -480,6 +503,88 @@ describe('Test Suite', function() {
480503

481504
});
482505

506+
describe('Graph#traceObjOpts', function() {
507+
508+
let nodes = [
509+
graph.nodes('person').find(1),
510+
graph.nodes('food').find(4),
511+
graph.nodes('person').find(5),
512+
graph.nodes('food').find(2),
513+
graph.nodes('person').find(3)
514+
];
515+
516+
const opts = {}
517+
518+
let path = graph.trace(graph.nodes('person').find(1), graph.nodes('person').find(3), opts);
519+
520+
it('should have the correct starting point', function() {
521+
522+
expect(path.start()).to.equal(nodes[0]);
523+
524+
});
525+
526+
it('should have the correct ending point', function() {
527+
528+
expect(path.end()).to.equal(nodes[4]);
529+
530+
});
531+
532+
it('should have the correct length', function() {
533+
534+
expect(path.length()).to.equal(4);
535+
536+
});
537+
538+
it('should have the correct distance', function() {
539+
540+
expect(path.distance()).to.equal(4);
541+
542+
});
543+
544+
});
545+
546+
describe('Graph#traceObjOptsDir', function() {
547+
548+
let nodes = [
549+
graph.nodes('person').find(1),
550+
graph.nodes('food').find(4),
551+
graph.nodes('person').find(5),
552+
graph.nodes('food').find(2),
553+
graph.nodes('person').find(3)
554+
];
555+
556+
const opts = {
557+
direction: 1
558+
}
559+
560+
let path = graph.trace(graph.nodes('person').find(1), graph.nodes('person').find(3), opts);
561+
562+
it('should have the correct starting point', function() {
563+
564+
expect(path.length()).to.be.equal(0);
565+
566+
});
567+
568+
it('should have the correct ending point', function() {
569+
570+
expect(path.length()).to.be.equal(0);
571+
572+
});
573+
574+
it('should have the correct length', function() {
575+
576+
expect(path.length()).to.be.equal(0);
577+
578+
});
579+
580+
it('should have the correct distance', function() {
581+
582+
expect(path.length()).to.be.equal(0);
583+
584+
});
585+
586+
});
587+
483588
describe('Graph#closest', function() {
484589

485590
let paths = graph.closest(graph.nodes('person').find(2), {
@@ -533,7 +638,55 @@ describe('Test Suite', function() {
533638

534639
expect(cnodes.indexOf(paths[1].end())).to.be.above(-1);
535640
expect(cnodes.indexOf(paths[2].end())).to.be.above(-1);
641+
});
642+
643+
it('should obey the compareNode function (all nodes in path must match)', function() {
536644

645+
let paths = graph.closest(graph.nodes('person').find(2), {
646+
compareNode: function(node) { return node.entity === 'person'; },
647+
count: 0,
648+
direction: 0,
649+
minDepth: 0,
650+
maxDepth: 0
651+
});
652+
653+
expect(paths.length).to.equal(1);
654+
655+
expect(paths[0].length()).to.equal(0);
656+
expect(paths[0].end()).to.equal(graph.nodes('person').find(2));
657+
expect(paths[0].start()).to.equal(graph.nodes('person').find(2));
658+
659+
});
660+
661+
it('should obey the compareEdge function (all edges in path must match)', function() {
662+
663+
let paths = graph.closest(graph.nodes('person').find(7), {
664+
compareEdge: function(edge) { return edge.entity === 'dislikes'; },
665+
count: 0,
666+
direction: 0,
667+
minDepth: 0,
668+
maxDepth: 0
669+
});
670+
671+
expect(paths.length).to.equal(5);
672+
673+
expect(paths[0].length()).to.equal(0); // person7 to self
674+
675+
expect(paths[1].length()).to.equal(1); // person7 dislikes food5
676+
expect(paths[1].start()).to.equal(graph.nodes('person').find(7));
677+
expect(paths[1].end()).to.equal(graph.nodes('food').find(5));
678+
679+
expect(paths[2].length()).to.equal(1); // person7 dislikes food6
680+
expect(paths[2].start()).to.equal(graph.nodes('person').find(7));
681+
expect(paths[2].end()).to.equal(graph.nodes('food').find(6));
682+
683+
expect(paths[3].length()).to.equal(2); // person7 dislikes the same food as person6
684+
expect(paths[3].start()).to.equal(graph.nodes('person').find(7));
685+
expect(paths[3].end()).to.equal(graph.nodes('person').find(6));
686+
687+
expect(paths[4].length()).to.equal(2); // person7 dislikes the same food as person5
688+
expect(paths[4].start()).to.equal(graph.nodes('person').find(7));
689+
expect(paths[4].end()).to.equal(graph.nodes('person').find(5));
537690
});
538691

539692
it('should obey the count attribute', function() {
@@ -595,6 +748,23 @@ describe('Test Suite', function() {
595748

596749
});
597750

751+
it('should obey the maxDepth attribute by length', function() {
752+
753+
let paths = graph.closest(graph.nodes('person').find(2), {
754+
compare: function() { return true; },
755+
count: 0,
756+
direction: 0,
757+
minDepth: 0,
758+
maxDepth: 1,
759+
byLength: true
760+
});
761+
762+
expect(paths.length).to.equal(2);
763+
expect(paths[0].end()).to.equal(graph.nodes('person').find(2));
764+
expect(paths[1].end()).to.equal(graph.nodes('food').find(1));
765+
766+
});
767+
598768
});
599769

600770
});

0 commit comments

Comments
 (0)