Skip to content

Commit 0d30fce

Browse files
committed
Added a default base option.
1 parent b8f2dd9 commit 0d30fce

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ var jsonld = require("jsonld")
33
, uuid = require("uuid")
44
, IRI = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i;
55

6-
function levelgraphJSONLD(db) {
6+
function levelgraphJSONLD(db, jsonldOpts) {
77

88
if (db.jsonld) {
99
return db;
1010
}
1111

1212
var graphdb = Object.create(db);
1313

14-
graphdb.jsonld = {};
14+
jsonldOpts = jsonldOpts || {};
15+
jsonldOpts.base = jsonldOpts.base || "";
16+
17+
graphdb.jsonld = {
18+
options: jsonldOpts
19+
};
1520

1621
graphdb.jsonld.put = function(obj, options, callback) {
1722
if (typeof obj === 'string') {
@@ -23,7 +28,7 @@ function levelgraphJSONLD(db) {
2328
options = {};
2429
}
2530

26-
options.base = options.base || "";
31+
options.base = options.base || this.options.base;
2732

2833
if (!obj["@id"]) {
2934
obj["@id"] = options.base + uuid.v1();
@@ -43,7 +48,7 @@ function levelgraphJSONLD(db) {
4348
delete triples["@id"];
4449

4550
if (!subject) {
46-
subject = options.base + uuid.v1();
51+
subject = (options.base || graphdb.jsond.options.base) + uuid.v1();
4752
}
4853

4954
Object.keys(triples).forEach(function(predicate) {

test/put_spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,42 @@ describe("jsonld.put", function() {
105105
});
106106
});
107107
});
108+
109+
describe("jsonld.put with default base", function() {
110+
111+
var db, manu;
112+
113+
beforeEach(function() {
114+
db = jsonld(graph(level()), { base: "http://levelgraph.io/ahah/" });
115+
manu = manu = {
116+
"@context": {
117+
"name": "http://xmlns.com/foaf/0.1/name"
118+
, "homepage": {
119+
"@id": "http://xmlns.com/foaf/0.1/homepage"
120+
, "@type": "@id"
121+
}
122+
}
123+
, "@id": "http://manu.sporny.org#person"
124+
, "name": "Manu Sporny"
125+
, "homepage": "http://manu.sporny.org/"
126+
};
127+
});
128+
129+
afterEach(function(done) {
130+
db.close(done);
131+
});
132+
133+
it("should use it", function(done) {
134+
manu["@id"] = "42"
135+
db.jsonld.put(manu, function() {
136+
db.get({
137+
subject: "http://levelgraph.io/ahah/42"
138+
, predicate: "http://xmlns.com/foaf/0.1/name"
139+
, object: "Manu Sporny"
140+
}, function(err, triples) {
141+
expect(triples).to.have.property("length", 1);
142+
done();
143+
});
144+
});
145+
});
146+
});

0 commit comments

Comments
 (0)