diff --git a/README.md b/README.md index c0118f2..abc2887 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,37 @@ -# solid-namespace +# Solid Namespace [![](https://img.shields.io/badge/project-Solid-7C4DFF.svg?style=flat)](https://github.com/solid/solid) [![NPM Version](https://img.shields.io/npm/v/solid-namespace.svg?style=flat)](https://npm.im/solid-namespace) A collection of common RDF namespaces used in the Solid project. +solid-namespace can be used with any RDF/JS-compatible library (e.g. [rdflib.js](/linkeddata/rdflib.js)). + + ## Usage +You can use this library in two ways. + +1. With a RDF JS library to get NamedNodes +2. Without a library to get url strings + +### With a rdf library + +If a rdf library is provided then the map of namespaces given will be the result of calling `rdflib.Namespace` on the namespace urls. + +```js +const $rdf = require('rdflib'); +const ns = require('solid-namespace')($rdf); +const store = $rdf.graph(); + +let me = ...; +let name = store.any(me, ns.vcard('fn')) || store.any(me, ns.foaf('name')); + +console.log(ns.foaf('name')); // -> NamedNode() +``` + +### Without a rdf library + ```js -var rdf = require('rdflib') // optional -var vocab = require('solid-namespace')(rdf) // or require('solid-namespace')() -console.log(vocab.foaf('name')) -// -> NamedNode() +const ns = require('solid-namespace')(); +console.log(ns.foaf('name')); // -> "http://xmlns.com/foaf/0.1/name" ``` diff --git a/index.js b/index.js index ffa7214..0435253 100644 --- a/index.js +++ b/index.js @@ -1,41 +1,70 @@ -'use strict' /** - * Provides a hashmap of relevant vocabs / namespaces. + * Provides a way to access commonly used namespaces + * * Usage: * * ``` - * var rdf = require('rdflib') // optional - * var vocab = require('solid-vocab')(rdf) // or require('solid-vocab')() - * console.log(vocab.foaf('name')) // -> + * const $rdf = require('rdflib'); //or any other RDF/JS-compatible library + * const ns = require('solid-namespace')($rdf); + * const store = $rdf.graph(); + * + * let me = ...; + * let name = store.any(me, ns.vcard(‘fn’)) || store.any(me, ns.foaf(‘name’)); * ``` * @module vocab */ +const aliases = { + acl: 'http://www.w3.org/ns/auth/acl#', + arg: 'http://www.w3.org/ns/pim/arg#', + cal: 'http://www.w3.org/2002/12/cal/ical#', + contact: 'http://www.w3.org/2000/10/swap/pim/contact#', + dc: 'http://purl.org/dc/elements/1.1/', + dct: 'http://purl.org/dc/terms/', + doap: 'http://usefulinc.com/ns/doap#', + foaf: 'http://xmlns.com/foaf/0.1/', + http: 'http://www.w3.org/2007/ont/http#', + httph: 'http://www.w3.org/2007/ont/httph#', + icalTZ: 'http://www.w3.org/2002/12/cal/icaltzd#', // Beware: not cal: + ldp: 'http://www.w3.org/ns/ldp#', + link: 'http://www.w3.org/2007/ont/link#', + log: 'http://www.w3.org/2000/10/swap/log#', + meeting: 'http://www.w3.org/ns/pim/meeting#', + mo: 'http://purl.org/ontology/mo/', + owl: 'http://www.w3.org/2002/07/owl#', + pad: 'http://www.w3.org/ns/pim/pad#', + patch: 'http://www.w3.org/ns/pim/patch#', + qu: 'http://www.w3.org/2000/10/swap/pim/qif#', + trip: 'http://www.w3.org/ns/pim/trip#', + rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', + rdfs: 'http://www.w3.org/2000/01/rdf-schema#', + rss: 'http://purl.org/rss/1.0/', + sched: 'http://www.w3.org/ns/pim/schedule#', + schema: 'http:/schema.org/', // @@ beware confusion with documents no 303 + sioc: 'http://rdfs.org/sioc/ns#', + solid: 'http://www.w3.org/ns/solid/terms#', + space: 'http://www.w3.org/ns/pim/space#', + stat: 'http://www.w3.org/ns/posix/stat#', + tab: 'http://www.w3.org/2007/ont/link#', + tabont: 'http://www.w3.org/2007/ont/link#', + ui: 'http://www.w3.org/ns/ui#', + vcard: 'http://www.w3.org/2006/vcard/ns#', + wf: 'http://www.w3.org/2005/01/wf/flow#', + xsd: 'http://www.w3.org/2001/XMLSchema#' +} /** - * @param [rdf] {RDF} Optional RDF Library (such as rdflib.js or rdf-ext) to - * inject + * @param [rdflib] {RDF} Optional RDF Library (such as rdflib.js or rdf-ext) to inject */ -function vocab (rdf) { - var ns = require('rdf-ns')(rdf) - var vocabMap = { - 'acl': ns.base('http://www.w3.org/ns/auth/acl#'), - 'app': ns.base('http://www.w3.org/ns/solid/app#'), - 'cert': ns.base('http://www.w3.org/ns/auth/cert#'), - 'dct': ns.base('http://purl.org/dc/terms/'), - 'foaf': ns.base('http://xmlns.com/foaf/0.1/'), - 'ldp': ns.base('http://www.w3.org/ns/ldp#'), - 'owl': ns.base('http://www.w3.org/2002/07/owl#'), - 'pim': ns.base('http://www.w3.org/ns/pim/space#'), - 'rdf': ns.base('http://www.w3.org/1999/02/22-rdf-syntax-ns#'), - 'rdfs': ns.base('http://www.w3.org/2000/01/rdf-schema#'), - 'schema': ns.base('http://schema.org/'), - 'sioc': ns.base('http://rdfs.org/sioc/ns#'), - 'solid': ns.base('http://www.w3.org/ns/solid/terms#'), - 'stat': ns.base('http://www.w3.org/ns/posix/stat#'), - 'vcard': ns.base('http://www.w3.org/2006/vcard/ns#'), - 'xsd': ns.base('http://www.w3.org/2001/XMLSchema#') - } - return vocabMap -} +function vocab (rdf = { namedNode: u => u }) { + const namespaces = {} + for (const alias in aliases) { + const expansion = aliases[alias] + namespaces[alias] = function (localName = '') { + return rdf.namedNode(expansion + localName) + } + }; + + return namespaces +}; module.exports = vocab diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..41bb984 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "solid-namespace", + "version": "0.2.0", + "lockfileVersion": 1 +} diff --git a/package.json b/package.json index 9fb5a9c..a6d8da8 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "solid-namespace", - "version": "0.1.0", + "version": "0.2.0", "description": "A collection of common RDF namespaces used in the Solid project", "main": "./index.js", "scripts": { - "standard": "standard src/*" + "standard": "standard index.js", + "test": "node ./test.js" }, "repository": { "type": "git", @@ -22,15 +23,16 @@ "rest" ], "author": "Dmitri Zagidulin ", + "contributors": [ + "Dmitri Zagidulin ", + "Jordan Shurmer " + ], "license": "MIT", "bugs": { "url": "https://github.com/solid/solid-namespace/issues" }, "homepage": "https://github.com/solid/solid-namespace", - "dependencies": { - "rdf-ns": "^0.1.0" - }, - "devDependencies": {}, + "dependencies": {}, "standard": { "globals": [] } diff --git a/test.js b/test.js new file mode 100644 index 0000000..343759c --- /dev/null +++ b/test.js @@ -0,0 +1,13 @@ +const assert = require('assert'); + +//Test the non-rdflib functionality +const ns = require('./index.js')(); +assert.equal(ns.schema('Recipe'), 'http:/schema.org/Recipe'); + + +//Test the rdflib functionality +const rdflib = { + namedNode: val => 'RDF::'+val +}; +const rdfns = require('./index.js')(rdflib); +assert.equal(rdfns.schema('Recipe'), 'RDF::http:/schema.org/Recipe');