Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utility function allowing to sparql-escape numbers as xsd:decimal #35

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The following importable variables are available:
- `sparql`: [Template tag](https://www.npmjs.com/package/sparql-client-2#using-the-sparql-template-tag) to create queries with interpolated values
- `sparqlEscapeString(value) => string`: Function to escape a string in SPARQL
- `sparqlEscapeUri(value) => string`: Function to escape a URI in SPARQL
- `sparqlEscapeDecimal(value) => string`: Function to escape an integer or float as an `xsd:decimal` in SPARQL
- `sparqlEscapeInt(value) => string`: Function to escape an integer in SPARQL
- `sparqlEscapeFloat(value) => string`: Function to escape a float in SPARQL
- `sparqlEscapeDate(value) => string`: Function to escape a date in SPARQL. The given value is passed to the `Date` constructor.
Expand Down
3 changes: 3 additions & 0 deletions helpers/mu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mu = {
sparqlEscape: sparql.sparqlEscape,
sparqlEscapeString: sparql.sparqlEscapeString,
sparqlEscapeUri: sparql.sparqlEscapeUri,
sparqlEscapeDecimal: sparql.sparqlEscapeDecimal,
sparqlEscapeInt: sparql.sparqlEscapeInt,
sparqlEscapeFloat: sparql.sparqlEscapeFloat,
sparqlEscapeDate: sparql.sparqlEscapeDate,
Expand All @@ -30,6 +31,7 @@ const SPARQL = mu.SPARQL,
sparqlEscapeString = mu.sparqlEscapeString,
sparqlEscapeUri = mu.sparqlEscapeUri,
sparqlEscapeInt = mu.sparqlEscapeInt,
sparqlEscapeDecimal = mu.sparqlEscapeDecimal,
sparqlEscapeFloat = mu.sparqlEscapeFloat,
sparqlEscapeDate = mu.sparqlEscapeDate,
sparqlEscapeDateTime = mu.sparqlEscapeDateTime,
Expand All @@ -44,6 +46,7 @@ export {
sparqlEscape,
sparqlEscapeString,
sparqlEscapeUri,
sparqlEscapeDecimal,
sparqlEscapeInt,
sparqlEscapeFloat,
sparqlEscapeDate,
Expand Down
7 changes: 7 additions & 0 deletions helpers/mu/sparql.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ function sparqlEscapeUri( value ){
return '<' + value.replace(/[\\"<>]/g, function(match) { return '\\' + match; }) + '>';
};

function sparqlEscapeDecimal( value ){
return '"' + Number.parseFloat(value) + '"^^xsd:decimal';
};

function sparqlEscapeInt( value ){
return '"' + Number.parseInt(value) + '"^^xsd:integer';
};
Expand Down Expand Up @@ -135,6 +139,8 @@ function sparqlEscape( value, type ){
return sparqlEscapeUri(value);
case 'bool':
return sparqlEscapeBool(value);
case 'decimal':
return sparqlEscapeDecimal(value);
case 'int':
return sparqlEscapeInt(value);
case 'float':
Expand Down Expand Up @@ -176,6 +182,7 @@ export {
sparqlEscape,
sparqlEscapeString,
sparqlEscapeUri,
sparqlEscapeDecimal,
sparqlEscapeInt,
sparqlEscapeFloat,
sparqlEscapeDate,
Expand Down