mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-20 07:49:43 +02:00
enable prettier-plugin-jsdoc
This commit is contained in:
177
src/utils.js
177
src/utils.js
@@ -38,34 +38,36 @@ const directiveWithoutOpen =
|
||||
const anyComment = /\s*%%.*\n/gm;
|
||||
|
||||
/**
|
||||
* @function detectInit
|
||||
* Detects the init config object from the text
|
||||
* ```mermaid
|
||||
* %%{init: {"theme": "debug", "logLevel": 1 }}%%
|
||||
* graph LR
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* @function detectInit Detects the init config object from the text
|
||||
*
|
||||
* ```mermaid
|
||||
* %%{init: {"theme": "debug", "logLevel": 1 }}%%
|
||||
* graph LR
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* ```
|
||||
* or
|
||||
* ```mermaid
|
||||
* %%{initialize: {"theme": "dark", logLevel: "debug" }}%%
|
||||
* graph LR
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
*
|
||||
* Or
|
||||
*
|
||||
* ```mermaid
|
||||
* %%{initialize: {"theme": "dark", logLevel: "debug" }}%%
|
||||
* graph LR
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* ```
|
||||
* @param {string} text The text defining the graph
|
||||
* @param {any} cnf
|
||||
* @returns {object} the json object representing the init passed to mermaid.initialize()
|
||||
* @returns {object} The json object representing the init passed to mermaid.initialize()
|
||||
*/
|
||||
export const detectInit = function (text, cnf) {
|
||||
let inits = detectDirective(text, /(?:init\b)|(?:initialize\b)/);
|
||||
@@ -97,24 +99,25 @@ export const detectInit = function (text, cnf) {
|
||||
};
|
||||
|
||||
/**
|
||||
* @function detectDirective
|
||||
* Detects the directive from the text. Text can be single line or multiline. If type is null or omitted
|
||||
* the first directive encountered in text will be returned
|
||||
* ```mermaid
|
||||
* graph LR
|
||||
* %%{somedirective}%%
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* @function detectDirective Detects the directive from the text. Text can be single line or
|
||||
* multiline. If type is null or omitted the first directive encountered in text will be returned
|
||||
*
|
||||
* ```mermaid
|
||||
* graph LR
|
||||
* %%{somedirective}%%
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* ```
|
||||
* @param {string} text The text defining the graph
|
||||
* @param {string|RegExp} type The directive to return (default: null)
|
||||
* @returns {object | Array} An object or Array representing the directive(s): { type: string, args: object|null } matched by the input type
|
||||
* if a single directive was found, that directive object will be returned.
|
||||
* @param {string | RegExp} type The directive to return (default: null)
|
||||
* @returns {object | Array} An object or Array representing the directive(s): { type: string, args:
|
||||
* object|null } matched by the input type if a single directive was found, that directive object
|
||||
* will be returned.
|
||||
*/
|
||||
export const detectDirective = function (text, type = null) {
|
||||
try {
|
||||
@@ -158,19 +161,19 @@ export const detectDirective = function (text, type = null) {
|
||||
};
|
||||
|
||||
/**
|
||||
* @function detectType
|
||||
* Detects the type of the graph text. Takes into consideration the possible existence of an %%init
|
||||
* directive
|
||||
* ```mermaid
|
||||
* %%{initialize: {"startOnLoad": true, logLevel: "fatal" }}%%
|
||||
* graph LR
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* @function detectType Detects the type of the graph text. Takes into consideration the possible
|
||||
* existence of an %%init directive
|
||||
*
|
||||
* ```mermaid
|
||||
* %%{initialize: {"startOnLoad": true, logLevel: "fatal" }}%%
|
||||
* graph LR
|
||||
* a-->b
|
||||
* b-->c
|
||||
* c-->d
|
||||
* d-->e
|
||||
* e-->f
|
||||
* f-->g
|
||||
* g-->h
|
||||
* ```
|
||||
* @param {string} text The text defining the graph
|
||||
* @param {any} cnf
|
||||
@@ -248,11 +251,10 @@ const memoize = (fn, resolver) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* @function isSubstringInArray
|
||||
* Detects whether a substring in present in a given array
|
||||
* @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
|
||||
* @returns {number} The array index containing the substring or -1 if not present
|
||||
*/
|
||||
export const isSubstringInArray = function (str, arr) {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
@@ -391,7 +393,7 @@ const calcCardinalityPosition = (isRelationTypePresent, points, initialPosition)
|
||||
};
|
||||
|
||||
/**
|
||||
* position ['start_left', 'start_right', 'end_left', 'end_right']
|
||||
* Position ['start_left', 'start_right', 'end_left', 'end_right']
|
||||
*
|
||||
* @param {any} terminalMarkerSize
|
||||
* @param {any} position
|
||||
@@ -509,30 +511,27 @@ export const random = (options) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* @function assignWithDepth
|
||||
* Extends the functionality of {@link ObjectConstructor.assign} with the ability to merge arbitrary-depth objects
|
||||
* For each key in src with path `k` (recursively) performs an Object.assign(dst[`k`], src[`k`]) with
|
||||
* a slight change from the typical handling of undefined for dst[`k`]: instead of raising an error,
|
||||
* dst[`k`] is auto-initialized to {} and effectively merged with src[`k`]
|
||||
* <p>
|
||||
* Additionally, dissimilar types will not clobber unless the config.clobber parameter === true. Example:
|
||||
* ```
|
||||
* let config_0 = { foo: { bar: 'bar' }, bar: 'foo' };
|
||||
* let config_1 = { foo: 'foo', bar: 'bar' };
|
||||
* let result = assignWithDepth(config_0, config_1);
|
||||
* console.log(result);
|
||||
* //-> result: { foo: { bar: 'bar' }, bar: 'bar' }
|
||||
* ```
|
||||
* <p>
|
||||
* Traditional Object.assign would have clobbered foo in config_0 with foo in config_1.
|
||||
* <p>
|
||||
* If src is a destructured array of objects and dst is not an array, assignWithDepth will apply each element of src to dst
|
||||
* in order.
|
||||
* @param {any} dst - the destination of the merge
|
||||
* @param {any} src - the source object(s) to merge into destination
|
||||
* @param {{ depth: number, clobber: boolean }} config - depth: depth to traverse within src and dst for merging -
|
||||
* clobber: should dissimilar types clobber (default: { depth: 2, clobber: false })
|
||||
* @returns {*}
|
||||
* @function assignWithDepth Extends the functionality of {@link ObjectConstructor.assign} with the
|
||||
* ability to merge arbitrary-depth objects For each key in src with path `k` (recursively)
|
||||
* performs an Object.assign(dst[`k`], src[`k`]) with a slight change from the typical handling of
|
||||
* undefined for dst[`k`]: instead of raising an error, dst[`k`] is auto-initialized to {} and
|
||||
* effectively merged with src[`k`]<p> Additionally, dissimilar types will not clobber unless the
|
||||
* config.clobber parameter === true. Example:
|
||||
*
|
||||
* let config_0 = { foo: { bar: 'bar' }, bar: 'foo' };
|
||||
* let config_1 = { foo: 'foo', bar: 'bar' };
|
||||
* let result = assignWithDepth(config_0, config_1);
|
||||
* console.log(result);
|
||||
* //-> result: { foo: { bar: 'bar' }, bar: 'bar' }<p>
|
||||
*
|
||||
* Traditional Object.assign would have clobbered foo in config_0 with foo in config_1.<p> If src is
|
||||
* a destructured array of objects and dst is not an array, assignWithDepth will apply each
|
||||
* element of src to dst in order.
|
||||
* @param {any} dst - The destination of the merge
|
||||
* @param {any} src - The source object(s) to merge into destination
|
||||
* @param {{ depth: number; clobber: boolean }} config - Depth: depth to traverse within src and dst
|
||||
* for merging - clobber: should dissimilar types clobber (default: { depth: 2, clobber: false })
|
||||
* @returns {any}
|
||||
*/
|
||||
export const assignWithDepth = function (dst, src, config) {
|
||||
const { depth, clobber } = Object.assign({ depth: 2, clobber: false }, config);
|
||||
@@ -681,14 +680,13 @@ const breakString = memoize(
|
||||
);
|
||||
|
||||
/**
|
||||
* This calculates the text's height, taking into account the wrap breaks and
|
||||
* both the statically configured height, width, and the length of the text (in pixels).
|
||||
* This calculates the text's height, taking into account the wrap breaks and both the statically
|
||||
* configured height, width, and the length of the text (in pixels).
|
||||
*
|
||||
* If the wrapped text text has greater height, we extend the height, so it's
|
||||
* value won't overflow.
|
||||
* If the wrapped text text has greater height, we extend the height, so it's value won't overflow.
|
||||
*
|
||||
* @param {any} text the text to measure
|
||||
* @param {any} config - the config for fontSize, fontFamily, and fontWeight all impacting the resulting size
|
||||
* @param {any} text The text to measure
|
||||
* @param {any} config - The config for fontSize, fontFamily, and fontWeight all impacting the resulting size
|
||||
* @returns {any} - The height for the given text
|
||||
*/
|
||||
export const calculateTextHeight = function (text, config) {
|
||||
@@ -703,7 +701,7 @@ export const calculateTextHeight = function (text, config) {
|
||||
* This calculates the width of the given text, font size and family.
|
||||
*
|
||||
* @param {any} text - The text to calculate the width of
|
||||
* @param {any} config - the config for fontSize, fontFamily, and fontWeight all impacting the resulting size
|
||||
* @param {any} config - The config for fontSize, fontFamily, and fontWeight all impacting the resulting size
|
||||
* @returns {any} - The width for the given text
|
||||
*/
|
||||
export const calculateTextWidth = function (text, config) {
|
||||
@@ -715,7 +713,8 @@ export const calculateTextWidth = function (text, config) {
|
||||
* This calculates the dimensions of the given text, font size, font family, font weight, and margins.
|
||||
*
|
||||
* @param {any} text - The text to calculate the width of
|
||||
* @param {any} config - the config for fontSize, fontFamily, fontWeight, and margin all impacting the resulting size
|
||||
* @param {any} config - The config for fontSize, fontFamily, fontWeight, and margin all impacting
|
||||
* the resulting size
|
||||
* @returns - The width for the given text
|
||||
*/
|
||||
export const calculateTextDimensions = memoize(
|
||||
|
Reference in New Issue
Block a user