From c584f201fe94cdd8c76ada7c894a409a2ae81094 Mon Sep 17 00:00:00 2001 From: Karsten Gebbert Date: Wed, 16 Apr 2014 15:10:07 +0200 Subject: [PATCH] allow 0 as path element (closes #2) --- index.js | 10 +++++----- test/stringify_test.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 125da02..2103cbb 100644 --- a/index.js +++ b/index.js @@ -30,31 +30,31 @@ exports.stringify = function(obj) { obj = obj || {}; var parts = []; - if (obj.type) { + if (typeof obj.type !== 'undefined') { parts.push(obj.type); } else { if (obj.id || obj.subtype || obj.version || obj.index) parts.push(''); } - if (obj.id) { + if (typeof obj.id !== 'undefined') { parts.push(obj.id); } else { if (obj.subtype || obj.version || obj.index) parts.push(''); } - if (obj.subtype) { + if (typeof obj.subtype !== 'undefined') { parts.push(obj.subtype); } else { if (obj.version || obj.index) parts.push(''); } - if (obj.version) { + if (typeof obj.version !== 'undefined') { parts.push(obj.version); } else { if (obj.index) parts.push(''); } - if (obj.index) parts.push(obj.index); + if (typeof obj.index !== 'undefined') parts.push(obj.index); return parts.join('/'); }; diff --git a/test/stringify_test.js b/test/stringify_test.js index 4943ba5..fd700f6 100644 --- a/test/stringify_test.js +++ b/test/stringify_test.js @@ -127,3 +127,14 @@ test('type/id/subtype/version/index', function(t) { }), 'mytype/myid/mysubtype/myversion/myindex', 'should return string with type, id, subtype, version and index'); t.end(); }); + +test('0/1/2/3/4', function(t) { + t.deepEqual(stringify({ + type: 0, + id: 1, + subtype: 2, + version: 3, + index: 4 + }), '0/1/2/3/4', 'should correctly handle number path fields'); + t.end(); +});