-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (102 loc) · 3.48 KB
/
index.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
const datasetFactory = require('./lib/dataset')
const SimpleArray = require('./lib/array')
const SimpleContext = require('./lib/context')
const SimpleCore = require('./lib/core')
const SimpleHandler = require('./lib/define-property-handler')
class SimpleRDF {
toString () {
return this._core.graph.toString()
}
context (context) {
if (context) {
this._core.context = SimpleContext.create(context)
this._core.context.descriptions().forEach((description) => {
// access values with full IRI
this._handler.addProperty(description)
// access values with short property
this._handler.addProperty(description)
})
}
return this._core.context
}
iri (iri) {
if (iri) {
iri = SimpleCore.buildNode(iri)
if (!iri.equals(this._core.iri)) {
this._core.updateSubject(iri)
this._core.updateObject(iri)
this._core.iri = iri
}
}
return this._core.iri
}
graph (graph) {
if (graph) {
this._core.graph = graph
this._core.graph.match(this._core.iri).forEach(quad => {
const description = SimpleContext.parseJsonProperty(quad.predicate.value, quad.predicate.value)
if (!this._handler.hasProperty(description.property)) {
this._handler.addProperty(description)
}
})
}
return this._core.graph
}
child (iri) {
return this.create(this._core.context, iri, this._core.graph, this._options)
}
static init (context, iri, graph, options) {
this._core = new SimpleCore(this)
this._handler = new SimpleHandler(this, this._core)
this._core.iri = SimpleCore.buildNode(iri)
this.context(context || {})
this.graph(datasetFactory(graph, this._core))
this._options = options || {}
this._plugins.forEach((plugin) => {
if (typeof plugin.prototype.init === 'function') {
plugin.prototype.init.call(this, context, iri, graph, this._options)
}
})
}
static isArray (obj) {
return SimpleArray.isArray(obj)
}
static extend (plugin) {
let NewClass = function (context, iri, graph, options) {
if (this instanceof NewClass) {
SimpleRDF.init.call(this, context, iri, graph, options)
} else {
return NewClass.prototype.create(context, iri, graph, options)
}
}
// assign prototype from base class
NewClass.prototype = Object.create(this.prototype)
NewClass.prototype.init = SimpleRDF.init
NewClass.prototype.create = function (context, iri, graph, options) {
return new NewClass(context, iri, graph, options)
}
// assign prototype from plugin class
Object.getOwnPropertyNames(plugin.prototype).forEach((key) => {
NewClass.prototype[key] = plugin.prototype[key]
})
// assign static properties from base class
Object.getOwnPropertyNames(this).forEach((key) => {
// ignore Object properties (length, name, prototype, ...)
if (!(key in Object)) {
NewClass[key] = this[key]
}
})
// assign static properties from plugin class
Object.getOwnPropertyNames(plugin).forEach((key) => {
// ignore Object properties (length, name, prototype, ...)
if (!(key in Object)) {
NewClass[key] = plugin[key]
}
})
// clone the plugins array and add the new plugin
NewClass.prototype._plugins = (this.prototype._plugins || []).slice(0).concat([plugin])
return NewClass
}
}
SimpleRDF.prototype._plugins = []
module.exports = SimpleRDF.extend.call(Object, SimpleRDF)