Skip to content

Commit c851e3a

Browse files
committed
Export RDF/JS Term and Quad constructors.
1 parent dedd8fc commit c851e3a

18 files changed

+212
-195
lines changed

src/N3DataFactory.js

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44
import namespaces from './IRIs';
55
const { rdf, xsd } = namespaces;
66

7-
var DataFactory, DEFAULTGRAPH;
7+
let DEFAULTGRAPH;
8+
let _blankNodeCounter = 0;
89

9-
var _blankNodeCounter = 0;
10+
// ## DataFactory singleton
11+
const DataFactory = {
12+
namedNode,
13+
blankNode,
14+
variable,
15+
literal,
16+
defaultGraph,
17+
quad,
18+
triple: quad,
19+
};
20+
export default DataFactory;
1021

1122
// ## Term constructor
12-
class Term {
23+
export class Term {
1324
constructor(id) {
1425
this.id = id;
1526
}
@@ -41,15 +52,15 @@ class Term {
4152

4253

4354
// ## NamedNode constructor
44-
class NamedNode extends Term {
55+
export class NamedNode extends Term {
4556
// ### The term type of this term
4657
get termType() {
4758
return 'NamedNode';
4859
}
4960
}
5061

5162
// ## Literal constructor
52-
class Literal extends Term {
63+
export class Literal extends Term {
5364
// ### The term type of this term
5465
get termType() {
5566
return 'Literal';
@@ -108,7 +119,7 @@ class Literal extends Term {
108119
}
109120

110121
// ## BlankNode constructor
111-
class BlankNode extends Term {
122+
export class BlankNode extends Term {
112123
constructor(name) {
113124
super('_:' + name);
114125
}
@@ -124,7 +135,7 @@ class BlankNode extends Term {
124135
}
125136
}
126137

127-
class Variable extends Term {
138+
export class Variable extends Term {
128139
constructor(name) {
129140
super('?' + name);
130141
}
@@ -141,7 +152,7 @@ class Variable extends Term {
141152
}
142153

143154
// ## DefaultGraph constructor
144-
class DefaultGraph extends Term {
155+
export class DefaultGraph extends Term {
145156
constructor() {
146157
super('');
147158
return DEFAULTGRAPH || this;
@@ -166,7 +177,7 @@ DEFAULTGRAPH = new DefaultGraph();
166177

167178

168179
// ### Constructs a term from the given internal string ID
169-
function fromId(id, factory) {
180+
export function termFromId(id, factory) {
170181
factory = factory || DataFactory;
171182

172183
// Falsy value or empty string indicate the default graph
@@ -194,7 +205,7 @@ function fromId(id, factory) {
194205
}
195206

196207
// ### Constructs an internal string ID from the given term or ID string
197-
function toId(term) {
208+
export function termToId(term) {
198209
if (typeof term === 'string')
199210
return term;
200211
if (term instanceof Term)
@@ -217,7 +228,7 @@ function toId(term) {
217228

218229

219230
// ## Quad constructor
220-
class Quad {
231+
export class Quad {
221232
constructor(subject, predicate, object, graph) {
222233
this.subject = subject;
223234
this.predicate = predicate;
@@ -243,35 +254,9 @@ class Quad {
243254
this.graph.equals(other.graph);
244255
}
245256
}
257+
export { Quad as Triple };
246258

247259

248-
// ## DataFactory singleton
249-
DataFactory = {
250-
// ### Public factory functions
251-
namedNode,
252-
blankNode,
253-
variable,
254-
literal,
255-
defaultGraph,
256-
quad,
257-
triple: quad,
258-
259-
// ### Internal datatype constructors
260-
internal: {
261-
Term,
262-
NamedNode,
263-
BlankNode,
264-
Variable,
265-
Literal,
266-
DefaultGraph,
267-
Quad,
268-
Triple: Quad,
269-
fromId,
270-
toId,
271-
},
272-
};
273-
export default DataFactory;
274-
275260
// ### Creates an IRI
276261
function namedNode(iri) {
277262
return new NamedNode(iri);

src/N3Store.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// **N3Store** objects store N3 quads by graph in memory.
2-
import N3DataFactory from './N3DataFactory';
2+
import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory';
33
import { Readable } from 'stream';
44
import namespaces from './IRIs';
55

6-
const { toId, fromId } = N3DataFactory.internal;
7-
86
// ## Constructor
97
export default class N3Store {
108
constructor(quads, options) {
@@ -111,11 +109,11 @@ export default class N3Store {
111109
// Create quads for all items found in index 2.
112110
for (var l = 0; l < values.length; l++) {
113111
var parts = { subject: null, predicate: null, object: null };
114-
parts[name0] = fromId(entity0, this._factory);
115-
parts[name1] = fromId(entity1, this._factory);
116-
parts[name2] = fromId(entityKeys[values[l]], this._factory);
112+
parts[name0] = termFromId(entity0, this._factory);
113+
parts[name1] = termFromId(entity1, this._factory);
114+
parts[name2] = termFromId(entityKeys[values[l]], this._factory);
117115
var quad = this._factory.quad(
118-
parts.subject, parts.predicate, parts.object, fromId(graph, this._factory));
116+
parts.subject, parts.predicate, parts.object, termFromId(graph, this._factory));
119117
if (array)
120118
array.push(quad);
121119
else if (callback(quad))
@@ -204,7 +202,7 @@ export default class N3Store {
204202
return function (id) {
205203
if (!(id in uniqueIds)) {
206204
uniqueIds[id] = true;
207-
callback(fromId(entities[id]));
205+
callback(termFromId(entities[id]));
208206
}
209207
};
210208
}
@@ -220,10 +218,10 @@ export default class N3Store {
220218
predicate = subject.predicate, subject = subject.subject;
221219

222220
// Convert terms to internal string representation
223-
subject = toId(subject);
224-
predicate = toId(predicate);
225-
object = toId(object);
226-
graph = toId(graph);
221+
subject = termToId(subject);
222+
predicate = termToId(predicate);
223+
object = termToId(object);
224+
graph = termToId(graph);
227225

228226
// Find the graph that will contain the triple
229227
var graphItem = this._graphs[graph];
@@ -274,10 +272,10 @@ export default class N3Store {
274272
predicate = subject.predicate, subject = subject.subject;
275273

276274
// Convert terms to internal string representation
277-
subject = toId(subject);
278-
predicate = toId(predicate);
279-
object = toId(object);
280-
graph = toId(graph);
275+
subject = termToId(subject);
276+
predicate = termToId(predicate);
277+
object = termToId(object);
278+
graph = termToId(graph);
281279

282280
// Find internal identifiers for all components
283281
// and verify the quad exists.
@@ -329,10 +327,10 @@ export default class N3Store {
329327
// Setting any field to `undefined` or `null` indicates a wildcard.
330328
getQuads(subject, predicate, object, graph) {
331329
// Convert terms to internal string representation
332-
subject = subject && toId(subject);
333-
predicate = predicate && toId(predicate);
334-
object = object && toId(object);
335-
graph = graph && toId(graph);
330+
subject = subject && termToId(subject);
331+
predicate = predicate && termToId(predicate);
332+
object = object && termToId(object);
333+
graph = graph && termToId(graph);
336334

337335
var quads = [], graphs = this._getGraphs(graph), content,
338336
ids = this._ids, subjectId, predicateId, objectId;
@@ -393,10 +391,10 @@ export default class N3Store {
393391
// Setting any field to `undefined` or `null` indicates a wildcard.
394392
countQuads(subject, predicate, object, graph) {
395393
// Convert terms to internal string representation
396-
subject = subject && toId(subject);
397-
predicate = predicate && toId(predicate);
398-
object = object && toId(object);
399-
graph = graph && toId(graph);
394+
subject = subject && termToId(subject);
395+
predicate = predicate && termToId(predicate);
396+
object = object && termToId(object);
397+
graph = graph && termToId(graph);
400398

401399
var count = 0, graphs = this._getGraphs(graph), content,
402400
ids = this._ids, subjectId, predicateId, objectId;
@@ -458,10 +456,10 @@ export default class N3Store {
458456
// Setting any field to `undefined` or `null` indicates a wildcard.
459457
some(callback, subject, predicate, object, graph) {
460458
// Convert terms to internal string representation
461-
subject = subject && toId(subject);
462-
predicate = predicate && toId(predicate);
463-
object = object && toId(object);
464-
graph = graph && toId(graph);
459+
subject = subject && termToId(subject);
460+
predicate = predicate && termToId(predicate);
461+
object = object && termToId(object);
462+
graph = graph && termToId(graph);
465463

466464
var graphs = this._getGraphs(graph), content,
467465
ids = this._ids, subjectId, predicateId, objectId;
@@ -526,9 +524,9 @@ export default class N3Store {
526524
// Setting any field to `undefined` or `null` indicates a wildcard.
527525
forSubjects(callback, predicate, object, graph) {
528526
// Convert terms to internal string representation
529-
predicate = predicate && toId(predicate);
530-
object = object && toId(object);
531-
graph = graph && toId(graph);
527+
predicate = predicate && termToId(predicate);
528+
object = object && termToId(object);
529+
graph = graph && termToId(graph);
532530

533531
var ids = this._ids, graphs = this._getGraphs(graph), content, predicateId, objectId;
534532
callback = this._uniqueEntities(callback);
@@ -572,9 +570,9 @@ export default class N3Store {
572570
// Setting any field to `undefined` or `null` indicates a wildcard.
573571
forPredicates(callback, subject, object, graph) {
574572
// Convert terms to internal string representation
575-
subject = subject && toId(subject);
576-
object = object && toId(object);
577-
graph = graph && toId(graph);
573+
subject = subject && termToId(subject);
574+
object = object && termToId(object);
575+
graph = graph && termToId(graph);
578576

579577
var ids = this._ids, graphs = this._getGraphs(graph), content, subjectId, objectId;
580578
callback = this._uniqueEntities(callback);
@@ -618,9 +616,9 @@ export default class N3Store {
618616
// Setting any field to `undefined` or `null` indicates a wildcard.
619617
forObjects(callback, subject, predicate, graph) {
620618
// Convert terms to internal string representation
621-
subject = subject && toId(subject);
622-
predicate = predicate && toId(predicate);
623-
graph = graph && toId(graph);
619+
subject = subject && termToId(subject);
620+
predicate = predicate && termToId(predicate);
621+
graph = graph && termToId(graph);
624622

625623
var ids = this._ids, graphs = this._getGraphs(graph), content, subjectId, predicateId;
626624
callback = this._uniqueEntities(callback);

src/N3Writer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// **N3Writer** writes N3 documents.
22
import namespaces from './IRIs';
3-
import N3DataFactory from './N3DataFactory';
3+
import { default as N3DataFactory, Term } from './N3DataFactory';
44

55
const DEFAULTGRAPH = N3DataFactory.defaultGraph();
66

@@ -15,7 +15,7 @@ var escape = /["\\\t\n\r\b\f\u0000-\u0019\ud800-\udbff]/,
1515
};
1616

1717
// ## Placeholder class to represent already pretty-printed terms
18-
class SerializedTerm extends N3DataFactory.internal.Term {
18+
class SerializedTerm extends Term {
1919
// Pretty-printed nodes are not equal to any other node
2020
// (e.g., [] does not equal [])
2121
equals() {

src/index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import DataFactory from './N3DataFactory';
21
import Lexer from './N3Lexer';
32
import Parser from './N3Parser';
43
import Writer from './N3Writer';
@@ -7,13 +6,42 @@ import StreamParser from './N3StreamParser';
76
import StreamWriter from './N3StreamWriter';
87
import * as Util from './N3Util';
98

9+
import {
10+
default as DataFactory,
11+
12+
Term,
13+
NamedNode,
14+
Literal,
15+
BlankNode,
16+
Variable,
17+
DefaultGraph,
18+
Quad,
19+
Triple,
20+
21+
termFromId,
22+
termToId,
23+
} from './N3DataFactory';
24+
1025
export {
11-
DataFactory,
1226
Lexer,
1327
Parser,
1428
Writer,
1529
Store,
1630
StreamParser,
1731
StreamWriter,
1832
Util,
33+
34+
DataFactory,
35+
36+
Term,
37+
NamedNode,
38+
Literal,
39+
BlankNode,
40+
Variable,
41+
DefaultGraph,
42+
Quad,
43+
Triple,
44+
45+
termFromId,
46+
termToId,
1947
};

test/BlankNode-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { DataFactory } from '../src/';
2-
3-
const { BlankNode, Term } = DataFactory.internal;
1+
import { BlankNode, Term } from '../src/';
42

53
describe('BlankNode', function () {
64
describe('The BlankNode module', function () {

test/DefaultGraph-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { DataFactory } from '../src/';
2-
3-
const { DefaultGraph, Term } = DataFactory.internal;
1+
import { DefaultGraph, Term } from '../src/';
42

53
describe('DefaultGraph', function () {
64
describe('The DefaultGraph module', function () {

test/Literal-test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { DataFactory } from '../src/';
2-
3-
const { Literal, NamedNode, Term } = DataFactory.internal;
1+
import { Literal, NamedNode, Term } from '../src/';
42

53
describe('Literal', function () {
64
describe('The Literal module', function () {

test/N3DataFactory-test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { DataFactory } from '../src/';
2-
3-
const { NamedNode, Literal, BlankNode, Variable, DefaultGraph, Quad } = DataFactory.internal;
1+
import {
2+
DataFactory,
3+
NamedNode,
4+
Literal,
5+
BlankNode,
6+
Variable,
7+
DefaultGraph,
8+
Quad,
9+
} from '../src/';
410

511
describe('DataFactory', function () {
612
describe('namedNode', function () {

0 commit comments

Comments
 (0)