mirror of https://github.com/jo/docuri.git
first commit
commit
55f7a6715a
@ -0,0 +1 @@
|
|||||||
|
node_modules
|
@ -0,0 +1,48 @@
|
|||||||
|
docuri
|
||||||
|
======
|
||||||
|
Rich document ids for CouchDB.
|
||||||
|
|
||||||
|
`type/id/subtype/version/index`
|
||||||
|
|
||||||
|
eg `movie/blade-runner/gallery-image/medium/12`
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
Parse id string:
|
||||||
|
```js
|
||||||
|
require('docuri').parse('mytype/myid/mysubtype/myversion/myindex');
|
||||||
|
// {
|
||||||
|
// type: 'mytype',
|
||||||
|
// id: 'myid',
|
||||||
|
// subtype: 'mysubtype',
|
||||||
|
// version: 'myversion',
|
||||||
|
// index: 'myindex'
|
||||||
|
// }
|
||||||
|
```
|
||||||
|
|
||||||
|
Build id string from object:
|
||||||
|
```js
|
||||||
|
require('docuri').stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion',
|
||||||
|
index: 'myindex'
|
||||||
|
});
|
||||||
|
// 'mytype/myid/mysubtype/myversion/myindex'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Development
|
||||||
|
To run the unit tests:
|
||||||
|
```shell
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
For JShint:
|
||||||
|
```shell
|
||||||
|
npm run jshint
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
Copyright (c) 2014 Johannes J. Schmidt, null2 GmbH
|
||||||
|
Licensed under the MIT license.
|
||||||
|
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* docuri
|
||||||
|
*
|
||||||
|
* Copyright (c) 2014 null2 GmbH Berlin
|
||||||
|
* Licensed under the MIT license.
|
||||||
|
*/
|
||||||
|
|
||||||
|
exports.parse = function(str) {
|
||||||
|
str = str || '';
|
||||||
|
|
||||||
|
var obj = {};
|
||||||
|
var parts = str.split('/');
|
||||||
|
|
||||||
|
var type = parts.shift();
|
||||||
|
var id = parts.shift();
|
||||||
|
var subtype = parts.shift();
|
||||||
|
var version = parts.shift();
|
||||||
|
var index = parts.shift();
|
||||||
|
|
||||||
|
if (type) obj.type = type;
|
||||||
|
if (id) obj.id = id;
|
||||||
|
if (subtype) obj.subtype = subtype;
|
||||||
|
if (version) obj.version = version;
|
||||||
|
if (index) obj.index = index;
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.stringify = function(obj) {
|
||||||
|
obj = obj || {};
|
||||||
|
|
||||||
|
var parts = [];
|
||||||
|
if (obj.type) {
|
||||||
|
parts.push(obj.type);
|
||||||
|
} else {
|
||||||
|
if (obj.id || obj.subtype || obj.version || obj.index) parts.push('');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.id) {
|
||||||
|
parts.push(obj.id);
|
||||||
|
} else {
|
||||||
|
if (obj.subtype || obj.version || obj.index) parts.push('');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.subtype) {
|
||||||
|
parts.push(obj.subtype);
|
||||||
|
} else {
|
||||||
|
if (obj.version || obj.index) parts.push('');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.version) {
|
||||||
|
parts.push(obj.version);
|
||||||
|
} else {
|
||||||
|
if (obj.index) parts.push('');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.index) parts.push(obj.index);
|
||||||
|
|
||||||
|
return parts.join('/');
|
||||||
|
};
|
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "docuri",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Rich document ids for CouchDB",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "tap test/*.js",
|
||||||
|
"jshint": "jshint index.js test/*.js"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git://github.com/jo/docuri.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"couchdb"
|
||||||
|
],
|
||||||
|
"author": "Johannes J. Schmidt",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/jo/docuri/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/jo/docuri",
|
||||||
|
"devDependencies": {
|
||||||
|
"tap": "^0.4.8",
|
||||||
|
"jshint": "^2.5.0"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
var test = require('tap').test;
|
||||||
|
var parse = require('..').parse;
|
||||||
|
|
||||||
|
test('missing argument', function(t) {
|
||||||
|
t.deepEqual(parse(), {}, 'should return empty object');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('empty string', function(t) {
|
||||||
|
t.deepEqual(parse(''), {}, 'should return empty object');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type', function(t) {
|
||||||
|
t.deepEqual(parse('mytype'), { type: 'mytype' }, 'should return object with type');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id', function(t) {
|
||||||
|
t.deepEqual(parse('mytype/myid'), {
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid'
|
||||||
|
}, 'should return object with type and id');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id/subtype', function(t) {
|
||||||
|
t.deepEqual(parse('mytype/myid/mysubtype'), {
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype'
|
||||||
|
}, 'should return object with type, id and subtype');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id/subtype/version', function(t) {
|
||||||
|
t.deepEqual(parse('mytype/myid/mysubtype/myversion'), {
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion'
|
||||||
|
}, 'should return object with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id/subtype/version/index', function(t) {
|
||||||
|
t.deepEqual(parse('mytype/myid/mysubtype/myversion/myindex'), {
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion',
|
||||||
|
index: 'myindex'
|
||||||
|
}, 'should return object with type, id, subtype, version and index');
|
||||||
|
t.end();
|
||||||
|
});
|
@ -0,0 +1,129 @@
|
|||||||
|
var test = require('tap').test;
|
||||||
|
var stringify = require('..').stringify;
|
||||||
|
|
||||||
|
test('missing argument', function(t) {
|
||||||
|
t.deepEqual(stringify(), '', 'should return empty string');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('empty object', function(t) {
|
||||||
|
t.deepEqual(stringify({}), '', 'should return empty string');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type', function(t) {
|
||||||
|
t.deepEqual(stringify({ type: 'mytype' }), 'mytype', 'should return string with type');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('/id', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
id: 'myid'
|
||||||
|
}), '/myid', 'should return string with type and id');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid'
|
||||||
|
}), 'mytype/myid', 'should return string with type and id');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('//subtype', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
subtype: 'mysubtype'
|
||||||
|
}), '//mysubtype', 'should return string with type, id and subtype');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('/id/subtype', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype'
|
||||||
|
}), '/myid/mysubtype', 'should return string with type, id and subtype');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id/subtype', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype'
|
||||||
|
}), 'mytype/myid/mysubtype', 'should return string with type, id and subtype');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('/id/subtype/version', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion'
|
||||||
|
}), '/myid/mysubtype/myversion', 'should return string with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type//subtype/version', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion'
|
||||||
|
}), 'mytype//mysubtype/myversion', 'should return string with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type///version', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
version: 'myversion'
|
||||||
|
}), 'mytype///myversion', 'should return string with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('//subtype/version', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion'
|
||||||
|
}), '//mysubtype/myversion', 'should return string with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('///version', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
version: 'myversion'
|
||||||
|
}), '///myversion', 'should return string with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id/subtype/version', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion'
|
||||||
|
}), 'mytype/myid/mysubtype/myversion', 'should return string with type, id, subtype and version');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('////index', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
index: 'myindex'
|
||||||
|
}), '////myindex', 'should return string with type, id, subtype, version and index');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('type/id/subtype/version/index', function(t) {
|
||||||
|
t.deepEqual(stringify({
|
||||||
|
type: 'mytype',
|
||||||
|
id: 'myid',
|
||||||
|
subtype: 'mysubtype',
|
||||||
|
version: 'myversion',
|
||||||
|
index: 'myindex'
|
||||||
|
}), 'mytype/myid/mysubtype/myversion/myindex', 'should return string with type, id, subtype, version and index');
|
||||||
|
t.end();
|
||||||
|
});
|
Loading…
Reference in New Issue