dgeni-packages
Version:
A collection of dgeni packages for generating documentation from source code
74 lines (60 loc) • 1.88 kB
JavaScript
function parseCodeName(codeName) {
const parts = [];
let currentPart;
codeName.split('.').forEach(part => {
const subParts = part.split(':');
const name = subParts.pop();
const modifier = subParts.pop();
if ( !modifier && currentPart ) {
currentPart.name += '.' + name;
} else {
currentPart = {
name: name,
modifier: modifier
};
parts.push(currentPart);
}
});
return parts;
}
/**
* @dgService getAliases
* @description
* Get a list of all the aliases that can be made from the doc
* @param {Object} doc A doc from which to extract aliases
* @return {Array} A collection of aliases
*/
module.exports = function getAliases() {
return (doc) => {
const codeNameParts = parseCodeName(doc.id);
let methodName;
let aliases = [];
// Add the last part to the list of aliases
const part = codeNameParts.pop();
// If the name contains a # then it is a member and that should be included in the aliases
if ( part.name.indexOf('#') !== -1 ) {
methodName = part.name.split('#')[1];
}
// Add the part name and modifier, if provided
aliases.push(part.name);
if (part.modifier) {
aliases.push(part.modifier + ':' + part.name);
}
// Continue popping off the parts of the codeName and work forward collecting up each alias
aliases = codeNameParts.reduceRight((aliases, part) => {
// Add this part to each of the aliases we have so far
aliases.forEach(name => {
// Add the part name and modifier, if provided
aliases.push(part.name + '.' + name);
if ( part.modifier ) {
aliases.push(part.modifier + ':' + part.name + '.' + name);
}
});
return aliases;
}, aliases);
if ( methodName ) {
aliases.push(methodName);
}
return aliases;
};
};