Skip to content

Commit

Permalink
add replace function
Browse files Browse the repository at this point in the history
  • Loading branch information
Callidon committed Feb 14, 2020
1 parent 590f1c0 commit 5b2f62b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/operators/expressions/sparql-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,22 @@ export default {
return rdf.createBoolean(test)
},

'regex': function (subject: Term, pattern: Term, flags: Term) {
let regexp = (flags === null || flags === undefined) ? new RegExp(pattern.value) : new RegExp(pattern.value, flags.value)
'regex': function (subject: Term, pattern: Term, flags?: Term) {
const regexp = (flags === undefined) ? new RegExp(pattern.value) : new RegExp(pattern.value, flags.value)
return rdf.createBoolean(regexp.test(subject.value))
},

'replace': function (arg: Term, pattern: Term, replacement: Term, flags?: Term) {
const regexp = (flags === undefined) ? new RegExp(pattern.value) : new RegExp(pattern.value, flags.value)
const newValue = arg.value.replace(regexp, replacement.value)
if (rdf.termIsIRI(arg)) {
return rdf.createIRI(newValue)
} else if (rdf.termIsBNode(arg)) {
return rdf.createBNode(newValue)
}
return rdf.shallowCloneTerm(arg, newValue)
},

/*
Functions on Numerics https://www.w3.org/TR/sparql11-query/#func-numerics
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/sparql/filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,39 @@ describe('FILTER SPARQL queries', () => {
}`,
expectedNb: 1
},
{
name: 'replace',
query: `
PREFIX dblp-rdf: <https://dblp.uni-trier.de/rdf/schema-2017-04-18#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT * WHERE {
?s rdf:type dblp-rdf:Person .
FILTER(replace("abcd", "b", "Z") = "aZcd")
}`,
expectedNb: 1
},
{
name: 'replace (with flags)',
query: `
PREFIX dblp-rdf: <https://dblp.uni-trier.de/rdf/schema-2017-04-18#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT * WHERE {
?s rdf:type dblp-rdf:Person .
FILTER(replace("abab", "B", "Z", "i") = "aZab")
}`,
expectedNb: 1
},
{
name: 'replace (with complex REGEX)',
query: `
PREFIX dblp-rdf: <https://dblp.uni-trier.de/rdf/schema-2017-04-18#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT * WHERE {
?s rdf:type dblp-rdf:Person .
FILTER(replace("abab", "B.", "Z","i") = "aZb")
}`,
expectedNb: 1
},
{
name: 'abs',
query: `
Expand Down

0 comments on commit 5b2f62b

Please sign in to comment.