Skip to content

Commit a39a660

Browse files
author
Benjamin Forster
committed
add prefix helpers + tests
1 parent 2fd6e64 commit a39a660

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

lib/prefixes.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const PREFIX_KEY = '@prefix/'
2+
const PREFIX_REGEX = /^(\w+):/
3+
const DEFAULT_PREFIXES = {
4+
_: 'hg://',
5+
foaf: 'http://xmlns.com/foaf/0.1/'
6+
}
7+
8+
function toKey (prefix) {
9+
return PREFIX_KEY + prefix
10+
}
11+
function fromKey (key) {
12+
return key.replace(PREFIX_KEY, '')
13+
}
14+
15+
function fromNodes (nodes) {
16+
return {
17+
prefix: fromKey(nodes[0].key),
18+
uri: nodes[0].value
19+
}
20+
}
21+
22+
function toPrefixed (uri, prefixes) {
23+
if (!prefixes) return uri
24+
const prefix = Object.keys(prefixes).find(v => uri.startsWith(prefixes[v]))
25+
if (!prefix) return uri
26+
return uri.replace(prefixes[prefix], prefix + ':')
27+
}
28+
29+
function fromPrefixed (uri, prefixes) {
30+
if (!prefixes) return uri
31+
const match = uri.match(PREFIX_REGEX)
32+
if (match && prefixes[match[1]]) {
33+
return uri.replace(PREFIX_REGEX, prefixes[match[1]])
34+
}
35+
return uri
36+
}
37+
38+
module.exports = {
39+
DEFAULT_PREFIXES,
40+
PREFIX_KEY,
41+
toKey,
42+
fromKey,
43+
fromNodes,
44+
toPrefixed,
45+
fromPrefixed
46+
}

test/prefixes.spec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-env mocha */
2+
const expect = require('chai').expect
3+
const prefixes = require('../lib/prefixes')
4+
5+
describe('prefix utilities', () => {
6+
describe('toKey', () => {
7+
it('converts a prefix to a key', () => {
8+
expect(prefixes.toKey('this')).to.eql('@prefix/this')
9+
})
10+
})
11+
12+
describe('fromKey', () => {
13+
it('converts a key to its prefix', () => {
14+
expect(prefixes.fromKey('@prefix/this')).to.eql('this')
15+
})
16+
})
17+
18+
describe('fromNodes', () => {
19+
it('converts hyperdb nodes to prefix/uri object', () => {
20+
const dummyNodes = [{ key: '@prefix/this', value: 'http://this.example.com' }]
21+
expect(prefixes.fromNodes(dummyNodes)).to.eql({ uri: 'http://this.example.com', prefix: 'this' })
22+
})
23+
it('ignores conflicts', () => {
24+
const dummyNodes = [
25+
{ key: '@prefix/this', value: 'http://this.example.com' },
26+
{ key: '@prefix/this', value: 'http://conflict.example.com' }
27+
]
28+
expect(prefixes.fromNodes(dummyNodes)).to.eql({ uri: 'http://this.example.com', prefix: 'this' })
29+
})
30+
})
31+
32+
describe('toPrefixed', () => {
33+
it('returns unmodified string if prefix is undefined', () => {
34+
const str = 'http://test.com/this/ok#wow'
35+
const prefixed = prefixes.toPrefixed(str)
36+
expect(prefixed).to.eql(str)
37+
})
38+
it('returns unmodified string if prefix is not set', () => {
39+
const str = 'http://test.com/this/ok#wow'
40+
const prefixed = prefixes.toPrefixed(str, { 'wow': 'http://wow.com/' })
41+
expect(prefixed).to.eql(str)
42+
})
43+
it('returns prefix string if prefix is present', () => {
44+
const str = 'http://wow.com/now/this#and_hashed'
45+
const prefixed = prefixes.toPrefixed(str, { 'wow': 'http://wow.com/' })
46+
expect(prefixed).to.eql('wow:now/this#and_hashed')
47+
})
48+
})
49+
50+
describe('fromPrefixed', () => {
51+
it('returns unmodified string if prefix is undefined', () => {
52+
const str = 'some:thing'
53+
const prefixed = prefixes.fromPrefixed(str)
54+
expect(prefixed).to.eql(str)
55+
})
56+
it('returns unmodified string if prefix is not set', () => {
57+
const str = 'some:thing'
58+
const prefixed = prefixes.fromPrefixed(str, { 'wow': 'http://wow.com/' })
59+
expect(prefixed).to.eql(str)
60+
})
61+
it('returns prefix string if prefix is present', () => {
62+
const str = 'wow:now'
63+
const prefixed = prefixes.fromPrefixed(str, { 'wow': 'http://wow.com/' })
64+
expect(prefixed).to.eql('http://wow.com/now')
65+
})
66+
})
67+
})

0 commit comments

Comments
 (0)