pref: change detectors from .match into .test

there is no need for matching, capturing results, and validating nullablity
just using `.test` would be enough and significantly faster

for more info, see https://stackoverflow.com/10940138/16476610
This commit is contained in:
Yokozuna59 2023-06-12 14:45:08 +03:00
parent b11c511c5c
commit e688138167
21 changed files with 26 additions and 33 deletions

View File

@ -3,7 +3,7 @@ import type { ExternalDiagramDefinition } from 'mermaid';
const id = 'example-diagram'; const id = 'example-diagram';
const detector = (txt: string) => { const detector = (txt: string) => {
return txt.match(/^\s*example-diagram/) !== null; return /^\s*example-diagram/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -1,10 +1,9 @@
import type { ExternalDiagramDefinition } from 'mermaid'; import type { ExternalDiagramDefinition } from 'mermaid';
const id = 'zenuml'; const id = 'zenuml';
const regexp = /^\s*zenuml/;
const detector = (txt: string) => { const detector = (txt: string) => {
return txt.match(regexp) !== null; return /^\s*zenuml/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { ExternalDiagramDefinition } from '../../diagram-api/types.js';
const id = 'c4'; const id = 'c4';
const detector = (txt: string) => { const detector = (txt: string) => {
return txt.match(/^\s*C4Context|C4Container|C4Component|C4Dynamic|C4Deployment/) !== null; return /^\s*C4Context|C4Container|C4Component|C4Dynamic|C4Deployment/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -4,14 +4,11 @@ const id = 'classDiagram';
const detector: DiagramDetector = (txt, config) => { const detector: DiagramDetector = (txt, config) => {
// If we have configured to use dagre-wrapper then we should return true in this function for classDiagram code thus making it use the new class diagram // If we have configured to use dagre-wrapper then we should return true in this function for classDiagram code thus making it use the new class diagram
if ( if (/^\s*classDiagram/.test(txt) && config?.class?.defaultRenderer === 'dagre-wrapper') {
txt.match(/^\s*classDiagram/) !== null &&
config?.class?.defaultRenderer === 'dagre-wrapper'
) {
return true; return true;
} }
// We have not opted to use the new renderer so we should return true if we detect a class diagram // We have not opted to use the new renderer so we should return true if we detect a class diagram
return txt.match(/^\s*classDiagram-v2/) !== null; return /^\s*classDiagram-v2/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -8,7 +8,7 @@ const detector: DiagramDetector = (txt, config) => {
return false; return false;
} }
// We have not opted to use the new renderer so we should return true if we detect a class diagram // We have not opted to use the new renderer so we should return true if we detect a class diagram
return txt.match(/^\s*classDiagram/) !== null; return /^\s*classDiagram/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'er'; const id = 'er';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*erDiagram/) !== null; return /^\s*erDiagram/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -6,9 +6,9 @@ const id = 'flowchart-elk';
const detector: DiagramDetector = (txt: string, config?: MermaidConfig): boolean => { const detector: DiagramDetector = (txt: string, config?: MermaidConfig): boolean => {
if ( if (
// If diagram explicitly states flowchart-elk // If diagram explicitly states flowchart-elk
txt.match(/^\s*flowchart-elk/) || /^\s*flowchart-elk/.test(txt) ||
// If a flowchart/graph diagram has their default renderer set to elk // If a flowchart/graph diagram has their default renderer set to elk
(txt.match(/^\s*flowchart|graph/) && config?.flowchart?.defaultRenderer === 'elk') (/^\s*flowchart|graph/.test(txt) && config?.flowchart?.defaultRenderer === 'elk')
) { ) {
return true; return true;
} }

View File

@ -12,10 +12,10 @@ const detector: DiagramDetector = (txt, config) => {
} }
// If we have configured to use dagre-wrapper then we should return true in this function for graph code thus making it use the new flowchart diagram // If we have configured to use dagre-wrapper then we should return true in this function for graph code thus making it use the new flowchart diagram
if (txt.match(/^\s*graph/) !== null && config?.flowchart?.defaultRenderer === 'dagre-wrapper') { if (/^\s*graph/.test(txt) && config?.flowchart?.defaultRenderer === 'dagre-wrapper') {
return true; return true;
} }
return txt.match(/^\s*flowchart/) !== null; return /^\s*flowchart/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -11,7 +11,7 @@ const detector: DiagramDetector = (txt, config) => {
) { ) {
return false; return false;
} }
return txt.match(/^\s*graph/) !== null; return /^\s*graph/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'gantt'; const id = 'gantt';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*gantt/) !== null; return /^\s*gantt/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -4,7 +4,7 @@ import type { ExternalDiagramDefinition } from '../../diagram-api/types.js';
const id = 'gitGraph'; const id = 'gitGraph';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*gitGraph/) !== null; return /^\s*gitGraph/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'info'; const id = 'info';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*info/) !== null; return /^\s*info/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -2,7 +2,7 @@ import type { ExternalDiagramDefinition } from '../../diagram-api/types.js';
const id = 'mindmap'; const id = 'mindmap';
const detector = (txt: string) => { const detector = (txt: string) => {
return txt.match(/^\s*mindmap/) !== null; return /^\s*mindmap/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'pie'; const id = 'pie';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*pie/) !== null; return /^\s*pie/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'quadrantChart'; const id = 'quadrantChart';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*quadrantChart/) !== null; return /^\s*quadrantChart/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'requirement'; const id = 'requirement';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*requirement(Diagram)?/) !== null; return /^\s*requirement(Diagram)?/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'sequence'; const id = 'sequence';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*sequenceDiagram/) !== null; return /^\s*sequenceDiagram/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -2,14 +2,11 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'stateDiagram'; const id = 'stateDiagram';
const detector: DiagramDetector = (text, config) => { const detector: DiagramDetector = (txt, config) => {
if (text.match(/^\s*stateDiagram-v2/) !== null) { if (/^\s*stateDiagram-v2/.test(txt)) {
return true; return true;
} }
if (text.match(/^\s*stateDiagram/) && config?.state?.defaultRenderer === 'dagre-wrapper') { if (/^\s*stateDiagram/.test(txt) && config?.state?.defaultRenderer === 'dagre-wrapper') {
return true;
}
if (text.match(/^\s*stateDiagram/) && config?.state?.defaultRenderer === 'dagre-wrapper') {
return true; return true;
} }
return false; return false;

View File

@ -8,7 +8,7 @@ const detector: DiagramDetector = (txt, config) => {
if (config?.state?.defaultRenderer === 'dagre-wrapper') { if (config?.state?.defaultRenderer === 'dagre-wrapper') {
return false; return false;
} }
return txt.match(/^\s*stateDiagram/) !== null; return /^\s*stateDiagram/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { ExternalDiagramDefinition } from '../../diagram-api/types.js';
const id = 'timeline'; const id = 'timeline';
const detector = (txt: string) => { const detector = (txt: string) => {
return txt.match(/^\s*timeline/) !== null; return /^\s*timeline/.test(txt);
}; };
const loader = async () => { const loader = async () => {

View File

@ -3,7 +3,7 @@ import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-a
const id = 'journey'; const id = 'journey';
const detector: DiagramDetector = (txt) => { const detector: DiagramDetector = (txt) => {
return txt.match(/^\s*journey/) !== null; return /^\s*journey/.test(txt);
}; };
const loader = async () => { const loader = async () => {