0.1.1 • Published 1 year ago

@rdfjs/prefix-map v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@rdfjs/prefix-map

build status npm version

A Map for RDF/JS prefixes.

This package implements the JavaScript Map interface for RDF prefixes. Key-Value pairs are stored with the prefix as the key as a string and the namespaces as the value in an RDF/JS NamedNode object. There are some additional convenience methods to handle CURIEs and prefix events of RDF/JS Quad streams.

Usage

The package exports the constructor of the PrefixMap. New instances with initial pairs can be created just like JavaScript Maps but require an additional factory argument:

import rdf from '@rdfjs/data-model'
import PrefixMap from '@rdfjs/prefix-map'

// create a PrefixMap, fill it with some initial values, and hand over the data model factory
const prefixes = new PrefixMap([
  ['rdf', rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#')],
  ['schema', rdf.namedNode('http://schema.org')]
], { factory: rdf })

// create two NamedNodes, one with a CURIE and one with a full IRI
const personShort = rdf.namedNode('schema:Person')
const typeLong = rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type') 

// convert the CURIE to a full IRI and vice versa
const personLong = prefixes.resolve(personShort)
const typeShort = prefixes.shrink(typeLong)

// write the result to the console
console.log(personLong.value) // -> http://schema.org/Person
console.log(typeShort.value) // -> rdf:type