-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rdflib-utils: add addInstanceToTypeIndex
- Loading branch information
Showing
6 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
utils/rdflib/src/update-operations/addInstanceToTypeIndex.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { addInstanceToTypeIndex } from "./addInstanceToTypeIndex"; | ||
import { st, sym } from "rdflib"; | ||
import { rdf, solid } from "../namespaces"; | ||
import { when } from "jest-when"; | ||
import { generateId } from "../identifier/index.js"; | ||
|
||
jest.mock("../identifier/index.js"); | ||
|
||
describe("addInstanceToTypeIndex", () => { | ||
it("creates a new type registration for type", () => { | ||
when(generateId).mockReturnValue("74807edb"); | ||
const operation = addInstanceToTypeIndex( | ||
sym("https://alice.test/settings/privateTypeIndex.ttl"), | ||
"https://alice.test/contacts/1#this", | ||
sym("http://www.w3.org/2006/vcard/ns#AddressBook"), | ||
); | ||
expect(operation.insertions).toContainEqual( | ||
st( | ||
sym("https://alice.test/settings/privateTypeIndex.ttl#74807edb"), | ||
rdf("type"), | ||
solid("TypeRegistration"), | ||
sym("https://alice.test/settings/privateTypeIndex.ttl"), | ||
), | ||
); | ||
expect(operation.insertions).toContainEqual( | ||
st( | ||
sym("https://alice.test/settings/privateTypeIndex.ttl#74807edb"), | ||
solid("forClass"), | ||
sym("http://www.w3.org/2006/vcard/ns#AddressBook"), | ||
sym("https://alice.test/settings/privateTypeIndex.ttl"), | ||
), | ||
); | ||
}); | ||
|
||
it("links the instance to the registration", () => { | ||
when(generateId).mockReturnValue("74807edb"); | ||
const operation = addInstanceToTypeIndex( | ||
sym("https://alice.test/settings/privateTypeIndex.ttl"), | ||
"https://alice.test/contacts/1#this", | ||
sym("http://www.w3.org/2006/vcard/ns#AddressBook"), | ||
); | ||
expect(operation.insertions).toContainEqual( | ||
st( | ||
sym("https://alice.test/settings/privateTypeIndex.ttl#74807edb"), | ||
solid("instance"), | ||
sym("https://alice.test/contacts/1#this"), | ||
sym("https://alice.test/settings/privateTypeIndex.ttl"), | ||
), | ||
); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
utils/rdflib/src/update-operations/addInstanceToTypeIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NamedNode, st, sym } from "rdflib"; | ||
import { UpdateOperation } from "../web-operations/index.js"; | ||
import { generateId } from "../identifier/index.js"; | ||
import { rdf, solid } from "../namespaces/index.js"; | ||
|
||
/** | ||
* Create a new type registration for the given type that links to the given resource instance | ||
* @param typeIndexDoc - The type index document (private or public) | ||
* @param instanceUri - The URI of the instance to add | ||
* @param type - The RDF class to register the instance for | ||
*/ | ||
export function addInstanceToTypeIndex( | ||
typeIndexDoc: NamedNode, | ||
instanceUri: string, | ||
type: NamedNode, | ||
): UpdateOperation { | ||
const registrationNode = sym(`${typeIndexDoc.value}#${generateId()}`); | ||
return { | ||
deletions: [], | ||
filesToCreate: [], | ||
insertions: [ | ||
st( | ||
registrationNode, | ||
rdf("type"), | ||
solid("TypeRegistration"), | ||
typeIndexDoc, | ||
), | ||
st(registrationNode, solid("forClass"), type, typeIndexDoc), | ||
st(registrationNode, solid("instance"), sym(instanceUri), typeIndexDoc), | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { addInstanceToTypeIndex } from "./addInstanceToTypeIndex.js"; |