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)
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
Parse id string:
```js
require('docuri').parse('mytype/myid/mysubtype/myversion/myindex');
require('docuri').parse('mytype/myid/mysubtype/myindex/myversion');
// {
// type: 'mytype',
// id: 'myid',
// subtype: 'mysubtype',
// version: 'myversion',
// index: 'myindex'
// index: 'myindex',
// version: 'myversion'
// }
```
@ -24,18 +24,18 @@ require('docuri').stringify({
type: 'mytype',
id: 'myid',
subtype: 'mysubtype',
version: 'myversion',
index: 'myindex'
index: 'myindex',
version: 'myversion'
});
// 'mytype/myid/mysubtype/myversion/myindex'
// 'mytype/myid/mysubtype/myindex/myversion'
```
Change id string components:
```js
require('docuri').merge('mytype/myid/mysubtype/myversion/myindex', {
require('docuri').merge('mytype/myid/mysubtype/myindex/myversion', {
type: 'my_new_type',
});
// 'my_new_type/myid/mysubtype/myversion/myindex'
// 'my_new_type/myid/mysubtype/myindex/myversion'
```
## Browser support

@ -14,14 +14,14 @@ exports.parse = function(str) {
var type = parts.shift();
var id = parts.shift();
var subtype = parts.shift();
var version = parts.shift();
var index = parts.shift();
var version = 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;
if (version) obj.version = version;
return obj;
};
@ -48,13 +48,13 @@ exports.stringify = function(obj) {
if (obj.version || obj.index) parts.push('');
}
if (typeof obj.version !== 'undefined') {
parts.push(obj.version);
if (typeof obj.index !== 'undefined') {
parts.push(obj.index);
} 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('/');
};
@ -62,7 +62,7 @@ exports.stringify = function(obj) {
exports.merge = function(str, objToMerge) {
var obj = exports.parse(str);
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];
}
}

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

@ -4,38 +4,38 @@ var test = require('tap').test;
var merge = require('..').merge;
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();
});
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();
});
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();
});
test('changing version 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');
test('changing index component', function(t) {
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();
});
test('changing index 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');
test('changing version component', function(t) {
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();
});
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();
});
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();
});

@ -33,23 +33,23 @@ test('type/id/subtype', function(t) {
t.end();
});
test('type/id/subtype/version', function(t) {
t.deepEqual(parse('mytype/myid/mysubtype/myversion'), {
test('type/id/subtype/index', function(t) {
t.deepEqual(parse('mytype/myid/mysubtype/myindex'), {
type: 'mytype',
id: 'myid',
subtype: 'mysubtype',
version: 'myversion'
}, 'should return object with type, id, subtype and version');
index: 'myindex'
}, 'should return object with type, id, subtype and index');
t.end();
});
test('type/id/subtype/version/index', function(t) {
t.deepEqual(parse('mytype/myid/mysubtype/myversion/myindex'), {
test('type/id/subtype/index/version', function(t) {
t.deepEqual(parse('mytype/myid/mysubtype/myindex/myversion'), {
type: 'mytype',
id: 'myid',
subtype: 'mysubtype',
version: 'myversion',
index: 'myindex'
}, 'should return object with type, id, subtype, version and index');
index: 'myindex',
version: 'myversion'
}, 'should return object with type, id, subtype, index and version');
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({
id: 'myid',
subtype: 'mysubtype',
version: 'myversion'
}), '/myid/mysubtype/myversion', 'should return string with type, id, subtype and version');
index: 'myindex'
}), '/myid/mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end();
});
test('type//subtype/version', function(t) {
test('type//subtype/index', function(t) {
t.deepEqual(stringify({
type: 'mytype',
subtype: 'mysubtype',
version: 'myversion'
}), 'mytype//mysubtype/myversion', 'should return string with type, id, subtype and version');
index: 'myindex'
}), 'mytype//mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end();
});
test('type///version', function(t) {
test('type///index', function(t) {
t.deepEqual(stringify({
type: 'mytype',
version: 'myversion'
}), 'mytype///myversion', 'should return string with type, id, subtype and version');
index: 'myindex'
}), 'mytype///myindex', 'should return string with type, id, subtype and index');
t.end();
});
test('//subtype/version', function(t) {
test('//subtype/index', function(t) {
t.deepEqual(stringify({
subtype: 'mysubtype',
version: 'myversion'
}), '//mysubtype/myversion', 'should return string with type, id, subtype and version');
index: 'myindex'
}), '//mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end();
});
test('///version', function(t) {
test('///index', function(t) {
t.deepEqual(stringify({
version: 'myversion'
}), '///myversion', 'should return string with type, id, subtype and version');
index: 'myindex'
}), '///myindex', 'should return string with type, id, subtype and index');
t.end();
});
test('type/id/subtype/version', function(t) {
test('type/id/subtype/index', 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');
index: 'myindex'
}), 'mytype/myid/mysubtype/myindex', 'should return string with type, id, subtype and index');
t.end();
});
test('////index', function(t) {
test('////version', function(t) {
t.deepEqual(stringify({
index: 'myindex'
}), '////myindex', 'should return string with type, id, subtype, version and index');
version: 'myversion'
}), '////myversion', 'should return string with type, id, subtype, index and version');
t.end();
});
test('type/id/subtype/version/index', function(t) {
test('type/id/subtype/index/version', 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');
index: 'myindex',
version: 'myversion'
}), 'mytype/myid/mysubtype/myindex/myversion', 'should return string with type, id, subtype, index and version');
t.end();
});
@ -133,8 +133,8 @@ test('0/1/2/3/4', function(t) {
type: 0,
id: 1,
subtype: 2,
version: 3,
index: 4
index: 3,
version: 4
}), '0/1/2/3/4', 'should correctly handle number path fields');
t.end();
});

Loading…
Cancel
Save