|
1 | 1 | # RDF Vocabulary |
| 2 | + |
| 3 | +This library exposes utility functions and types to help working with RDF vocabulary terms. |
| 4 | + |
| 5 | +```ts |
| 6 | +import { createVocabulary } from 'rdf-vocabulary'; |
| 7 | + |
| 8 | +// Creates a vocabulary. |
| 9 | +// The first parameter is the namespace, all the following parameters are terms in the namespace. |
| 10 | +const FOAF = createVocabulary( |
| 11 | + 'http://xmlns.com/foaf/0.1/', |
| 12 | + 'Agent', |
| 13 | + 'knows', |
| 14 | +); |
| 15 | + |
| 16 | +// Full term URIs are then accessible through the vocabulary object. |
| 17 | +assert.equal(FOAF.knows, 'http://xmlns.com/foaf/0.1/knows'); |
| 18 | + |
| 19 | +// Through the `terms` field, the URIs are exposed as a NamedNode |
| 20 | +assert.equal(FOAF.terms.knows.value, 'http://xmlns.com/foaf/0.1/knows'); |
| 21 | + |
| 22 | +// The namespace is accessible in the same way |
| 23 | +assert.equal(FOAF.namespace, 'http://xmlns.com/foaf/0.1/'); |
| 24 | +assert.equal(FOAF.terms.namespace.value, 'http://xmlns.com/foaf/0.1/'); |
| 25 | + |
| 26 | +// New vocabularies can be created using existing vocabulary object |
| 27 | +const FOAF_EXTENDED = extendVocabulary(FOAF, 'name', 'made'); |
| 28 | +assert.equal(FOAF_EXTENDED.name, 'http://xmlns.com/foaf/0.1/name'); |
| 29 | +assert.equal(FOAF_EXTENDED.knows, 'http://xmlns.com/foaf/0.1/knows'); |
| 30 | + |
| 31 | +// There are several utility types |
| 32 | +const local: VocabularyLocal<typeof FOAF> = 'knows'; |
| 33 | +const value: VocabularyValue<typeof FOAF> = 'http://xmlns.com/foaf/0.1/knows'; |
| 34 | +const term: VocabularyTerm<typeof FOAF> = namedNode('http://xmlns.com/foaf/0.1/knows'); |
| 35 | +``` |
0 commit comments