Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/solid/Email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TermWrapper, ValueMappings, TermMappings } from 'rdfjs-wrapper';
import { VCARD, RDF } from '../vocabulary/mod.js';

export class Email extends TermWrapper {
get emailAddress(): string {
return this.singular(VCARD.value, ValueMappings.literalToString);
}

set emailAddress(value: string) {
this.overwrite(VCARD.value, value, TermMappings.stringToLiteral);
}

get emailType(): string | undefined {
return this.singularNullable(RDF.type, ValueMappings.iriToString);
}

set emailType(value: string | undefined) {
this.overwriteNullable(RDF.type, value, TermMappings.stringToIri);
}
}
15 changes: 15 additions & 0 deletions src/solid/EmailDataset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DatasetWrapper } from 'rdfjs-wrapper';
import { VCARD } from '../vocabulary/mod.js';
import { Email } from './Email.js';

export class EmailDataset extends DatasetWrapper {
get emails(): Iterable<Email> {
const objects = new Set([
...this.instancesOf(VCARD.Email, Email),
...this.objectsOf(VCARD.hasEmail, Email),
...this.objectsOf(VCARD.email, Email),
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything about how this vocabulary is used in the Solid community it's history there or best practices that migh be in use, but I see a potential problem with this shape in the context of the vCard Ontology.

vcard:email is a datatype property while vcard:hasEmail is an object property.
vcard:email will have a literal object or a blank node with a vcard:value property, itself a literal.
vcard:hasEmail will have a resource object or a blank node with a vcard:hasValue property, itself a resouce.

In this code, both of these are handled using the Email mapping class. Thst's not a problem, but then that class should be smarter.

In any case, I don't think we are doing justice to the versatility of vCard. In my understanding, all of the following are valid shapes:

[ :hasEmail <mailto:user@example.com> ] .
[ :hasEmail [ :hasValue <mailto:user@example.com> ] ] .
[ :email "mailto:user@example.com" ] .
[ :email [ :value "mailto:user@example.com" ] ] .

It is entirely possible to map any usage scenario to JavaScript classes using a combination of mapping properties and other logic. Here I'm just highlighting what seem like discrepancies. Happy to help with proposed programming constructs once there is clarity on what the shape we're mapping is.

If I'm calling out things that are irrelevant or have already been clarified then sorry.

])

return objects
}
}
20 changes: 20 additions & 0 deletions src/solid/Telephone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TermWrapper, ValueMappings, TermMappings } from 'rdfjs-wrapper';
import { VCARD } from '../vocabulary/mod.js';

export class Telephone extends TermWrapper {
get phoneNumber(): string {
return this.singular(VCARD.hasValue, ValueMappings.literalToString) || '';
}
Comment on lines +5 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I would advise against this pattern of using singular on one hand and a null coalesce for default value on the other hand. singular is a utility method that expects (requires) one and only one statement with the given subject and predicate. IOW, there is exactly one statement matching ?s vcard:hasValue ?o or else I throw (source).
  2. I would aim to keep 'mapping properties' separate from other logic, i.e. a property should consist either of invocations of the utility methods or of other logic (like assigning defaults). The reason is that mapping properties could/would be generated.
  3. I have not thought about whether a default value for this property is reasonable at all. I have afeeling that not having defaults is somehow better, simpler. But again, this is not a very informed opinion.


set phoneNumber(value: string) {
this.overwrite(VCARD.hasValue, value, TermMappings.stringToLiteral);
}

get phoneType(): string | undefined {
return this.singularNullable(VCARD.telephoneType, ValueMappings.iriToString);
}

set phoneType(value: string | undefined) {
this.overwriteNullable(VCARD.telephoneType, value, TermMappings.stringToIri);
}
}
14 changes: 14 additions & 0 deletions src/solid/TelephoneDataset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DatasetWrapper } from 'rdfjs-wrapper';
import { VCARD } from '../vocabulary/vcard.js';
import { Telephone } from './Telephone.js';

export class TelephoneDataset extends DatasetWrapper {
get telephones(): Iterable<Telephone> {
const objects = new Set([
...this.objectsOf(VCARD.hasTelephone, Telephone),
...this.objectsOf(VCARD.tel, Telephone),
])

return objects
}
}
4 changes: 4 additions & 0 deletions src/solid/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export * from "./Container.js"
export * from "./ContainerDataset.js"
export * from "./Resource.js"
export * from "./Email.js";
export * from "./EmailDataset.js";
export * from "./Telephone.js";
export * from "./TelephoneDataset.js";
25 changes: 15 additions & 10 deletions src/vocabulary/vcard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

export const VCARD = {
fn: "http://www.w3.org/2006/vcard/ns#fn",
hasEmail: "http://www.w3.org/2006/vcard/ns#hasEmail",
hasValue: "http://www.w3.org/2006/vcard/ns#hasValue",
hasPhoto: "http://www.w3.org/2006/vcard/ns#hasPhoto",
hasTelephone: "http://www.w3.org/2006/vcard/ns#hasTelephone",
title: "http://www.w3.org/2006/vcard/ns#title",
hasUrl: "http://www.w3.org/2006/vcard/ns#hasUrl",
organizationName: "http://www.w3.org/2006/vcard/ns#organization-name",
role: "http://www.w3.org/2006/vcard/ns#organization-name",
fn: "http://www.w3.org/2006/vcard/ns#fn",
Email: "http://www.w3.org/2006/vcard/ns#Email",
email: "http://www.w3.org/2006/vcard/ns#email",
hasEmail: "http://www.w3.org/2006/vcard/ns#hasEmail",
hasValue: "http://www.w3.org/2006/vcard/ns#hasValue",
hasPhoto: "http://www.w3.org/2006/vcard/ns#hasPhoto",
tel: "http://www.w3.org/2006/vcard/ns#tel",
hasTelephone: "http://www.w3.org/2006/vcard/ns#hasTelephone",
title: "http://www.w3.org/2006/vcard/ns#title",
hasUrl: "http://www.w3.org/2006/vcard/ns#hasUrl",
organizationName: "http://www.w3.org/2006/vcard/ns#organization-name",
phone: "http://www.w3.org/2006/vcard/ns#phone",
role: "http://www.w3.org/2006/vcard/ns#role",
value: "http://www.w3.org/2006/vcard/ns#value",
telephoneType: "http://www.w3.org/2006/vcard/ns#TelephoneType",
} as const;
85 changes: 85 additions & 0 deletions test/unit/email.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { DataFactory, Parser, Store } from "n3"
import assert from "node:assert"
import { describe, it } from "node:test"

import { Email } from "@solid/object";

describe("Email tests", () => {

const sampleRDF = `
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<https://example.org/person/1>
a vcard:Individual ;
vcard:fn "Alice" ;
vcard:hasEmail <https://example.org/email/1> .

<https://example.org/email/1>
a vcard:Email ;
vcard:value "alice@example.org" ;
rdf:type vcard:Work .
`;

it("should parse and retrieve email address", () => {
const store = new Store()
store.addQuads(new Parser().parse(sampleRDF))

const email = new Email(
DataFactory.namedNode("https://example.org/email/1"),
store,
DataFactory
)

assert.equal(email.emailAddress, "alice@example.org")
assert.equal(typeof email.emailAddress, "string")
})

it("should allow setting email address", () => {
const store = new Store()
store.addQuads(new Parser().parse(sampleRDF))

const email = new Email(
DataFactory.namedNode("https://example.org/email/1"),
store,
DataFactory
)

email.emailAddress = "bob@example.org"

assert.equal(email.emailAddress, "bob@example.org")
})

it("should parse and retrieve email type", () => {
const store = new Store()
store.addQuads(new Parser().parse(sampleRDF))

const email = new Email(
DataFactory.namedNode("https://example.org/email/1"),
store,
DataFactory
)

const emailType = DataFactory.namedNode("http://www.w3.org/2006/vcard/ns#Home")

assert.ok(emailType !== undefined)
assert.equal(typeof emailType, "object")
assert.equal(emailType.value, "http://www.w3.org/2006/vcard/ns#Home")
})

it("should allow setting email type", () => {
const store = new Store()

const email = new Email(
DataFactory.namedNode("https://example.org/email/2"),
store,
DataFactory
)

email.emailAddress = "test@example.org"
email.emailType = "http://www.w3.org/2006/vcard/ns#Home"

assert.equal(email.emailType, "http://www.w3.org/2006/vcard/ns#Home")
})

})
105 changes: 105 additions & 0 deletions test/unit/telephone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { DataFactory, Parser, Store } from "n3"
import assert from "node:assert"
import { describe, it } from "node:test"

import { Telephone } from "@solid/object";

describe("Telephone tests", () => {

const sampleRDF = `
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> .

<https://example.org/person/1>
a vcard:Individual ;
vcard:fn "Alice" ;
vcard:hasTelephone <https://example.org/phone/1> .

<https://example.org/phone/1>
a vcard:Telephone ;
vcard:hasValue "+1234567890" ;
vcard:TelephoneType vcard:Cell .
`;

it("should parse and retrieve phone number", () => {
const store = new Store()
store.addQuads(new Parser().parse(sampleRDF))

const telephone = new Telephone(
DataFactory.namedNode("https://example.org/phone/1"),
store,
DataFactory
)

assert.equal(telephone.phoneNumber, "+1234567890")
assert.equal(typeof telephone.phoneNumber, "string")
})

it("should allow setting phone number", () => {
const store = new Store()
store.addQuads(new Parser().parse(sampleRDF))

const telephone = new Telephone(
DataFactory.namedNode("https://example.org/phone/1"),
store,
DataFactory
)

telephone.phoneNumber = "+0987654321"

assert.equal(telephone.phoneNumber, "+0987654321")
})

it("should parse and retrieve phone type", () => {
const store = new Store()
store.addQuads(new Parser().parse(sampleRDF))

const telephone = new Telephone(
DataFactory.namedNode("https://example.org/phone/1"),
store,
DataFactory
)

const phoneType = telephone.phoneType

assert.ok(phoneType !== undefined)
assert.equal(typeof phoneType, "string")
assert.equal(phoneType, "http://www.w3.org/2006/vcard/ns#Cell")
})

it("should allow setting phone type", () => {
const store = new Store()

const telephone = new Telephone(
DataFactory.namedNode("https://example.org/phone/2"),
store,
DataFactory
)

telephone.phoneNumber = "+1112223333"
telephone.phoneType = "http://www.w3.org/2006/vcard/ns#Car"

assert.equal(telephone.phoneType, "http://www.w3.org/2006/vcard/ns#Car")
})

it("should throw when phone number is missing", () => {
const noPhoneRDF = `
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> .

<https://example.org/phone/empty>
a vcard:Telephone .
`
const store = new Store()
store.addQuads(new Parser().parse(noPhoneRDF))

const telephone = new Telephone(
DataFactory.namedNode("https://example.org/phone/empty"),
store,
DataFactory
)

assert.throws(() => {
telephone.phoneNumber
})
})

})