From 8db475cadbf20110583c0008a02ddd851a6140b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Dierick?= Date: Tue, 23 Nov 2021 13:33:33 +0100 Subject: [PATCH 1/2] add utility function allowing to sparql-escape numbers as xsd:decimal --- README.md | 1 + helpers/mu/index.js | 3 +++ helpers/mu/sparql.js | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 9900bdc..a4c4557 100644 --- a/README.md +++ b/README.md @@ -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 a JS integer 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. diff --git a/helpers/mu/index.js b/helpers/mu/index.js index 3f84979..54b0c0d 100644 --- a/helpers/mu/index.js +++ b/helpers/mu/index.js @@ -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, @@ -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, @@ -44,6 +46,7 @@ export { sparqlEscape, sparqlEscapeString, sparqlEscapeUri, + sparqlEscapeDecimal, sparqlEscapeInt, sparqlEscapeFloat, sparqlEscapeDate, diff --git a/helpers/mu/sparql.js b/helpers/mu/sparql.js index bc649b4..98ea9a9 100644 --- a/helpers/mu/sparql.js +++ b/helpers/mu/sparql.js @@ -107,6 +107,10 @@ function sparqlEscapeUri( value ){ return '<' + value.replace(/[\\"']/g, function(match) { return '\\' + match; }) + '>'; }; +function sparqlEscapeDecimal( value ){ + return '"' + Number.parseInt(value) + '"^^xsd:decimal'; +}; + function sparqlEscapeInt( value ){ return '"' + Number.parseInt(value) + '"^^xsd:integer'; }; @@ -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': @@ -176,6 +182,7 @@ export { sparqlEscape, sparqlEscapeString, sparqlEscapeUri, + sparqlEscapeDecimal, sparqlEscapeInt, sparqlEscapeFloat, sparqlEscapeDate, From 95c67185257fe6174e3cc676f7fb97c2ea2f3140 Mon Sep 17 00:00:00 2001 From: Aad Versteden Date: Mon, 4 Apr 2022 10:44:58 +0200 Subject: [PATCH 2/2] Decimal may be either integer or float Number.parseFloat parses both integers as well as floats. Review with @erikap --- README.md | 2 +- helpers/mu/sparql.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a4c4557..d39f3b4 100644 --- a/README.md +++ b/README.md @@ -60,7 +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 a JS integer as an `xsd:decimal` 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. diff --git a/helpers/mu/sparql.js b/helpers/mu/sparql.js index 98ea9a9..2381057 100644 --- a/helpers/mu/sparql.js +++ b/helpers/mu/sparql.js @@ -108,7 +108,7 @@ function sparqlEscapeUri( value ){ }; function sparqlEscapeDecimal( value ){ - return '"' + Number.parseInt(value) + '"^^xsd:decimal'; + return '"' + Number.parseFloat(value) + '"^^xsd:decimal'; }; function sparqlEscapeInt( value ){