configurable parts

pull/9/head
Johannes J. Schmidt 11 years ago
parent ed9f5886e8
commit 6c88aa2372

@ -1,14 +1,25 @@
# docuri [![Build Status](https://travis-ci.org/jo/docuri.svg?branch=master)](https://travis-ci.org/jo/docuri)
Rich document ids for CouchDB.
Rich document ids for CouchDB:
`type/id/subtype/index/version`
eg `movie/blade-runner/gallery-image/12/medium`
For example: `movie/blade-runner/gallery-image/12/medium`
Docuris have many advantages:
* You can access the doc type (eg. in changes feed)
* They sort well in Futon
* They tell a lot about the document
* You can rely on a schema and construct ids of dependend documents (eg. a specific version of an image)
* You can easily delete related documents (eg. by requesting a range from `_all_docs`)
and I'm sure I forgot to mention the best...
Give Docuris a try!
## Usage
Parse id string:
```js
require('docuri').parse('mytype/myid/mysubtype/myindex/myversion');
var docuri = require('docuri');
docuri.parse('mytype/myid/mysubtype/myindex/myversion');
// {
// type: 'mytype',
// id: 'myid',
@ -20,7 +31,7 @@ require('docuri').parse('mytype/myid/mysubtype/myindex/myversion');
Build id string from object:
```js
require('docuri').stringify({
docuri.stringify({
type: 'mytype',
id: 'myid',
subtype: 'mysubtype',
@ -32,12 +43,30 @@ require('docuri').stringify({
Change id string components:
```js
require('docuri').merge('mytype/myid/mysubtype/myindex/myversion', {
docuri.merge('mytype/myid/mysubtype/myindex/myversion', {
type: 'my_new_type',
});
// 'my_new_type/myid/mysubtype/myindex/myversion'
```
Use custom definition:
```js
docuri(['id', 'meta']).parse('42/answer');
// {
// id: '42',
// meta: 'answer'
// }
```
Access current definition:
```js
docuri();
// ['type', 'id', 'subtype', 'index', 'version']
docuri(['id', 'meta']);
docuri();
// ['id', 'meta']
```
## Browser support
To use docid in your client-side application, browserify it like this:
@ -54,11 +83,6 @@ To run the unit tests:
npm test
```
For JShint:
```shell
npm run jshint
```
## License
Copyright (c) 2014 Johannes J. Schmidt, null2 GmbH
Licensed under the MIT license.

@ -8,8 +8,18 @@
// type/id/subtype/index/version
var PARTS = ['type', 'id', 'subtype', 'index', 'version'];
function docuri(parts) {
if (parts) {
PARTS = parts;
exports.parse = function(str) {
return docuri;
}
return PARTS;
}
docuri.parse = function(str) {
str = str || '';
return str.split('/').reduce(function(obj, value, i) {
@ -21,7 +31,7 @@ exports.parse = function(str) {
}, {});
};
exports.stringify = function(obj) {
docuri.stringify = function(obj) {
obj = obj || {};
return PARTS.map(function(part) {
@ -29,10 +39,10 @@ exports.stringify = function(obj) {
}).join('/').replace(/\/+$/, '');
};
exports.merge = function(str, objToMerge) {
docuri.merge = function(str, objToMerge) {
objToMerge = objToMerge || {};
var obj = exports.parse(str);
var obj = docuri.parse(str);
PARTS.forEach(function(part) {
if (objToMerge[part]) {
@ -40,5 +50,9 @@ exports.merge = function(str, objToMerge) {
}
});
return exports.stringify(obj);
return docuri.stringify(obj);
};
module.exports = docuri;

@ -1,11 +1,10 @@
{
"name": "docuri",
"version": "2.0.1",
"version": "2.1.0",
"description": "Rich document ids for CouchDB",
"main": "index.js",
"scripts": {
"test": "tap test/*.js",
"jshint": "jshint index.js test/*.js"
"test": "tap test/*.js"
},
"repository": {
"type": "git",
@ -21,7 +20,6 @@
},
"homepage": "https://github.com/jo/docuri",
"devDependencies": {
"tap": "^0.4.8",
"jshint": "^2.5.0"
"tap": "^0.4.8"
}
}

@ -1,5 +1,3 @@
'use strict';
var test = require('tap').test;
var merge = require('..').merge;

@ -0,0 +1,28 @@
var test = require('tap').test;
var docuri = require('..');
test('default configuration', function(t) {
t.deepEqual(docuri(), ['type', 'id', 'subtype', 'index', 'version'], 'should return default parts');
t.end();
});
test('set configuration', function(t) {
var parts = ['my', 'parts'];
t.type(docuri(parts).merge, 'function', 'should return docuri api: merge');
t.type(docuri(parts).parse, 'function', 'should return docuri api: parse');
t.type(docuri(parts).stringify, 'function', 'should return docuri api: stringify');
t.end();
});
test('change configuration', function(t) {
var parts = ['my', 'parts'];
docuri(parts);
t.deepEqual(docuri(), parts, 'should return custom parts');
t.end();
});
test('use changed configuration', function(t) {
var parts = ['my', 'parts'];
t.deepEqual(docuri(parts).parse('one/two'), { my: 'one', parts: 'two'}, 'should use custom parts');
t.end();
});
Loading…
Cancel
Save