mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-23 09:20:03 +02:00
1119 Support method return types
Small refactor to split out logic for determining method display text and style. Updated documentation Used regex to parse method statements in class diagrams to extract discrete elements to set display appropriately. Added tests and updated docs
This commit is contained in:
@@ -105,7 +105,7 @@ describe('class diagram, ', function () {
|
||||
parser.parse(str);
|
||||
});
|
||||
|
||||
it('should handle parsing of method statements grouped by brackets', function () {
|
||||
it('should handle parsing of method statements grouped by brackets', function () {
|
||||
const str =
|
||||
'classDiagram\n' +
|
||||
'class Dummy_Class {\n' +
|
||||
@@ -121,6 +121,36 @@ describe('class diagram, ', function () {
|
||||
parser.parse(str);
|
||||
});
|
||||
|
||||
it('should handle return types on methods', function () {
|
||||
const str =
|
||||
'classDiagram\n' +
|
||||
'Object <|-- ArrayList\n' +
|
||||
'Object : equals()\n' +
|
||||
'Object : -Object[] objects\n' +
|
||||
'Object : +getObjects() Object[]\n' +
|
||||
'ArrayList : Dummy elementData\n' +
|
||||
'ArrayList : getDummy() Dummy';
|
||||
|
||||
parser.parse(str);
|
||||
});
|
||||
|
||||
it('should handle return types on methods grouped by brackets', function () {
|
||||
const str =
|
||||
'classDiagram\n' +
|
||||
'class Dummy_Class {\n' +
|
||||
'string data\n' +
|
||||
'getDummy() Dummy\n' +
|
||||
'}\n' +
|
||||
'\n' +
|
||||
'class Flight {\n' +
|
||||
' int flightNumber\n' +
|
||||
' datetime departureTime\n' +
|
||||
' getDepartureTime() datetime\n' +
|
||||
'}';
|
||||
|
||||
parser.parse(str);
|
||||
});
|
||||
|
||||
it('should handle parsing of separators', function () {
|
||||
const str =
|
||||
'classDiagram\n' +
|
||||
|
@@ -314,11 +314,24 @@ const drawClass = function(elem, classDef) {
|
||||
|
||||
const id = classDef.id;
|
||||
const buildDisplayTextForMethod = function(txt) {
|
||||
let regEx = /(\+|-|~|#)?(\w+)\((\w+\[?\]?)?\s?(\w+)?\)([*|$])?\s?(\w+\[?\]?)?/;
|
||||
let cssStyle = '';
|
||||
let methodEnd = txt.indexOf(')') + 1;
|
||||
let methodName = txt.substring(0, methodEnd);
|
||||
let displayText = txt;
|
||||
let methodName = txt;
|
||||
let classifier = '';
|
||||
|
||||
let classifier = txt.substring(methodEnd, methodEnd + 1);
|
||||
let parsedText = txt.match(regEx);
|
||||
|
||||
if (parsedText) {
|
||||
let visibility = parsedText[1] ? parsedText[1] : '';
|
||||
methodName = parsedText[2] ? parsedText[2] : '';
|
||||
let parameterType = parsedText[3] ? parsedText[3] : '';
|
||||
let parameterName = parsedText[4] ? parsedText[4] : '';
|
||||
classifier = parsedText[5] ? parsedText[5] : '';
|
||||
let returnType = parsedText[6] ? ' : ' + parsedText[6] : '';
|
||||
displayText =
|
||||
visibility + methodName + '(' + parameterType + ' ' + parameterName + ')' + returnType;
|
||||
}
|
||||
|
||||
switch (classifier) {
|
||||
case '*':
|
||||
@@ -331,19 +344,10 @@ const drawClass = function(elem, classDef) {
|
||||
|
||||
let method = {
|
||||
methodname: methodName,
|
||||
displayText: methodName,
|
||||
displayText: displayText,
|
||||
cssStyle: cssStyle
|
||||
};
|
||||
|
||||
let returnTypeStart = txt.indexOf('[') + 1;
|
||||
let returnTypeEnd = txt.indexOf(']');
|
||||
|
||||
if (returnTypeStart > 1 && returnTypeEnd > returnTypeStart) {
|
||||
let returnType = txt.substring(returnTypeStart, returnTypeEnd);
|
||||
|
||||
method.displayText = methodName + ' : ' + returnType;
|
||||
}
|
||||
|
||||
return method;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user