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

61 lines
1.2 KiB
JavaScript

10 years ago
/*
* docuri
*
* Copyright (c) 2014 null2 GmbH Berlin
* Licensed under the MIT license.
*/
exports.parse = function(str) {
str = str || '';
var obj = {};
var parts = str.split('/');
var type = parts.shift();
var id = parts.shift();
var subtype = parts.shift();
var version = parts.shift();
var index = parts.shift();
if (type) obj.type = type;
if (id) obj.id = id;
if (subtype) obj.subtype = subtype;
if (version) obj.version = version;
if (index) obj.index = index;
return obj;
};
exports.stringify = function(obj) {
obj = obj || {};
var parts = [];
if (typeof obj.type !== 'undefined') {
10 years ago
parts.push(obj.type);
} else {
if (obj.id || obj.subtype || obj.version || obj.index) parts.push('');
}
if (typeof obj.id !== 'undefined') {
10 years ago
parts.push(obj.id);
} else {
if (obj.subtype || obj.version || obj.index) parts.push('');
}
if (typeof obj.subtype !== 'undefined') {
10 years ago
parts.push(obj.subtype);
} else {
if (obj.version || obj.index) parts.push('');
}
if (typeof obj.version !== 'undefined') {
10 years ago
parts.push(obj.version);
} else {
if (obj.index) parts.push('');
}
if (typeof obj.index !== 'undefined') parts.push(obj.index);
10 years ago
return parts.join('/');
};