Files
bower/lib/util/endpointParser.js
André Cruz 4c3802878a Another huge commit.
- Add info command
- Fix list --paths
- Fix search with no name
- Fix conflict detection when uninstalling
- Mix tweaks and fixes
2013-06-23 01:14:08 +01:00

50 lines
1.1 KiB
JavaScript

var semver = require('semver');
var createError = require('./createError');
function decompose(endpoint) {
var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/;
var matches = endpoint.match(regExp);
if (!matches) {
throw createError('Invalid endpoint: ' + endpoint, 'EINVEND');
}
return {
name: matches[1] || '',
source: matches[2],
target: matches[3] || '*'
};
}
function compose(decEndpoint) {
var composed = '';
if (decEndpoint.name) {
composed += decEndpoint.name + '=';
}
composed += decEndpoint.source;
if (decEndpoint.target) {
composed += '#' + decEndpoint.target;
}
return composed;
}
function json2decomposed(key, value) {
var endpoint = key + '=';
if (semver.valid(value) != null || semver.validRange(value) != null) {
endpoint += key + '#' + value;
} else {
endpoint += value;
}
return decompose(endpoint);
}
module.exports.decompose = decompose;
module.exports.compose = compose;
module.exports.json2decomposed = json2decomposed;