n3 1.20.4 → 1.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -10
- package/browser/n3.min.js +1 -1
- package/lib/N3Lexer.js +3 -3
- package/lib/N3Parser.js +31 -7
- package/lib/N3Store.js +43 -1
- package/lib/N3StreamParser.js +14 -9
- package/package.json +3 -3
- package/src/N3Lexer.js +3 -3
- package/src/N3Parser.js +38 -7
- package/src/N3Store.js +40 -2
- package/src/N3StreamParser.js +12 -6
package/README.md
CHANGED
|
@@ -83,12 +83,15 @@ we assume that a quad is simply a triple in a named or default graph.
|
|
|
83
83
|
|
|
84
84
|
`N3.Parser` transforms Turtle, TriG, N-Triples, or N-Quads document into quads through a callback:
|
|
85
85
|
```JavaScript
|
|
86
|
+
const tomAndJerry = `PREFIX c: <http://example.org/cartoons#>
|
|
87
|
+
# Tom is a cat
|
|
88
|
+
c:Tom a c:Cat.
|
|
89
|
+
c:Jerry a c:Mouse;
|
|
90
|
+
c:smarterThan c:Tom.`
|
|
91
|
+
|
|
86
92
|
const parser = new N3.Parser();
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
c:Tom a c:Cat.
|
|
90
|
-
c:Jerry a c:Mouse;
|
|
91
|
-
c:smarterThan c:Tom.`,
|
|
93
|
+
|
|
94
|
+
parser.parse(tomAndJerry,
|
|
92
95
|
(error, quad, prefixes) => {
|
|
93
96
|
if (quad)
|
|
94
97
|
console.log(quad);
|
|
@@ -101,9 +104,29 @@ If there are no more quads,
|
|
|
101
104
|
the callback is invoked one last time with `null` for `quad`
|
|
102
105
|
and a hash of prefixes as third argument.
|
|
103
106
|
<br>
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
|
|
108
|
+
Alternatively, an object can be supplied, where `onQuad`, `onPrefix` and `onComment` are used to listen for `quads`, `prefixes` and `comments` as follows:
|
|
109
|
+
```JavaScript
|
|
110
|
+
const parser = new N3.Parser();
|
|
111
|
+
|
|
112
|
+
parser.parse(tomAndJerry, {
|
|
113
|
+
// onQuad (required) accepts a listener of type (quad: RDF.Quad) => void
|
|
114
|
+
onQuad: (err, quad) => { console.log(quad); },
|
|
115
|
+
// onPrefix (optional) accepts a listener of type (prefix: string, iri: NamedNode) => void
|
|
116
|
+
onPrefix: (prefix, iri) => { console.log(prefix, 'expands to', iri.value); },
|
|
117
|
+
// onComment (optional) accepts a listener of type (comment: string) => void
|
|
118
|
+
onComment: (comment) => { console.log('#', comment); },
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
If no callbacks are provided, parsing happens synchronously returning an array of quads:
|
|
123
|
+
|
|
124
|
+
```JavaScript
|
|
125
|
+
const parser = new N3.Parser();
|
|
126
|
+
|
|
127
|
+
// An array of resultant Quads
|
|
128
|
+
const quadArray = parser.parse(tomAndJerry);
|
|
129
|
+
```
|
|
107
130
|
|
|
108
131
|
By default, `N3.Parser` parses a permissive superset of Turtle, TriG, N-Triples, and N-Quads.
|
|
109
132
|
<br>
|
|
@@ -168,17 +191,18 @@ function SlowConsumer() {
|
|
|
168
191
|
```
|
|
169
192
|
|
|
170
193
|
A dedicated `prefix` event signals every prefix with `prefix` and `term` arguments.
|
|
194
|
+
A dedicated `comment` event can be enabled by setting `comments: true` in the N3.StreamParser constructor.
|
|
171
195
|
|
|
172
196
|
## Writing
|
|
173
197
|
|
|
174
198
|
### From quads to a string
|
|
175
199
|
|
|
176
200
|
`N3.Writer` serializes quads as an RDF document.
|
|
177
|
-
Write quads through `
|
|
201
|
+
Write quads through `addQuad`.
|
|
178
202
|
|
|
179
203
|
```JavaScript
|
|
180
204
|
const writer = new N3.Writer({ prefixes: { c: 'http://example.org/cartoons#' } }); // Create a writer which uses `c` as a prefix for the namespace `http://example.org/cartoons#`
|
|
181
|
-
writer.
|
|
205
|
+
writer.addQuad(quad(
|
|
182
206
|
namedNode('http://example.org/cartoons#Tom'), // Subject
|
|
183
207
|
namedNode('http://example.org/cartoons#name'), // Predicate
|
|
184
208
|
literal('Tom') // Object
|