You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
docuri/index.js

59 lines
986 B
JavaScript

10 years ago
/*
10 years ago
* docuri: Rich document ids for CouchDB.
10 years ago
*
* Copyright (c) 2014 null2 GmbH Berlin
* Licensed under the MIT license.
*/
10 years ago
// type/id/subtype/index/version
var PARTS = ['type', 'id', 'subtype', 'index', 'version'];
10 years ago
function docuri(parts) {
if (parts) {
PARTS = parts;
10 years ago
return docuri;
}
return PARTS;
}
docuri.parse = function(str) {
10 years ago
str = str || '';
10 years ago
10 years ago
return str.split('/').reduce(function(obj, value, i) {
if (value) {
obj[PARTS[i]] = value;
}
10 years ago
10 years ago
return obj;
}, {});
10 years ago
};
docuri.stringify = function(obj) {
10 years ago
obj = obj || {};
10 years ago
return PARTS.map(function(part) {
return typeof obj[part] === 'undefined' ? '' : obj[part];
}).join('/').replace(/\/+$/, '');
10 years ago
};
docuri.merge = function(str, objToMerge) {
10 years ago
objToMerge = objToMerge || {};
var obj = docuri.parse(str);
10 years ago
PARTS.forEach(function(part) {
if (objToMerge[part]) {
obj[part] = objToMerge[part];
}
10 years ago
});
return docuri.stringify(obj);
};
module.exports = docuri;