Skip to content

Commit

Permalink
Improve addTermDefinition method to handle edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsw committed May 21, 2024
1 parent 2fd932b commit 740e30f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/rocrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,18 @@ class ROCrate {
}
}

/**
* Add the term and its definition to the first context definition (a map) found from the `@context` entries.
* If no existing context definition found, a new one will be created.
* @param {string} term
* @param {string|object} definition
*/
addTermDefinition(term, definition) {
var context = Array.from(this.__context).find(c => typeof c === 'object');
if (!context) {
context = {};
this.__context.add(context);
}
context[term] = definition;
const id = typeof definition === 'string' ? definition : definition["@id"] || term;
this.__contextTermIndex.set(term, definition);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ro-crate",
"version": "3.3.7",
"version": "3.3.8",
"description": "Research Object Crate (RO-Crate) utilities for making and consuming crates",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 9 additions & 1 deletion test/rocrate.new.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,15 @@ describe("addTermDefinition", function () {
assert.ok(!crate.getDefinition('Geometry'));
crate.addTermDefinition('Geometry', 'http://www.opengis.net/ont/geosparql#Geometry');
assert.ok(crate.getDefinition('Geometry'));

});
it("can add a new term to new context", async function () {
const crate = new ROCrate({'@context': 'http://example.com/test'}, {array: true});
//await crate.resolveContext();
assert.ok(!crate.getDefinition('Geometry'));
crate.addTermDefinition('Geometry', 'http://www.opengis.net/ont/geosparql#Geometry');
assert.equal(crate.getDefinition('Geometry')['@id'], 'http://www.opengis.net/ont/geosparql#Geometry');
assert.equal(crate.getTerm('http://www.opengis.net/ont/geosparql#Geometry'), 'Geometry');
assert.equal(crate.context.length, 2);
});
});
describe("getTerm", function () {
Expand Down

0 comments on commit 740e30f

Please sign in to comment.