Comparing version 1.0.0 to 1.1.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-request')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'd3-request'], factory) : | ||
(factory((global.d3 = global.d3 || {}),global.d3)); | ||
}(this, function (exports,d3Request) { 'use strict'; | ||
(global = global || self, factory(global.d3 = {}, global.d3)); | ||
}(this, function (exports, d3Request) { 'use strict'; | ||
const xmlSchema = "http://www.w3.org/2001/XMLSchema#"; | ||
function sparql (endpoint, query, callback) { | ||
var url = endpoint + '?query=' + encodeURIComponent(query) | ||
var url = endpoint + '?query=' + encodeURIComponent(query); | ||
var response = function (xhr) { | ||
try { | ||
var body = JSON.parse(xhr.responseText) | ||
} catch (e) { | ||
throw new Error("unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: " + xhr.responseText) | ||
} | ||
return body.results.bindings.map(function(row) { | ||
var rowObject = {} | ||
Object.keys(row).forEach(function (column) { | ||
rowObject[column] = dataTypeCasting(row[column]) | ||
}) | ||
return rowObject | ||
}) | ||
} | ||
var sparql = d3Request.request(url) | ||
.mimeType('application/sparql-results+json') | ||
.response(parseResponse); | ||
var sparql = d3Request.request(url).mimeType('application/sparql-results+json').response(response) | ||
if (callback) { | ||
if (typeof callback !== 'function') { | ||
throw new Error('invalid callback: ' + callback) | ||
} | ||
if (callback != null) { | ||
if (typeof callback !== "function") throw new Error("invalid callback: " + callback); | ||
return sparql.get(callback) | ||
sparql.get(callback); | ||
return | ||
} | ||
return sparql | ||
} | ||
var xmlSchema = 'http://www.w3.org/2001/XMLSchema#'; | ||
}; | ||
function parseResponse (xhr) { | ||
try { | ||
var body = JSON.parse(xhr.responseText); | ||
} catch (e) { | ||
throw new Error('unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: ' + xhr.responseText) | ||
} | ||
return body.results.bindings.map(function (row) { | ||
var rowObject = {}; | ||
Object.keys(row).forEach(function (column) { | ||
rowObject[column] = dataTypeToJS(row[column]); | ||
}); | ||
return rowObject | ||
}) | ||
} | ||
function dataTypeCasting(value) { | ||
function dataTypeToJS (value) { | ||
var v = value.value; | ||
if (typeof value.datatype !== "undefined") { | ||
var dt = value.datatype.replace(xmlSchema,'') | ||
switch(dt) { | ||
if (typeof value.datatype === 'string') { | ||
var dt = value.datatype.replace(xmlSchema, ''); | ||
switch (dt) { | ||
case 'string': | ||
v = new String(v); break; | ||
v = String(v); break | ||
case 'boolean': | ||
v = new Boolean(v == "false" ? false : v); break; | ||
v = Boolean(v === 'false' ? false : v); break | ||
case 'float': | ||
@@ -64,10 +67,10 @@ case 'integer': | ||
case 'unsignedByte': | ||
v = new Number(v); break; | ||
v = Number(v); break | ||
case 'date': | ||
case 'time': | ||
case 'dateTime': | ||
v = new Date(v); break; | ||
v = new Date(v); break | ||
} | ||
} | ||
return v; | ||
return v | ||
} | ||
@@ -79,2 +82,2 @@ | ||
})); | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("d3-request")):"function"==typeof define&&define.amd?define(["exports","d3-request"],n):n(e.d3=e.d3||{},e.d3)}(this,function(e,n){"use strict";function t(e,t,r){var a=e+"?query="+encodeURIComponent(t),o=function(e){try{var n=JSON.parse(e.responseText)}catch(n){throw new Error("unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: "+e.responseText)}return n.results.bindings.map(function(e){var n={};return Object.keys(e).forEach(function(t){n[t]=s(e[t])}),n})},i=n.request(a).mimeType("application/sparql-results+json").response(o);if(null!=r){if("function"!=typeof r)throw new Error("invalid callback: "+r);return i.get(r)}return i}function s(e){var n=e.value;if(void 0!==e.datatype){switch(e.datatype.replace(r,"")){case"string":n=new String(n);break;case"boolean":n=new Boolean("false"!=n&&n);break;case"float":case"integer":case"long":case"double":case"decimal":case"nonPositiveInteger":case"nonNegativeInteger":case"negativeInteger":case"int":case"unsignedLong":case"positiveInteger":case"short":case"unsignedInt":case"byte":case"unsignedShort":case"unsignedByte":n=new Number(n);break;case"date":case"time":case"dateTime":n=new Date(n)}}return n}const r="http://www.w3.org/2001/XMLSchema#";e.sparql=t,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-request")):"function"==typeof define&&define.amd?define(["exports","d3-request"],t):t((e=e||self).d3={},e.d3)}(this,function(e,a){"use strict";var s="http://www.w3.org/2001/XMLSchema#";function o(t){try{var e=JSON.parse(t.responseText)}catch(e){throw new Error("unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: "+t.responseText)}return e.results.bindings.map(function(t){var n={};return Object.keys(t).forEach(function(e){n[e]=function(e){var t=e.value;if("string"==typeof e.datatype){var n=e.datatype.replace(s,"");switch(n){case"string":t=String(t);break;case"boolean":t=Boolean("false"!==t&&t);break;case"float":case"integer":case"long":case"double":case"decimal":case"nonPositiveInteger":case"nonNegativeInteger":case"negativeInteger":case"int":case"unsignedLong":case"positiveInteger":case"short":case"unsignedInt":case"byte":case"unsignedShort":case"unsignedByte":t=Number(t);break;case"date":case"time":case"dateTime":t=new Date(t)}}return t}(t[e])}),n})}e.sparql=function(e,t,n){var s=e+"?query="+encodeURIComponent(t),r=a.request(s).mimeType("application/sparql-results+json").response(o);if(!n)return r;if("function"!=typeof n)throw new Error("invalid callback: "+n);r.get(n)},Object.defineProperty(e,"__esModule",{value:!0})}); |
@@ -1,1 +0,1 @@ | ||
export {default as sparql} from "./src/sparql"; | ||
export { default as sparql } from './src/sparql' |
{ | ||
"name": "d3-sparql", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Queries a SPARQL endpoint with a SELECT query and provides the data d3 “usable”", | ||
@@ -18,15 +18,19 @@ "keywords": [ | ||
"scripts": { | ||
"pretest": "rm -rf build && mkdir build && rollup -g d3-request:d3 -f umd -n d3 -o build/d3-sparql.js -- index.js", | ||
"build": "rimraf build && mkdirp build && rollup -g d3-request:d3 -f umd -n d3 -o build/d3-sparql.js -- index.js && uglifyjs build/d3-sparql.js -c -m -o build/d3-sparql.min.js", | ||
"lint": "standard", | ||
"pretest": "npm run lint && npm run build", | ||
"test": "tape 'test/**/*-test.js'", | ||
"prepublish": "npm run test && uglifyjs build/d3-sparql.js -c -m -o build/d3-sparql.min.js", | ||
"postpublish": "zip -j build/d3-sparql.zip -- LICENSE README.md build/d3-sparql.js build/d3-sparql.min.js" | ||
"prepublish": "npm run test" | ||
}, | ||
"devDependencies": { | ||
"rollup": "0.27", | ||
"tape": "4", | ||
"uglify-js": "2" | ||
"mkdirp": "^0.5.1", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.10.0", | ||
"standard": "^12.0.1", | ||
"tape": "^4.10.1", | ||
"uglify-js": "^3.5.4" | ||
}, | ||
"dependencies": { | ||
"d3-request": "^1.0.5" | ||
"d3-request": "^1.0.6" | ||
} | ||
} |
@@ -13,3 +13,4 @@ # d3-sparql | ||
FILTER(LANG(?developerName) = 'en') | ||
} | ||
}` | ||
wikidataUrl = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql' | ||
@@ -33,3 +34,3 @@ ``` | ||
- Transformation of [XSD Datatypes](https://www.w3.org/2011/rdf-wg/wiki/XSD_Datatypes) (e.g. `xsd:dateTime`, `xsd:boolean`, ...) to native JavaScript types. | ||
- Reformating of the JSON Structure to a d3 style layout while using the provided variables names of the SPARQL Query. | ||
- Reformatting of the JSON Structure to a d3 style layout while using the provided variables names of the SPARQL Query. | ||
- A backport to a d3 v3 implementation is [provided](https://github.com/zazuko/d3-sparql/tree/v3). | ||
@@ -63,2 +64,2 @@ | ||
## Acknowledgement | ||
The initial development of this library by [Zazuko](http://www.zazuko.com) was sponsered by the [City of Zürich](https://www.stadt-zuerich.ch/). | ||
The initial development of this library by [Zazuko](http://www.zazuko.com) was supported by the [City of Zürich](https://www.stadt-zuerich.ch/). |
@@ -1,44 +0,48 @@ | ||
import {request} from 'd3-request' | ||
import { request } from 'd3-request' | ||
const xmlSchema = "http://www.w3.org/2001/XMLSchema#"; | ||
export default function (endpoint, query, callback) { | ||
var url = endpoint + '?query=' + encodeURIComponent(query) | ||
var response = function (xhr) { | ||
try { | ||
var body = JSON.parse(xhr.responseText) | ||
} catch (e) { | ||
throw new Error("unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: " + xhr.responseText) | ||
} | ||
return body.results.bindings.map(function(row) { | ||
var rowObject = {} | ||
Object.keys(row).forEach(function (column) { | ||
rowObject[column] = dataTypeCasting(row[column]) | ||
}) | ||
return rowObject | ||
}) | ||
} | ||
var sparql = request(url) | ||
.mimeType('application/sparql-results+json') | ||
.response(parseResponse) | ||
var sparql = request(url).mimeType('application/sparql-results+json').response(response) | ||
if (callback) { | ||
if (typeof callback !== 'function') { | ||
throw new Error('invalid callback: ' + callback) | ||
} | ||
if (callback != null) { | ||
if (typeof callback !== "function") throw new Error("invalid callback: " + callback); | ||
return sparql.get(callback) | ||
sparql.get(callback) | ||
return | ||
} | ||
return sparql | ||
}; | ||
export function dataTypeCasting(value) { | ||
var v = value.value; | ||
if (typeof value.datatype !== "undefined") { | ||
var dt = value.datatype.replace(xmlSchema,'') | ||
switch(dt) { | ||
var xmlSchema = 'http://www.w3.org/2001/XMLSchema#' | ||
function parseResponse (xhr) { | ||
try { | ||
var body = JSON.parse(xhr.responseText) | ||
} catch (e) { | ||
throw new Error('unable to parse response, either the Endpoint URL is wrong or the Endpoint does not answer with sparql-results+json: ' + xhr.responseText) | ||
} | ||
return body.results.bindings.map(function (row) { | ||
var rowObject = {} | ||
Object.keys(row).forEach(function (column) { | ||
rowObject[column] = dataTypeToJS(row[column]) | ||
}) | ||
return rowObject | ||
}) | ||
} | ||
function dataTypeToJS (value) { | ||
var v = value.value | ||
if (typeof value.datatype === 'string') { | ||
var dt = value.datatype.replace(xmlSchema, '') | ||
switch (dt) { | ||
case 'string': | ||
v = new String(v); break; | ||
v = String(v); break | ||
case 'boolean': | ||
v = new Boolean(v == "false" ? false : v); break; | ||
v = Boolean(v === 'false' ? false : v); break | ||
case 'float': | ||
@@ -60,10 +64,10 @@ case 'integer': | ||
case 'unsignedByte': | ||
v = new Number(v); break; | ||
v = Number(v); break | ||
case 'date': | ||
case 'time': | ||
case 'dateTime': | ||
v = new Date(v); break; | ||
v = new Date(v); break | ||
} | ||
} | ||
return v; | ||
return v | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
16531
12
136
63
6
1
Updatedd3-request@^1.0.6