forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
40 lines (39 loc) · 1.3 KB
/
node.js
File metadata and controls
40 lines (39 loc) · 1.3 KB
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
// This file attaches all functionality to Node
// that would otherwise require circular dependencies.
import { fromValue } from './collection';
import Node from './node-internal';
import Namespace from './namespace';
import { isCollection, isLiteral } from './utils/terms';
/**
* Creates an RDF Node from a native javascript value.
* RDF Nodes are returned unchanged, undefined returned as itself.
* @method fromValue
* @static
* @param value {Node|Date|String|Number|Boolean|Undefined}
* @return {Node|Collection}
*/
Node.fromValue = fromValue;
export default Node;
const ns = {
xsd: Namespace('http://www.w3.org/2001/XMLSchema#')
};
/**
* Gets the javascript object equivalent to a node
* @param term The RDF node
*/
Node.toJS = function (term) {
if (isCollection(term)) {
return term.elements.map(Node.toJS); // Array node (not standard RDFJS)
}
if (!isLiteral(term)) return term;
if (term.datatype.equals(ns.xsd('boolean'))) {
return term.value === '1' || term.value === 'true';
}
if (term.datatype.equals(ns.xsd('dateTime')) || term.datatype.equals(ns.xsd('date'))) {
return new Date(term.value);
}
if (term.datatype.equals(ns.xsd('integer')) || term.datatype.equals(ns.xsd('float')) || term.datatype.equals(ns.xsd('decimal'))) {
return Number(term.value);
}
return term.value;
};