Skip to content

Commit

Permalink
fix: serialize from string with not so nice turtle files
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Apr 6, 2024
1 parent 12954a3 commit 973955e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/rdf/RDFSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ export class RDFSerializer extends DataSerializer {
});
quads = parser.parse(input);
}
const subjectUri = new URL(subject);
subjectUri.hash = '';
const containerUri = new URL(subjectUri.href.slice(0, subjectUri.href.lastIndexOf('/') + 1));
quads = quads.map((quad) => {
let subject = quad.subject;
let predicate = quad.predicate;
let object = quad.object;
if (subject.termType === 'NamedNode' && subject.value.startsWith('#')) {
subject = DataFactory.namedNode(new URL(subject.value, subjectUri).href);
} else if (subject.termType === 'NamedNode' && !subject.value.startsWith('http')) {
subject = DataFactory.namedNode(new URL(subject.value, containerUri).href);
}
if (predicate.termType === 'NamedNode' && predicate.value.startsWith('#')) {
predicate = DataFactory.namedNode(new URL(predicate.value, subjectUri).href);
} else if (subject.termType === 'NamedNode' && !subject.value.startsWith('http')) {
predicate = DataFactory.namedNode(new URL(predicate.value, containerUri).href);
}
if (object.termType === 'NamedNode' && object.value.startsWith('#')) {
object = DataFactory.namedNode(new URL(object.value, subjectUri).href);
} else if (object.termType === 'NamedNode' && !object.value.startsWith('http')) {
object = DataFactory.namedNode(new URL(object.value, containerUri).href);
}
return DataFactory.quad(subject, predicate, object);
});
const store = new Store(quads);
return this.deserializeFromStore(
subject ? DataFactory.namedNode(subject) : DataFactory.blankNode(quads[0].subject.value),
Expand Down
26 changes: 25 additions & 1 deletion test/specs/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { expect } from "chai";
import { IriString, RDFSerializer, Subject, User } from "../../src";

const otherProfile: string = `
@prefix : <#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
@prefix pro: <./>.
@prefix sol: </>.
pro:card a foaf:PersonalProfileDocument; foaf:maker :me; foaf:primaryTopic :me.
:me
a foaf:Person;
vcard:fn "Jan Hofmann";
vcard:hasPhoto <QR%20jh%20solid.jpg>;
vcard:hasURL "https://www.scs.fraunhofer.de/en/vision/data-spaces-iot.html";
vcard:organization-name
"Center for Applied Research on Supply Chain Services at Fraunhofer IIS";
vcard:role "Data Spaces & IoT Solutions";
solid:oidcIssuer sol: .
`;
const profile: string = `
@prefix : <https://solid.maximvdw.be/profile/card#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
Expand Down Expand Up @@ -72,9 +92,13 @@ describe('User', () => {
it('should deserialize a user profile from string', () => {
const user: User = RDFSerializer.deserializeFromString("https://solid.maximvdw.be/profile/card#me", profile);
expect(user).to.not.be.undefined;
console.log(user);
expect(user.name).to.equal("Maxim Van de Wynckel");
expect(user.picture).to.equal("https://solid.maximvdw.be/profile/1568226501835_Maxim_square.jpg");

const user2: User = RDFSerializer.deserializeFromString("https://solid.dyn.hofmannsnet.de/jan/profile/card#me", otherProfile);
expect(user2).to.not.be.undefined;
expect(user2.name).to.equal("Jan Hofmann");
expect(user2.picture).to.equal("https://solid.dyn.hofmannsnet.de/jan/profile/QR%20jh%20solid.jpg");
});

it('should deserialize a user profile from subject', () => {
Expand Down

0 comments on commit 973955e

Please sign in to comment.