diff --git a/README.md b/README.md index aecffdc..5e96e85 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/helpers/mu/index.js b/helpers/mu/index.js index 8b6258b..9905c36 100644 --- a/helpers/mu/index.js +++ b/helpers/mu/index.js @@ -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, @@ -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, @@ -38,6 +40,7 @@ export { SPARQL, query, update, + sparqlEscape, sparqlEscapeString, sparqlEscapeUri, sparqlEscapeInt, diff --git a/helpers/mu/server.js b/helpers/mu/server.js index d2362d9..6a77f3c 100644 --- a/helpers/mu/server.js +++ b/helpers/mu/server.js @@ -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; diff --git a/helpers/mu/sparql.js b/helpers/mu/sparql.js index 211cb74..f482a87 100644 --- a/helpers/mu/sparql.js +++ b/helpers/mu/sparql.js @@ -48,6 +48,28 @@ 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, @@ -55,6 +77,7 @@ const exports = { sparql: SPARQL, query: query, update: update, + sparqlEscape: sparqlEscape, sparqlEscapeString: sparqlEscapeString, sparqlEscapeUri: sparqlEscapeUri, sparqlEscapeInt: sparqlEscapeInt, @@ -71,6 +94,7 @@ export { SPARQL as sparql, query, update, + sparqlEscape, sparqlEscapeString, sparqlEscapeUri, sparqlEscapeInt,