From ac4af0f98051df23023320469b9e40b1b6d832a2 Mon Sep 17 00:00:00 2001 From: "Johannes J. Schmidt" Date: Mon, 2 Jun 2014 15:00:53 +0200 Subject: [PATCH] (fixes #8) - Make it work as CouchDB CommonJS Do not export a function. `docuri()` is now called `docuri.definition()`. --- README.md | 16 ++++++++-------- index.js | 5 ++++- package.json | 2 +- test/config_test.js | 28 ---------------------------- test/definition_test.js | 28 ++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 38 deletions(-) delete mode 100644 test/config_test.js create mode 100644 test/definition_test.js diff --git a/README.md b/README.md index 87bceb0..235468d 100644 --- a/README.md +++ b/README.md @@ -74,21 +74,21 @@ docuri.arity({ type: 'mytype', subtype: 'mysubtype' }); // 3 ``` -### `docuri([definition])` -Use custom definition: +### `docuri.definition([array])` +Access or use custom definition: ```js -docuri(); +docuri.definition(); // ['type', 'id', 'subtype', 'index', 'version'] -docuri(['id', 'meta']); -docuri(); -// ['id', 'meta'] -docuri.parse('42/answer'); +docuri + .definition(['id', 'meta']) + .parse('42/answer'); // { // id: '42', // meta: 'answer' // } ``` -Note: `definition` MUST be an array of strings with at least one item. +Note: the (optional) argument to `docuri.definition` MUST be an array of strings +containing at least one item. ## Browser support diff --git a/index.js b/index.js index 67f29ae..b808a1d 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,11 @@ // type/id/subtype/index/version var DEFINITION = ['type', 'id', 'subtype', 'index', 'version']; +var docuri; -function docuri(definition) { +module.exports = exports = docuri = {}; + +docuri.definition = function(definition) { if (definition) { DEFINITION = definition; diff --git a/package.json b/package.json index 9a9be9b..ab7a466 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docuri", - "version": "2.2.0", + "version": "3.0.0", "description": "Rich document ids for CouchDB", "main": "index.js", "scripts": { diff --git a/test/config_test.js b/test/config_test.js deleted file mode 100644 index 8badff8..0000000 --- a/test/config_test.js +++ /dev/null @@ -1,28 +0,0 @@ -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(); -}); diff --git a/test/definition_test.js b/test/definition_test.js new file mode 100644 index 0000000..f55269f --- /dev/null +++ b/test/definition_test.js @@ -0,0 +1,28 @@ +var test = require('tap').test; +var definition = require('..').definition; + +test('default configuration', function(t) { + t.deepEqual(definition(), ['type', 'id', 'subtype', 'index', 'version'], 'should return default parts'); + t.end(); +}); + +test('set configuration', function(t) { + var parts = ['my', 'parts']; + t.type(definition(parts).merge, 'function', 'should return docuri api: merge'); + t.type(definition(parts).parse, 'function', 'should return docuri api: parse'); + t.type(definition(parts).stringify, 'function', 'should return docuri api: stringify'); + t.end(); +}); + +test('change configuration', function(t) { + var parts = ['my', 'parts']; + definition(parts); + t.deepEqual(definition(), parts, 'should return custom parts'); + t.end(); +}); + +test('use changed configuration', function(t) { + var parts = ['my', 'parts']; + t.deepEqual(definition(parts).parse('one/two'), { my: 'one', parts: 'two'}, 'should use custom parts'); + t.end(); +});