rdfprefix
v1.0.0
Published
RDF Prefix helper to expand and compact iri (compatible with json-ld context)
Downloads
69
Maintainers
Readme
rdfprefix
RDF Prefix helper to expand and compact iri (compatible with json-ld context)
Features
- Expand prefixed iri to iri
- Compact iri to prefixed iri
- Support JSON-LD context as prefix definition
Installation
npm install --save rdfprefix
Usage
var rdfprefix = require('rdfprefix')
// Basic initialization
var prefixes = rdfprefix()
// Initialize with prefixes
var prefixes = rdfprefix({schema: 'http://schema.org'})
// Initialize with an array of prefixes
var prefixes = rdfprefix([
{owl: 'http://www.w3.org/2002/07/owl#'},
{schema: 'http://schema.org/'}
])
// Supports Json-ld @context
var prefixes = rdfprefix({
'@vocab': 'http://purl.org/dc/terms/',
'schema': 'http://schema.org/',
'displayName': 'schema:name',
'alias': {
'@id': 'schema:alternateName',
'@container': '@set'
},
'schema:sameAs': {
'@type': 'schema:URL',
'@container': '@set'
}
})
// Add prefixes...
prefixes('owl', 'http://www.w3.org/2002/07/owl#')
prefixes({
'cc': 'http://creativecommons.org/ns#',
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'Class': 'rdfs:Class'
})
// Use prefixes
var schema = prefixes('schema')
schema('name') // -> 'http://schema.org/name'
// Expand prefixed iri
prefixes.expand('schema:name') // -> 'http://schema.org/name'
prefixes.expand('abstract') // -> 'http://purl.org/dc/terms/abstract' (see '@vocab')
prefixes.expand('http://www.exemple.org/play') // -> 'http://www.exemple.org/play'
prefixes.expand('undefinedprefix:foo') // -> throw Error()
prefixes.expand('undefinedprefix:foo', true) // -> 'undefinedprefix:foo' (tolerant = true)
// Compact iri
prefix.compact('http://schema.org/name') // -> 'schema:name'
prefix.compact('http://purl.org/dc/terms/abstract') // -> 'abstract' (see '@vocab')
prefix.compact('http://www.exemple.org/play') // -> 'http://www.exemple.org/play'
// Get a serializable copy of prefixes
prefix.toJSON() // -> { schema: 'http://schema.org/', ... } (without '@vocab')
// Like toJSON but with the '@vocab' prefix if any
prefix.toContext() // -> { '@vocab': 'http://purl.org/dc/terms/abstract', schema: ... }
Test coverage
| | | | ------------ | -------------- | | Statements | 100% ( 76/76 ) | | Branches | 100% ( 52/52 ) | | Functions | 100% ( 14/14 ) | | Lines | 100% ( 76/76 ) |