Skip to content

Commit

Permalink
Added general SPARQL escape helper with type paramater
Browse files Browse the repository at this point in the history
  • Loading branch information
erikap committed Jun 3, 2017
1 parent 3c13d34 commit 6e485bd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The following importable variables are available:
- `sparqlEscapeDate(value) => string`: Function to escape a date in SPARQL
- `sparqlEscapeDateTime(value) => string`: Function to escape a datetime in SPARQL
- `sparqlEscapeBool(value) => string`: Function to escape a boolean in SPARQL
- `sparqlEscape(value, type) => string`: Function to escape a value in SPARQL according to the given type. Type must be one of `'string'`, `'uri'`, `'int'`, `'float'`, `'date'`, `'dateTime'`, `'bool'`.

You can either import specific attributes from the mu library, or import the whole mu object.

Expand Down
3 changes: 3 additions & 0 deletions helpers/mu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mu = {
SPARQL: sparql.sparql,
query: sparql.query,
update: sparql.update,
sparqlEscape: sparql.sparqlEscape,
sparqlEscapeString: sparql.sparqlEscapeString,
sparqlEscapeUri: sparql.sparqlEscapeUri,
sparqlEscapeInt: sparql.sparqlEscapeInt,
Expand All @@ -24,6 +25,7 @@ const mu = {
const SPARQL = mu.SPARQL,
query = mu.query,
update = mu.update,
sparqlEscape = mu.sparqlEscape,
sparqlEscapeString = mu.sparqlEscapeString,
sparqlEscapeUri = mu.sparqlEscapeUri,
sparqlEscapeInt = mu.sparqlEscapeInt,
Expand All @@ -38,6 +40,7 @@ export {
SPARQL,
query,
update,
sparqlEscape,
sparqlEscapeString,
sparqlEscapeUri,
sparqlEscapeInt,
Expand Down
2 changes: 1 addition & 1 deletion helpers/mu/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.use(function(err, req, res, next) {

// start server
app.listen( 80, function() {
console.log('Starting server on port 80');
console.log(`Starting server on port 80 in ${app.get('env')} mode`);
});

export default app;
24 changes: 24 additions & 0 deletions helpers/mu/sparql.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,36 @@ function sparqlEscapeBool( value ){
return value ? '"true"^^xsd:boolean' : '"false"^^xsd:boolean';
};

function sparqlEscape( value, type ){
switch(type) {
case 'string':
return sparqlEscapeString(value);
case 'uri':
return sparqlEscapeUri(value);
case 'bool':
return sparqlEscapeBool(value);
case 'int':
return sparqlEscapeInt(value);
case 'float':
return sparqlEscapeFloat(value);
case 'date':
return sparqlEscapeDate(value);
case 'dateTime':
return sparqlEscapeDateTime(value);
default:
console.error(`WARN: Unknown escape type '${type}'. Escaping as string`);
return sparqlEscapeString(value);
}
}

//==-- exports --==//
const exports = {
newSparqlClient: newSparqlClient,
SPARQL: SPARQL,
sparql: SPARQL,
query: query,
update: update,
sparqlEscape: sparqlEscape,
sparqlEscapeString: sparqlEscapeString,
sparqlEscapeUri: sparqlEscapeUri,
sparqlEscapeInt: sparqlEscapeInt,
Expand All @@ -71,6 +94,7 @@ export {
SPARQL as sparql,
query,
update,
sparqlEscape,
sparqlEscapeString,
sparqlEscapeUri,
sparqlEscapeInt,
Expand Down

0 comments on commit 6e485bd

Please sign in to comment.