added merge ability

pull/5/head
andi pieper 10 years ago
parent b6679d376c
commit 958edbc693

@ -58,3 +58,13 @@ exports.stringify = function(obj) {
return parts.join('/');
};
exports.merge = function(str, objToMerge) {
var obj = exports.parse(str);
for (var prop in objToMerge) {
if (['type', 'id', 'subtype', 'version', 'index'].indexOf(prop) > -1) {
obj[prop] = objToMerge[prop];
}
}
return exports.stringify(obj);
};

@ -0,0 +1,42 @@
'use strict';
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.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.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.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');
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');
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.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.end();
});
Loading…
Cancel
Save