mermaid/src/utils.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-03-09 06:33:35 +01:00
import * as d3 from 'd3'
/**
* @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
*/
2017-09-09 08:46:58 +02:00
export const detectType = function (text) {
2017-04-11 16:14:25 +02:00
text = text.replace(/^\s*%%.*\n/g, '\n')
if (text.match(/^\s*sequenceDiagram/)) {
2018-03-12 14:16:22 +01:00
return 'sequence'
2017-04-11 16:14:25 +02:00
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*gantt/)) {
return 'gantt'
}
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*classDiagram/)) {
2018-03-12 14:16:22 +01:00
return 'class'
2017-04-11 16:14:25 +02:00
}
2015-10-30 10:47:25 +01:00
2017-04-11 16:14:25 +02:00
if (text.match(/^\s*gitGraph/)) {
2018-03-12 14:16:22 +01:00
return 'git'
2017-04-11 16:14:25 +02:00
}
2018-03-12 14:06:49 +01:00
return 'flowchart'
2017-04-11 16:14:25 +02:00
}
/**
* @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
**/
2017-09-09 08:46:58 +02:00
export const isSubstringInArray = function (str, arr) {
for (let i = 0; i < arr.length; i++) {
2017-04-11 16:14:25 +02:00
if (arr[i].match(str)) return i
}
2017-04-11 16:14:25 +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) {
return defaultCurve
}
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
2017-09-10 13:41:34 +02:00
}