n3 1.20.4 → 1.21.0
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 +33 -7
- package/browser/n3.min.js +1 -1
- package/lib/N3Lexer.js +3 -3
- package/lib/N3Parser.js +31 -7
- package/lib/N3Store.js +6 -1
- package/lib/N3StreamParser.js +14 -9
- package/package.json +1 -1
- package/src/N3Lexer.js +3 -3
- package/src/N3Parser.js +38 -7
- package/src/N3Store.js +1 -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,30 @@ 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
|
-
|
|
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
|
+
|
|
105
122
|
<br>
|
|
106
|
-
If no callbacks are provided, parsing happens synchronously.
|
|
123
|
+
If no callbacks are provided, parsing happens synchronously returning an array of quads.
|
|
124
|
+
|
|
125
|
+
```JavaScript
|
|
126
|
+
const parser = new N3.Parser();
|
|
127
|
+
|
|
128
|
+
// An array of resultant Quads
|
|
129
|
+
const quadArray = parser.parse(tomAndJerry);
|
|
130
|
+
```
|
|
107
131
|
|
|
108
132
|
By default, `N3.Parser` parses a permissive superset of Turtle, TriG, N-Triples, and N-Quads.
|
|
109
133
|
<br>
|
|
@@ -169,6 +193,8 @@ function SlowConsumer() {
|
|
|
169
193
|
|
|
170
194
|
A dedicated `prefix` event signals every prefix with `prefix` and `term` arguments.
|
|
171
195
|
|
|
196
|
+
A dedicated `comment` event can be enabled by setting `comments: true` in the N3.StreamParser constructor.
|
|
197
|
+
|
|
172
198
|
## Writing
|
|
173
199
|
|
|
174
200
|
### From quads to a string
|