Skip to content

Commit 0900cfc

Browse files
Merge pull request #3 from JordanShurmer/master
Export rdflib NamedNodes
2 parents 8b3335c + 0289e37 commit 0900cfc

File tree

5 files changed

+112
-40
lines changed

5 files changed

+112
-40
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
# solid-namespace
1+
# Solid Namespace
22
[![](https://img.shields.io/badge/project-Solid-7C4DFF.svg?style=flat)](https://github.com/solid/solid)
33
[![NPM Version](https://img.shields.io/npm/v/solid-namespace.svg?style=flat)](https://npm.im/solid-namespace)
44

55
A collection of common RDF namespaces used in the Solid project.
66

7+
solid-namespace can be used with any RDF/JS-compatible library (e.g. [rdflib.js](/linkeddata/rdflib.js)).
8+
9+
710
## Usage
811

12+
You can use this library in two ways.
13+
14+
1. With a RDF JS library to get NamedNodes
15+
2. Without a library to get url strings
16+
17+
### With a rdf library
18+
19+
If a rdf library is provided then the map of namespaces given will be the result of calling `rdflib.Namespace` on the namespace urls.
20+
21+
```js
22+
const $rdf = require('rdflib');
23+
const ns = require('solid-namespace')($rdf);
24+
const store = $rdf.graph();
25+
26+
let me = ...;
27+
let name = store.any(me, ns.vcard('fn')) || store.any(me, ns.foaf('name'));
28+
29+
console.log(ns.foaf('name')); // -> NamedNode(<http://xmlns.com/foaf/0.1/name>)
30+
```
31+
32+
### Without a rdf library
33+
934
```js
10-
var rdf = require('rdflib') // optional
11-
var vocab = require('solid-namespace')(rdf) // or require('solid-namespace')()
12-
console.log(vocab.foaf('name'))
13-
// -> NamedNode(<http://xmlns.com/foaf/0.1/name>)
35+
const ns = require('solid-namespace')();
36+
console.log(ns.foaf('name')); // -> "http://xmlns.com/foaf/0.1/name"
1437
```

index.js

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,70 @@
1-
'use strict'
21
/**
3-
* Provides a hashmap of relevant vocabs / namespaces.
2+
* Provides a way to access commonly used namespaces
3+
*
44
* Usage:
55
*
66
* ```
7-
* var rdf = require('rdflib') // optional
8-
* var vocab = require('solid-vocab')(rdf) // or require('solid-vocab')()
9-
* console.log(vocab.foaf('name')) // -> <http://xmlns.com/foaf/0.1/name>
7+
* const $rdf = require('rdflib'); //or any other RDF/JS-compatible library
8+
* const ns = require('solid-namespace')($rdf);
9+
* const store = $rdf.graph();
10+
*
11+
* let me = ...;
12+
* let name = store.any(me, ns.vcard(‘fn’)) || store.any(me, ns.foaf(‘name’));
1013
* ```
1114
* @module vocab
1215
*/
16+
const aliases = {
17+
acl: 'http://www.w3.org/ns/auth/acl#',
18+
arg: 'http://www.w3.org/ns/pim/arg#',
19+
cal: 'http://www.w3.org/2002/12/cal/ical#',
20+
contact: 'http://www.w3.org/2000/10/swap/pim/contact#',
21+
dc: 'http://purl.org/dc/elements/1.1/',
22+
dct: 'http://purl.org/dc/terms/',
23+
doap: 'http://usefulinc.com/ns/doap#',
24+
foaf: 'http://xmlns.com/foaf/0.1/',
25+
http: 'http://www.w3.org/2007/ont/http#',
26+
httph: 'http://www.w3.org/2007/ont/httph#',
27+
icalTZ: 'http://www.w3.org/2002/12/cal/icaltzd#', // Beware: not cal:
28+
ldp: 'http://www.w3.org/ns/ldp#',
29+
link: 'http://www.w3.org/2007/ont/link#',
30+
log: 'http://www.w3.org/2000/10/swap/log#',
31+
meeting: 'http://www.w3.org/ns/pim/meeting#',
32+
mo: 'http://purl.org/ontology/mo/',
33+
owl: 'http://www.w3.org/2002/07/owl#',
34+
pad: 'http://www.w3.org/ns/pim/pad#',
35+
patch: 'http://www.w3.org/ns/pim/patch#',
36+
qu: 'http://www.w3.org/2000/10/swap/pim/qif#',
37+
trip: 'http://www.w3.org/ns/pim/trip#',
38+
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
39+
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
40+
rss: 'http://purl.org/rss/1.0/',
41+
sched: 'http://www.w3.org/ns/pim/schedule#',
42+
schema: 'http:/schema.org/', // @@ beware confusion with documents no 303
43+
sioc: 'http://rdfs.org/sioc/ns#',
44+
solid: 'http://www.w3.org/ns/solid/terms#',
45+
space: 'http://www.w3.org/ns/pim/space#',
46+
stat: 'http://www.w3.org/ns/posix/stat#',
47+
tab: 'http://www.w3.org/2007/ont/link#',
48+
tabont: 'http://www.w3.org/2007/ont/link#',
49+
ui: 'http://www.w3.org/ns/ui#',
50+
vcard: 'http://www.w3.org/2006/vcard/ns#',
51+
wf: 'http://www.w3.org/2005/01/wf/flow#',
52+
xsd: 'http://www.w3.org/2001/XMLSchema#'
53+
}
1354

1455
/**
15-
* @param [rdf] {RDF} Optional RDF Library (such as rdflib.js or rdf-ext) to
16-
* inject
56+
* @param [rdflib] {RDF} Optional RDF Library (such as rdflib.js or rdf-ext) to inject
1757
*/
18-
function vocab (rdf) {
19-
var ns = require('rdf-ns')(rdf)
20-
var vocabMap = {
21-
'acl': ns.base('http://www.w3.org/ns/auth/acl#'),
22-
'app': ns.base('http://www.w3.org/ns/solid/app#'),
23-
'cert': ns.base('http://www.w3.org/ns/auth/cert#'),
24-
'dct': ns.base('http://purl.org/dc/terms/'),
25-
'foaf': ns.base('http://xmlns.com/foaf/0.1/'),
26-
'ldp': ns.base('http://www.w3.org/ns/ldp#'),
27-
'owl': ns.base('http://www.w3.org/2002/07/owl#'),
28-
'pim': ns.base('http://www.w3.org/ns/pim/space#'),
29-
'rdf': ns.base('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
30-
'rdfs': ns.base('http://www.w3.org/2000/01/rdf-schema#'),
31-
'schema': ns.base('http://schema.org/'),
32-
'sioc': ns.base('http://rdfs.org/sioc/ns#'),
33-
'solid': ns.base('http://www.w3.org/ns/solid/terms#'),
34-
'stat': ns.base('http://www.w3.org/ns/posix/stat#'),
35-
'vcard': ns.base('http://www.w3.org/2006/vcard/ns#'),
36-
'xsd': ns.base('http://www.w3.org/2001/XMLSchema#')
37-
}
38-
return vocabMap
39-
}
58+
function vocab (rdf = { namedNode: u => u }) {
59+
const namespaces = {}
60+
for (const alias in aliases) {
61+
const expansion = aliases[alias]
62+
namespaces[alias] = function (localName = '') {
63+
return rdf.namedNode(expansion + localName)
64+
}
65+
};
66+
67+
return namespaces
68+
};
4069

4170
module.exports = vocab

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "solid-namespace",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "A collection of common RDF namespaces used in the Solid project",
55
"main": "./index.js",
66
"scripts": {
7-
"standard": "standard src/*"
7+
"standard": "standard index.js",
8+
"test": "node ./test.js"
89
},
910
"repository": {
1011
"type": "git",
@@ -22,15 +23,16 @@
2223
"rest"
2324
],
2425
"author": "Dmitri Zagidulin <[email protected]>",
26+
"contributors": [
27+
"Dmitri Zagidulin <[email protected]>",
28+
"Jordan Shurmer <[email protected]>"
29+
],
2530
"license": "MIT",
2631
"bugs": {
2732
"url": "https://github.com/solid/solid-namespace/issues"
2833
},
2934
"homepage": "https://github.com/solid/solid-namespace",
30-
"dependencies": {
31-
"rdf-ns": "^0.1.0"
32-
},
33-
"devDependencies": {},
35+
"dependencies": {},
3436
"standard": {
3537
"globals": []
3638
}

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const assert = require('assert');
2+
3+
//Test the non-rdflib functionality
4+
const ns = require('./index.js')();
5+
assert.equal(ns.schema('Recipe'), 'http:/schema.org/Recipe');
6+
7+
8+
//Test the rdflib functionality
9+
const rdflib = {
10+
namedNode: val => 'RDF::'+val
11+
};
12+
const rdfns = require('./index.js')(rdflib);
13+
assert.equal(rdfns.schema('Recipe'), 'RDF::http:/schema.org/Recipe');

0 commit comments

Comments
 (0)