From 6c88aa23725c8618a6c32f323ea449f0ff8629c3 Mon Sep 17 00:00:00 2001 From: "Johannes J. Schmidt" Date: Wed, 28 May 2014 20:08:37 +0200 Subject: [PATCH] configurable parts --- README.md | 44 ++++++++++++++++++++++++++++++++++---------- index.js | 24 +++++++++++++++++++----- package.json | 8 +++----- test/merge_test.js | 2 -- test/parts_test.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 test/parts_test.js diff --git a/README.md b/README.md index 8dea608..01cc658 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index d3d5ec1..e6a8b75 100644 --- a/index.js +++ b/index.js @@ -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; + diff --git a/package.json b/package.json index 0cfe502..1a43555 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/merge_test.js b/test/merge_test.js index df3bb6e..989522e 100644 --- a/test/merge_test.js +++ b/test/merge_test.js @@ -1,5 +1,3 @@ -'use strict'; - var test = require('tap').test; var merge = require('..').merge; diff --git a/test/parts_test.js b/test/parts_test.js new file mode 100644 index 0000000..8badff8 --- /dev/null +++ b/test/parts_test.js @@ -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(); +});