change sequence of version and index

Before this api breaking change a docuri looked like this:
`type/id/subtype/version/index`
Now we swapped version and index:
`type/id/subtype/index/version`
pull/9/head
Johannes J. Schmidt 10 years ago
parent 9f12b25215
commit 461c7ee0db

@ -1,20 +1,20 @@
# docuri [![Build Status](https://travis-ci.org/jo/docuri.svg?branch=master)](https://travis-ci.org/jo/docuri) # 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/version/index` `type/id/subtype/index/version`
eg `movie/blade-runner/gallery-image/medium/12` eg `movie/blade-runner/gallery-image/12/medium`
## Usage ## Usage
Parse id string: Parse id string:
```js ```js
require('docuri').parse('mytype/myid/mysubtype/myversion/myindex'); require('docuri').parse('mytype/myid/mysubtype/myindex/myversion');
// { // {
// type: 'mytype', // type: 'mytype',
// id: 'myid', // id: 'myid',
// subtype: 'mysubtype', // subtype: 'mysubtype',
// version: 'myversion', // index: 'myindex',
// index: 'myindex' // version: 'myversion'
// } // }
``` ```
@ -24,18 +24,18 @@ require('docuri').stringify({
type: 'mytype', type: 'mytype',
id: 'myid', id: 'myid',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion', index: 'myindex',
index: 'myindex' version: 'myversion'
}); });
// 'mytype/myid/mysubtype/myversion/myindex' // 'mytype/myid/mysubtype/myindex/myversion'
``` ```
Change id string components: Change id string components:
```js ```js
require('docuri').merge('mytype/myid/mysubtype/myversion/myindex', { require('docuri').merge('mytype/myid/mysubtype/myindex/myversion', {
type: 'my_new_type', type: 'my_new_type',
}); });
// 'my_new_type/myid/mysubtype/myversion/myindex' // 'my_new_type/myid/mysubtype/myindex/myversion'
``` ```
## Browser support ## Browser support

@ -14,14 +14,14 @@ exports.parse = function(str) {
var type = parts.shift(); var type = parts.shift();
var id = parts.shift(); var id = parts.shift();
var subtype = parts.shift(); var subtype = parts.shift();
var version = parts.shift();
var index = parts.shift(); var index = parts.shift();
var version = parts.shift();
if (type) obj.type = type; if (type) obj.type = type;
if (id) obj.id = id; if (id) obj.id = id;
if (subtype) obj.subtype = subtype; if (subtype) obj.subtype = subtype;
if (version) obj.version = version;
if (index) obj.index = index; if (index) obj.index = index;
if (version) obj.version = version;
return obj; return obj;
}; };
@ -48,13 +48,13 @@ exports.stringify = function(obj) {
if (obj.version || obj.index) parts.push(''); if (obj.version || obj.index) parts.push('');
} }
if (typeof obj.version !== 'undefined') { if (typeof obj.index !== 'undefined') {
parts.push(obj.version); parts.push(obj.index);
} else { } else {
if (obj.index) parts.push(''); if (obj.version) parts.push('');
} }
if (typeof obj.index !== 'undefined') parts.push(obj.index); if (typeof obj.version !== 'undefined') parts.push(obj.version);
return parts.join('/'); return parts.join('/');
}; };
@ -62,7 +62,7 @@ exports.stringify = function(obj) {
exports.merge = function(str, objToMerge) { exports.merge = function(str, objToMerge) {
var obj = exports.parse(str); var obj = exports.parse(str);
for (var prop in objToMerge) { for (var prop in objToMerge) {
if (['type', 'id', 'subtype', 'version', 'index'].indexOf(prop) > -1) { if (['type', 'id', 'subtype', 'index', 'version'].indexOf(prop) > -1) {
obj[prop] = objToMerge[prop]; obj[prop] = objToMerge[prop];
} }
} }

@ -1,6 +1,6 @@
{ {
"name": "docuri", "name": "docuri",
"version": "1.1.0", "version": "2.0.0",
"description": "Rich document ids for CouchDB", "description": "Rich document ids for CouchDB",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

@ -4,38 +4,38 @@ var test = require('tap').test;
var merge = require('..').merge; var merge = require('..').merge;
test('changing type component', function(t) { test('changing type component', function(t) {
t.equal(merge('type/id/subtype/version/index', {type:'new_type'}), 'new_type/id/subtype/version/index', 'should return docuri string with type changed'); t.equal(merge('type/id/subtype/index/version', {type:'new_type'}), 'new_type/id/subtype/index/version', 'should return docuri string with type changed');
t.end(); t.end();
}); });
test('changing id component', function(t) { test('changing id component', function(t) {
t.equal(merge('type/id/subtype/version/index', {id:'new_id'}), 'type/new_id/subtype/version/index', 'should return docuri string with id changed'); t.equal(merge('type/id/subtype/index/version', {id:'new_id'}), 'type/new_id/subtype/index/version', 'should return docuri string with id changed');
t.end(); t.end();
}); });
test('changing subtype component', function(t) { test('changing subtype component', function(t) {
t.equal(merge('type/id/subtype/version/index', {subtype:'new_subtype'}), 'type/id/new_subtype/version/index', 'should return docuri string with subtype changed'); t.equal(merge('type/id/subtype/index/version', {subtype:'new_subtype'}), 'type/id/new_subtype/index/version', 'should return docuri string with subtype changed');
t.end(); t.end();
}); });
test('changing version component', function(t) { test('changing index component', function(t) {
t.equal(merge('type/id/subtype/version/index', {version:'new_version'}), 'type/id/subtype/new_version/index', 'should return docuri string with version changed'); t.equal(merge('type/id/subtype/index/version', {index:'new_index'}), 'type/id/subtype/new_index/version', 'should return docuri string with index changed');
t.end(); t.end();
}); });
test('changing index component', function(t) { test('changing version component', function(t) {
t.equal(merge('type/id/subtype/version/index', {index:'new_index'}), 'type/id/subtype/version/new_index', 'should return docuri string with index changed'); t.equal(merge('type/id/subtype/index/version', {version:'new_version'}), 'type/id/subtype/index/new_version', 'should return docuri string with version changed');
t.end(); t.end();
}); });
test('changing unknown component', function(t) { test('changing unknown component', function(t) {
t.equal(merge('type/id/subtype/version/index', {unknown:'i dont know'}), 'type/id/subtype/version/index', 'should return unchanged docuri'); t.equal(merge('type/id/subtype/index/version', {unknown:'i dont know'}), 'type/id/subtype/index/version', 'should return unchanged docuri');
t.end(); t.end();
}); });
test('missing second argument', function(t) { test('missing second argument', function(t) {
t.equal(merge('type/id/subtype/version/index'), 'type/id/subtype/version/index', 'should return unchanged docuri string'); t.equal(merge('type/id/subtype/index/version'), 'type/id/subtype/index/version', 'should return unchanged docuri string');
t.end(); t.end();
}); });

@ -33,23 +33,23 @@ test('type/id/subtype', function(t) {
t.end(); t.end();
}); });
test('type/id/subtype/version', function(t) { test('type/id/subtype/index', function(t) {
t.deepEqual(parse('mytype/myid/mysubtype/myversion'), { t.deepEqual(parse('mytype/myid/mysubtype/myindex'), {
type: 'mytype', type: 'mytype',
id: 'myid', id: 'myid',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion' index: 'myindex'
}, 'should return object with type, id, subtype and version'); }, 'should return object with type, id, subtype and index');
t.end(); t.end();
}); });
test('type/id/subtype/version/index', function(t) { test('type/id/subtype/index/version', function(t) {
t.deepEqual(parse('mytype/myid/mysubtype/myversion/myindex'), { t.deepEqual(parse('mytype/myid/mysubtype/myindex/myversion'), {
type: 'mytype', type: 'mytype',
id: 'myid', id: 'myid',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion', index: 'myindex',
index: 'myindex' version: 'myversion'
}, 'should return object with type, id, subtype, version and index'); }, 'should return object with type, id, subtype, index and version');
t.end(); t.end();
}); });

@ -58,73 +58,73 @@ test('type/id/subtype', function(t) {
}); });
test('/id/subtype/version', function(t) { test('/id/subtype/index', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
id: 'myid', id: 'myid',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion' index: 'myindex'
}), '/myid/mysubtype/myversion', 'should return string with type, id, subtype and version'); }), '/myid/mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end(); t.end();
}); });
test('type//subtype/version', function(t) { test('type//subtype/index', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
type: 'mytype', type: 'mytype',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion' index: 'myindex'
}), 'mytype//mysubtype/myversion', 'should return string with type, id, subtype and version'); }), 'mytype//mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end(); t.end();
}); });
test('type///version', function(t) { test('type///index', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
type: 'mytype', type: 'mytype',
version: 'myversion' index: 'myindex'
}), 'mytype///myversion', 'should return string with type, id, subtype and version'); }), 'mytype///myindex', 'should return string with type, id, subtype and index');
t.end(); t.end();
}); });
test('//subtype/version', function(t) { test('//subtype/index', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion' index: 'myindex'
}), '//mysubtype/myversion', 'should return string with type, id, subtype and version'); }), '//mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end(); t.end();
}); });
test('///version', function(t) { test('///index', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
version: 'myversion' index: 'myindex'
}), '///myversion', 'should return string with type, id, subtype and version'); }), '///myindex', 'should return string with type, id, subtype and index');
t.end(); t.end();
}); });
test('type/id/subtype/version', function(t) { test('type/id/subtype/index', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
type: 'mytype', type: 'mytype',
id: 'myid', id: 'myid',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion' index: 'myindex'
}), 'mytype/myid/mysubtype/myversion', 'should return string with type, id, subtype and version'); }), 'mytype/myid/mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end(); t.end();
}); });
test('////index', function(t) { test('////version', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
index: 'myindex' version: 'myversion'
}), '////myindex', 'should return string with type, id, subtype, version and index'); }), '////myversion', 'should return string with type, id, subtype, index and version');
t.end(); t.end();
}); });
test('type/id/subtype/version/index', function(t) { test('type/id/subtype/index/version', function(t) {
t.deepEqual(stringify({ t.deepEqual(stringify({
type: 'mytype', type: 'mytype',
id: 'myid', id: 'myid',
subtype: 'mysubtype', subtype: 'mysubtype',
version: 'myversion', index: 'myindex',
index: 'myindex' version: 'myversion'
}), 'mytype/myid/mysubtype/myversion/myindex', 'should return string with type, id, subtype, version and index'); }), 'mytype/myid/mysubtype/myindex/myversion', 'should return string with type, id, subtype, index and version');
t.end(); t.end();
}); });
@ -133,8 +133,8 @@ test('0/1/2/3/4', function(t) {
type: 0, type: 0,
id: 1, id: 1,
subtype: 2, subtype: 2,
version: 3, index: 3,
index: 4 version: 4
}), '0/1/2/3/4', 'should correctly handle number path fields'); }), '0/1/2/3/4', 'should correctly handle number path fields');
t.end(); t.end();
}); });

Loading…
Cancel
Save