mirror of https://github.com/jo/docuri.git
rewrite
parent
461c7ee0db
commit
ed9f5886e8
@ -1,70 +1,44 @@
|
|||||||
/*
|
/*
|
||||||
* docuri
|
* docuri: Rich document ids for CouchDB.
|
||||||
*
|
*
|
||||||
* Copyright (c) 2014 null2 GmbH Berlin
|
* Copyright (c) 2014 null2 GmbH Berlin
|
||||||
* Licensed under the MIT license.
|
* Licensed under the MIT license.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exports.parse = function(str) {
|
// type/id/subtype/index/version
|
||||||
str = str || '';
|
var PARTS = ['type', 'id', 'subtype', 'index', 'version'];
|
||||||
|
|
||||||
var obj = {};
|
|
||||||
var parts = str.split('/');
|
|
||||||
|
|
||||||
var type = parts.shift();
|
exports.parse = function(str) {
|
||||||
var id = parts.shift();
|
str = str || '';
|
||||||
var subtype = parts.shift();
|
|
||||||
var index = parts.shift();
|
|
||||||
var version = parts.shift();
|
|
||||||
|
|
||||||
if (type) obj.type = type;
|
return str.split('/').reduce(function(obj, value, i) {
|
||||||
if (id) obj.id = id;
|
if (value) {
|
||||||
if (subtype) obj.subtype = subtype;
|
obj[PARTS[i]] = value;
|
||||||
if (index) obj.index = index;
|
}
|
||||||
if (version) obj.version = version;
|
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
|
}, {});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.stringify = function(obj) {
|
exports.stringify = function(obj) {
|
||||||
obj = obj || {};
|
obj = obj || {};
|
||||||
|
|
||||||
var parts = [];
|
return PARTS.map(function(part) {
|
||||||
if (typeof obj.type !== 'undefined') {
|
return typeof obj[part] === 'undefined' ? '' : obj[part];
|
||||||
parts.push(obj.type);
|
}).join('/').replace(/\/+$/, '');
|
||||||
} else {
|
|
||||||
if (obj.id || obj.subtype || obj.version || obj.index) parts.push('');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof obj.id !== 'undefined') {
|
|
||||||
parts.push(obj.id);
|
|
||||||
} else {
|
|
||||||
if (obj.subtype || obj.version || obj.index) parts.push('');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof obj.subtype !== 'undefined') {
|
|
||||||
parts.push(obj.subtype);
|
|
||||||
} else {
|
|
||||||
if (obj.version || obj.index) parts.push('');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof obj.index !== 'undefined') {
|
|
||||||
parts.push(obj.index);
|
|
||||||
} else {
|
|
||||||
if (obj.version) parts.push('');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof obj.version !== 'undefined') parts.push(obj.version);
|
|
||||||
|
|
||||||
return parts.join('/');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.merge = function(str, objToMerge) {
|
exports.merge = function(str, objToMerge) {
|
||||||
|
objToMerge = objToMerge || {};
|
||||||
|
|
||||||
var obj = exports.parse(str);
|
var obj = exports.parse(str);
|
||||||
for (var prop in objToMerge) {
|
|
||||||
if (['type', 'id', 'subtype', 'index', 'version'].indexOf(prop) > -1) {
|
PARTS.forEach(function(part) {
|
||||||
obj[prop] = objToMerge[prop];
|
if (objToMerge[part]) {
|
||||||
|
obj[part] = objToMerge[part];
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
return exports.stringify(obj);
|
return exports.stringify(obj);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue