-
Notifications
You must be signed in to change notification settings - Fork 5
/
NodeUtils.js
150 lines (125 loc) · 3.91 KB
/
NodeUtils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var _ = require('lodash');
var UriUtils = require('../util/UriUtils');
var TypedValue = require('./rdf_datatype/TypedValue');
var NodeUtils = {
matches: function(pattern, candidate) {
var result = candidate && (pattern == null || candidate.equals(pattern));
//console.log('NodeUtils.matches: ' + pattern + ' with ' + candidate + ' > ' + result);
return result;
},
uniq: function(nodes) {
var result = _.uniq(nodes, false, function(node) {
return '' + node;
});
return result;
},
getSubstitute: function(node, fnNodeMap) {
var result = fnNodeMap(node);
if (!result) {
result = node;
}
return result;
},
/**
* Push the node into to the array if it is a variable
*/
pushVar: function(array, node) {
if (node.isVariable()) {
var c = false;
array.forEach(function(item) {
c = c || node.equals(item);
});
if (!c) {
array.push(node);
}
}
return array;
},
getLang: function(node) {
var result = node && node.isLiteral() ? node.getLiteralLanguage() : null;
return result;
},
getUri: function(node) {
var result = node && node.isUri() ? node.getUri() : null;
return result;
},
/**
* Obtain a "pretty" string from a node object, under an optional prefixMapping.
* Pretty means, that Uris will be converted to their short form (if a prefix mapping applies) or
* their local name (otherwise).
*
* @param node
* @param prefixMapping
* @returns {String}
*/
toPrettyString: function(node, prefixMapping) {
var result;
if(node == null) {
result = null;
} else if(node.isUri()) {
var uri = node.getUri();
if(prefixMapping) {
result = prefixMapping.shortForm(uri);
if(result === uri) {
result = UriUtils.extractLabel(uri);
}
} else {
result = UriUtils.extractLabel(uri);
}
} else {
result = '' + this.getValue(node);
}
return result;
},
// Turn a node of any kind into a javascript literal value
getValue: function(node) {
var result;
if(node == null) {
result = null;
} else if(node.isUri()) {
result = node.getUri();
} else if(node.isBlank()) {
result = node.toString();
} else if(node.isLiteral()) {
var tmp = node.getLiteralValue();
result = tmp instanceof TypedValue
? tmp.getLexicalValue()
: tmp
;
} else {
throw new Error('Unknow node type: ', node);
}
//console.log('Returned value: ' + result + '; for node: ' + node);
return result;
},
toTalisRdfJson: function(node) {
var result;
if(node == null) {
result = null; // TODO: Maybe returning an empty object (possibly with type = unknown) would be better?
} else if(node.isUri()) {
result = {
type: 'uri',
value: node.getUri(),
};
} else if(node.isBlank()) {
result = {
type: 'bnode',
value: node.toString()
};
} else if(node.isLiteral()) {
result = {
type: 'literal',
value: node.getLiteralLexicalForm(),
datatype: node.getLiteralDatatypeUri(),
lang: node.getLiteralLanguage()
};
} else {
result = {
type: 'unknown'
};
}
//console.log('Returned value: ' + result + '; for node: ' + node);
return result;
}
};
module.exports = NodeUtils;