Remove native memoize
This commit is contained in:
parent
056d5200c6
commit
8a476f882d
|
@ -73,6 +73,7 @@
|
||||||
"fast-clone": "^1.5.13",
|
"fast-clone": "^1.5.13",
|
||||||
"graphlib": "^2.1.8",
|
"graphlib": "^2.1.8",
|
||||||
"khroma": "^2.0.0",
|
"khroma": "^2.0.0",
|
||||||
|
"micro-memoize": "^4.0.11",
|
||||||
"moment-mini": "^2.24.0",
|
"moment-mini": "^2.24.0",
|
||||||
"non-layered-tidy-tree-layout": "^2.0.2",
|
"non-layered-tidy-tree-layout": "^2.0.2",
|
||||||
"stylis": "^4.0.10"
|
"stylis": "^4.0.10"
|
||||||
|
|
51
src/utils.ts
51
src/utils.ts
|
@ -20,6 +20,7 @@ import { log } from './logger';
|
||||||
import { detectType } from './diagram-api/detectType';
|
import { detectType } from './diagram-api/detectType';
|
||||||
import assignWithDepth from './assignWithDepth';
|
import assignWithDepth from './assignWithDepth';
|
||||||
import { MermaidConfig } from './config.type';
|
import { MermaidConfig } from './config.type';
|
||||||
|
import memoize from 'micro-memoize';
|
||||||
|
|
||||||
// Effectively an enum of the supported curve types, accessible by name
|
// Effectively an enum of the supported curve types, accessible by name
|
||||||
const d3CurveTypes = {
|
const d3CurveTypes = {
|
||||||
|
@ -43,10 +44,10 @@ const anyComment = /\s*%%.*\n/gm;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function detectInit Detects the init config object from the text
|
* @function detectInit Detects the init config object from the text
|
||||||
*
|
|
||||||
* @param config
|
* @param config
|
||||||
|
*
|
||||||
* ```mermaid
|
* ```mermaid
|
||||||
* %%{init: {"theme": "debug", "logLevel": 1 }}%%
|
* %%{init: {"theme": "debug", "logLevel": 1 }}%%
|
||||||
* graph LR
|
* graph LR
|
||||||
* a-->b
|
* a-->b
|
||||||
* b-->c
|
* b-->c
|
||||||
|
@ -165,27 +166,6 @@ export const detectDirective = function (text, type = null) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Caches results of functions based on input
|
|
||||||
*
|
|
||||||
* @param {Function} fn Function to run
|
|
||||||
* @param {Function} resolver Function that resolves to an ID given arguments the `fn` takes
|
|
||||||
* @returns {Function} An optimized caching function
|
|
||||||
*/
|
|
||||||
const memoize = (fn, resolver) => {
|
|
||||||
let cache = {};
|
|
||||||
return (...args) => {
|
|
||||||
let n = resolver ? resolver.apply(this, args) : args[0];
|
|
||||||
if (n in cache) {
|
|
||||||
return cache[n];
|
|
||||||
} else {
|
|
||||||
let result = fn(...args);
|
|
||||||
cache[n] = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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 {string} str The substring to detect
|
||||||
|
@ -594,7 +574,7 @@ export const wrapLabel = memoize(
|
||||||
return completedLines.filter((line) => line !== '').join(config.joinWith);
|
return completedLines.filter((line) => line !== '').join(config.joinWith);
|
||||||
},
|
},
|
||||||
(label, maxWidth, config) =>
|
(label, maxWidth, config) =>
|
||||||
`${label}-${maxWidth}-${config.fontSize}-${config.fontWeight}-${config.fontFamily}-${config.joinWith}`
|
`${label}${maxWidth}${config.fontSize}${config.fontWeight}${config.fontFamily}${config.joinWith}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const breakString = memoize(
|
const breakString = memoize(
|
||||||
|
@ -632,7 +612,8 @@ const breakString = memoize(
|
||||||
* 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} text The text to measure
|
||||||
* @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 height for the given text
|
* @returns {any} - The height for the given text
|
||||||
*/
|
*/
|
||||||
export const calculateTextHeight = function (text, config) {
|
export const calculateTextHeight = function (text, config) {
|
||||||
|
@ -647,7 +628,8 @@ export const calculateTextHeight = function (text, config) {
|
||||||
* This calculates the width of the given text, font size and family.
|
* This calculates the width of the given text, font size and family.
|
||||||
*
|
*
|
||||||
* @param {any} text - The text to calculate the width of
|
* @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
|
* @returns {any} - The width for the given text
|
||||||
*/
|
*/
|
||||||
export const calculateTextWidth = function (text, config) {
|
export const calculateTextWidth = function (text, config) {
|
||||||
|
@ -656,7 +638,8 @@ export const calculateTextWidth = function (text, config) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This calculates the dimensions of the given text, font size, font family, font weight, and margins.
|
* 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} text - The text to calculate the width of
|
||||||
* @param {any} config - The config for fontSize, fontFamily, fontWeight, and margin all impacting
|
* @param {any} config - The config for fontSize, fontFamily, fontWeight, and margin all impacting
|
||||||
|
@ -727,7 +710,8 @@ export const calculateTextDimensions = memoize(
|
||||||
* Applys d3 attributes
|
* Applys d3 attributes
|
||||||
*
|
*
|
||||||
* @param {any} d3Elem D3 Element to apply the attributes onto
|
* @param {any} d3Elem D3 Element to apply the attributes onto
|
||||||
* @param {[string, string][]} attrs Object.keys equivalent format of key to value mapping of attributes
|
* @param {[string, string][]} attrs Object.keys equivalent format of key to value mapping of
|
||||||
|
* attributes
|
||||||
*/
|
*/
|
||||||
const d3Attrs = function (d3Elem, attrs) {
|
const d3Attrs = function (d3Elem, attrs) {
|
||||||
for (let attr of attrs) {
|
for (let attr of attrs) {
|
||||||
|
@ -860,18 +844,12 @@ export interface DetailedError {
|
||||||
hash: any;
|
hash: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @param error */
|
||||||
*
|
|
||||||
* @param error
|
|
||||||
*/
|
|
||||||
export function isDetailedError(error: unknown): error is DetailedError {
|
export function isDetailedError(error: unknown): error is DetailedError {
|
||||||
return 'str' in error;
|
return 'str' in error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @param error */
|
||||||
*
|
|
||||||
* @param error
|
|
||||||
*/
|
|
||||||
export function getErrorMessage(error: unknown): string {
|
export function getErrorMessage(error: unknown): string {
|
||||||
if (error instanceof Error) return error.message;
|
if (error instanceof Error) return error.message;
|
||||||
return String(error);
|
return String(error);
|
||||||
|
@ -894,7 +872,6 @@ export default {
|
||||||
getStylesFromArray,
|
getStylesFromArray,
|
||||||
generateId,
|
generateId,
|
||||||
random,
|
random,
|
||||||
memoize,
|
|
||||||
runFunc,
|
runFunc,
|
||||||
entityDecode,
|
entityDecode,
|
||||||
initIdGenerator: initIdGenerator,
|
initIdGenerator: initIdGenerator,
|
||||||
|
|
|
@ -9070,6 +9070,11 @@ methods@~1.1.2:
|
||||||
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
|
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
|
||||||
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=
|
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=
|
||||||
|
|
||||||
|
micro-memoize@^4.0.11:
|
||||||
|
version "4.0.11"
|
||||||
|
resolved "https://registry.yarnpkg.com/micro-memoize/-/micro-memoize-4.0.11.tgz#f664afc8bd8c11cb2838716a7306d6e1ec205d3a"
|
||||||
|
integrity sha512-CjxsaYe4j43df32DtzzNCwanPqZjZDwuQAZilsCYpa2ZVtSPDjHXbTlR4gsEZRyO9/twHs0b7HLjvy/sowl7sA==
|
||||||
|
|
||||||
micromark-core-commonmark@^1.0.1:
|
micromark-core-commonmark@^1.0.1:
|
||||||
version "1.0.6"
|
version "1.0.6"
|
||||||
resolved "https://registry.yarnpkg.com/micromark-core-commonmark/-/micromark-core-commonmark-1.0.6.tgz#edff4c72e5993d93724a3c206970f5a15b0585ad"
|
resolved "https://registry.yarnpkg.com/micromark-core-commonmark/-/micromark-core-commonmark-1.0.6.tgz#edff4c72e5993d93724a3c206970f5a15b0585ad"
|
||||||
|
|
Loading…
Reference in New Issue