Skip to content
This repository was archived by the owner on Feb 5, 2020. It is now read-only.

Commit 0bc08f2

Browse files
committed
Implement CONTAINS operator.
1 parent f1b1444 commit 0bc08f2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/util/SparqlExpressionEvaluator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ operators = {
132132
'langmatches': function (a, b) {
133133
return a.toLowerCase() === b.toLowerCase();
134134
},
135+
'contains': function (string, substring) {
136+
substring = N3Util.getLiteralValue(substring);
137+
string = N3Util.getLiteralValue(string);
138+
return string.indexOf(substring) >= 0;
139+
},
135140
'regex': function (subject, pattern) {
136141
if (N3Util.isLiteral(subject))
137142
subject = N3Util.getLiteralValue(subject);
@@ -176,7 +181,7 @@ operators = {
176181

177182
// Tag all operators that have boolean results
178183
[
179-
'!', '&&', '||', '=', '<', '<=', '>', '>=', 'langmatches', 'regex',
184+
'!', '&&', '||', '=', '<', '<=', '>', '>=', 'langmatches', 'contains', 'regex',
180185
].forEach(function (operatorName) {
181186
operators[operatorName].resultType = 'boolean';
182187
});

test/util/SparqlExpressionEvaluator-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,29 @@ describe('SparqlExpressionEvaluator', function () {
355355
});
356356
});
357357

358+
describe('of the CONTAINS operator', function () {
359+
var evaluator = SparqlExpressionEvaluator({
360+
type: 'operation',
361+
operator: 'contains',
362+
args: [
363+
'"defgh"',
364+
'?a',
365+
],
366+
});
367+
368+
it('should return true if the substring is part of the string', function () {
369+
evaluator({ '?a': '"efg"' }).should.equal(TRUE);
370+
});
371+
372+
it('should return true if the substring is equal to the string', function () {
373+
evaluator({ '?a': '"defgh"^^<urn:type>' }).should.equal(TRUE);
374+
});
375+
376+
it('should return false if the substring is not part of the string', function () {
377+
evaluator({ '?a': '"abc"' }).should.equal(FALSE);
378+
});
379+
});
380+
358381
describe('of the regex operator', function () {
359382
var evaluator = SparqlExpressionEvaluator({
360383
type: 'operation',

0 commit comments

Comments
 (0)