-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalizedLabel.js
42 lines (38 loc) · 1.15 KB
/
localizedLabel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { directive, Directive } from 'lit/directive.js'
import { rdfs } from '@tpluscode/rdf-ns-builders'
import { taggedLiteral } from './taggedLiteral.js'
/**
* @typedef {import('clownface').AnyPointer | import('@tpluscode/rdfine').RdfResource | undefined} PointerLike
*/
class LocalizedLabelDirective extends Directive {
/**
*
* @param {PointerLike} resource
* @param {object} [options]
* @param {import('@rdfjs/types').NamedNode | Array<import('@rdfjs/types').NamedNode>} [options.property]
* @param {*} [options.fallback]
* @returns {string|*}
*/
render(resource, { property = rdfs.label, fallback = '' } = {}) {
/**
* @private
* @type {import('@rdfjs/types').NamedNode}
*/
this.property = property
/**
* @private
* @type {string}
*/
this.fallback = fallback
if (resource) {
/**
* @private
* @type {PointerLike}
*/
this.pointer = 'pointer' in resource ? resource.pointer : resource
}
const label = this.pointer?.out(this.property)
return taggedLiteral(label, { fallback })
}
}
export const localizedLabel = directive(LocalizedLabelDirective)