mermaid/src/utils.js

81 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-09-12 21:58:57 +02:00
import * as d3 from 'd3';
import { logger } from './logger';
/**
* @function detectType
* Detects the type of the graph text.
* ```mermaid
* graph LR
* a-->b
* b-->c
* c-->d
* d-->e
* e-->f
* f-->g
* g-->h
* ```
*
* @param {string} text The text defining the graph
* @returns {string} A graph definition key
*/
2019-09-12 21:58:57 +02:00
export const detectType = function(text) {
text = text.replace(/^\s*%%.*\n/g, '\n');
logger.debug('Detecting diagram type based on the text ' + text);
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*sequenceDiagram/)) {
2019-09-12 21:58:57 +02:00
return 'sequence';
2017-04-11 16:14:25 +02:00
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*gantt/)) {
2019-09-12 21:58:57 +02:00
return 'gantt';
2017-04-11 16:14:25 +02:00
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*classDiagram/)) {
2019-09-12 21:58:57 +02:00
return 'class';
2017-04-11 16:14:25 +02:00
}
2015-10-30 10:47:25 +01:00
2019-09-25 21:01:21 +02:00
if (text.match(/^\s*stateDiagram/)) {
return 'state';
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*gitGraph/)) {
2019-09-12 21:58:57 +02:00
return 'git';
2017-04-11 16:14:25 +02:00
}
if (text.match(/^\s*info/)) {
2019-09-12 21:58:57 +02:00
return 'info';
}
2019-09-11 21:20:28 +02:00
if (text.match(/^\s*pie/)) {
2019-09-12 21:58:57 +02:00
return 'pie';
2019-09-11 21:20:28 +02:00
}
2019-09-12 21:58:57 +02:00
return 'flowchart';
};
/**
* @function isSubstringInArray
* Detects whether a substring in present in a given array
* @param {string} str The substring to detect
* @param {array} arr The array to search
* @returns {number} the array index containing the substring or -1 if not present
**/
2019-09-12 21:58:57 +02:00
export const isSubstringInArray = function(str, arr) {
2017-09-09 08:46:58 +02:00
for (let i = 0; i < arr.length; i++) {
2019-09-12 21:58:57 +02:00
if (arr[i].match(str)) return i;
}
2019-09-12 21:58:57 +02:00
return -1;
};
2017-09-10 13:41:34 +02:00
2018-03-09 06:33:35 +01:00
export const interpolateToCurve = (interpolate, defaultCurve) => {
2018-03-18 02:35:28 +01:00
if (!interpolate) {
2019-09-12 21:58:57 +02:00
return defaultCurve;
2018-03-18 02:35:28 +01:00
}
2019-09-12 21:58:57 +02:00
const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`;
return d3[curveName] || defaultCurve;
};
2018-03-09 06:33:35 +01:00
2017-09-10 13:41:34 +02:00
export default {
detectType,
2018-03-09 06:33:35 +01:00
isSubstringInArray,
interpolateToCurve
2019-09-12 21:58:57 +02:00
};