From 55f7a6715ac23735f6c9ffbed6fc0485443f52b4 Mon Sep 17 00:00:00 2001 From: "Johannes J. Schmidt" Date: Wed, 16 Apr 2014 11:15:07 +0200 Subject: [PATCH] first commit --- .gitignore | 1 + README.md | 48 +++++++++++++++ index.js | 60 +++++++++++++++++++ package.json | 27 +++++++++ test/parse_test.js | 55 ++++++++++++++++++ test/stringify_test.js | 129 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 320 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 test/parse_test.js create mode 100644 test/stringify_test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..9c62781 --- /dev/null +++ b/README.md @@ -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. + diff --git a/index.js b/index.js new file mode 100644 index 0000000..125da02 --- /dev/null +++ b/index.js @@ -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('/'); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..f3a85e6 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test/parse_test.js b/test/parse_test.js new file mode 100644 index 0000000..638056e --- /dev/null +++ b/test/parse_test.js @@ -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(); +}); diff --git a/test/stringify_test.js b/test/stringify_test.js new file mode 100644 index 0000000..4943ba5 --- /dev/null +++ b/test/stringify_test.js @@ -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(); +});