mirror of
https://github.com/bower/bower.git
synced 2026-01-23 05:07:55 -05:00
53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
var semver = require('semver');
|
|
var createError = require('./createError');
|
|
|
|
function decompose(endpoint) {
|
|
var regExp = /^(?:([\w\-]|(?:[\w\.\-]+[\w\-])?)=)?([^\|#]+)(?:#(.*))?$/;
|
|
var matches = endpoint.match(regExp);
|
|
var target;
|
|
|
|
if (!matches) {
|
|
throw createError('Invalid endpoint: ' + endpoint, 'EINVEND');
|
|
}
|
|
|
|
target = matches[3];
|
|
|
|
return {
|
|
name: matches[1] || '',
|
|
source: matches[2],
|
|
target: !target || target === 'latest' ? '*' : target
|
|
};
|
|
}
|
|
|
|
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 (value === 'latest' || 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;
|