-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsparql.js
72 lines (65 loc) · 1.62 KB
/
sparql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var fetch
if (typeof window !== 'undefined') {
fetch = window.fetch
} else {
fetch = require('isomorphic-fetch')
}
export default function (endpoint, query, options) {
var url = endpoint + '?query=' + encodeURIComponent(query)
var defaultOptions = {
method: 'GET',
headers: {
'Accept': 'application/sparql-results+json'
}
}
Object.assign(defaultOptions, options)
return fetch(url, defaultOptions)
.then(function (response) {
return response.json()
})
.then(parseResponse)
};
var xmlSchema = 'http://www.w3.org/2001/XMLSchema#'
function parseResponse (body) {
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 = String(v); break
case 'boolean':
v = Boolean(v === 'false' ? false : v); 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':
v = Number(v); break
case 'date':
case 'time':
case 'dateTime':
v = new Date(v); break
}
}
return v
}