diff --git a/README.md b/README.md index 565aa30..8dea608 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 4a56b9a..ad7a0d6 100644 --- a/index.js +++ b/index.js @@ -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]; } } diff --git a/package.json b/package.json index a27c6b3..b05ed61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docuri", - "version": "1.1.0", + "version": "2.0.0", "description": "Rich document ids for CouchDB", "main": "index.js", "scripts": { diff --git a/test/merge_test.js b/test/merge_test.js index 931bf4b..df3bb6e 100644 --- a/test/merge_test.js +++ b/test/merge_test.js @@ -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(); }); diff --git a/test/parse_test.js b/test/parse_test.js index 638056e..959c9cc 100644 --- a/test/parse_test.js +++ b/test/parse_test.js @@ -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(); }); diff --git a/test/stringify_test.js b/test/stringify_test.js index fd700f6..364a6c4 100644 --- a/test/stringify_test.js +++ b/test/stringify_test.js @@ -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(); });