mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-02-04 07:13:25 +08:00
fix: Use lodash memoize
This commit is contained in:
parent
1c6328cc1b
commit
5aae45dc97
@ -73,7 +73,6 @@
|
|||||||
"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"
|
||||||
@ -89,6 +88,7 @@
|
|||||||
"@types/d3": "^7.4.0",
|
"@types/d3": "^7.4.0",
|
||||||
"@types/dompurify": "^2.3.4",
|
"@types/dompurify": "^2.3.4",
|
||||||
"@types/jest": "^28.1.7",
|
"@types/jest": "^28.1.7",
|
||||||
|
"@types/lodash": "^4.14.184",
|
||||||
"@types/stylis": "^4.0.2",
|
"@types/stylis": "^4.0.2",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.36.1",
|
"@typescript-eslint/eslint-plugin": "^5.36.1",
|
||||||
"@typescript-eslint/parser": "^5.36.1",
|
"@typescript-eslint/parser": "^5.36.1",
|
||||||
|
@ -2,6 +2,7 @@ import utils from './utils';
|
|||||||
import assignWithDepth from './assignWithDepth';
|
import assignWithDepth from './assignWithDepth';
|
||||||
import { detectType } from './diagram-api/detectType';
|
import { detectType } from './diagram-api/detectType';
|
||||||
import { addDiagrams } from './diagram-api/diagram-orchestration';
|
import { addDiagrams } from './diagram-api/diagram-orchestration';
|
||||||
|
import { memoize } from 'lodash';
|
||||||
addDiagrams();
|
addDiagrams();
|
||||||
|
|
||||||
describe('when assignWithDepth: should merge objects within objects', function () {
|
describe('when assignWithDepth: should merge objects within objects', function () {
|
||||||
@ -121,20 +122,30 @@ describe('when assignWithDepth: should merge objects within objects', function (
|
|||||||
});
|
});
|
||||||
describe('when memoizing', function () {
|
describe('when memoizing', function () {
|
||||||
it('should return the same value', function () {
|
it('should return the same value', function () {
|
||||||
const fib = utils.memoize(function (n, canary) {
|
const fib = memoize(
|
||||||
canary.flag = true;
|
function (n, x, canary) {
|
||||||
if (n < 2) {
|
canary.flag = true;
|
||||||
return 1;
|
if (n < 2) {
|
||||||
} else {
|
return 1;
|
||||||
//We'll console.log a loader every time we have to recurse
|
} else {
|
||||||
return fib(n - 2, canary) + fib(n - 1, canary);
|
//We'll console.log a loader every time we have to recurse
|
||||||
}
|
return fib(n - 2, x, canary) + fib(n - 1, x, canary);
|
||||||
});
|
}
|
||||||
|
},
|
||||||
|
(n, x, _) => `${n}${x}`
|
||||||
|
);
|
||||||
let canary = { flag: false };
|
let canary = { flag: false };
|
||||||
fib(10, canary);
|
fib(10, 'a', canary);
|
||||||
expect(canary.flag).toBe(true);
|
expect(canary.flag).toBe(true);
|
||||||
canary = { flag: false };
|
canary = { flag: false };
|
||||||
fib(10, canary);
|
fib(10, 'a', canary);
|
||||||
|
expect(canary.flag).toBe(false);
|
||||||
|
fib(10, 'b', canary);
|
||||||
|
fib(10, 'b', canary);
|
||||||
|
expect(canary.flag).toBe(true);
|
||||||
|
canary = { flag: false };
|
||||||
|
fib(10, 'b', canary);
|
||||||
|
fib(10, 'a', canary);
|
||||||
expect(canary.flag).toBe(false);
|
expect(canary.flag).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -20,7 +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';
|
import { memoize } from 'lodash';
|
||||||
|
|
||||||
// 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 = {
|
||||||
@ -47,7 +47,8 @@ const anyComment = /\s*%%.*\n/gm;
|
|||||||
* @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
|
||||||
@ -602,7 +603,7 @@ const breakString = memoize(
|
|||||||
return { hyphenatedStrings: lines, remainingWord: currentLine };
|
return { hyphenatedStrings: lines, remainingWord: currentLine };
|
||||||
},
|
},
|
||||||
(word, maxWidth, hyphenCharacter = '-', config) =>
|
(word, maxWidth, hyphenCharacter = '-', config) =>
|
||||||
`${word}-${maxWidth}-${hyphenCharacter}-${config.fontSize}-${config.fontWeight}-${config.fontFamily}`
|
`${word}${maxWidth}${hyphenCharacter}${config.fontSize}${config.fontWeight}${config.fontFamily}`
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -703,7 +704,7 @@ export const calculateTextDimensions = memoize(
|
|||||||
: 1;
|
: 1;
|
||||||
return dims[index];
|
return dims[index];
|
||||||
},
|
},
|
||||||
(text, config) => `${text}-${config.fontSize}-${config.fontWeight}-${config.fontFamily}`
|
(text, config) => `${text}${config.fontSize}${config.fontWeight}${config.fontFamily}`
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
10
yarn.lock
10
yarn.lock
@ -2643,6 +2643,11 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
"@types/node" "*"
|
"@types/node" "*"
|
||||||
|
|
||||||
|
"@types/lodash@^4.14.184":
|
||||||
|
version "4.14.184"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.184.tgz#23f96cd2a21a28e106dc24d825d4aa966de7a9fe"
|
||||||
|
integrity sha512-RoZphVtHbxPZizt4IcILciSWiC6dcn+eZ8oX9IWEYfDMcocdd42f7NPI6fQj+6zI8y4E0L7gu2pcZKLGTRaV9Q==
|
||||||
|
|
||||||
"@types/mdast@^3.0.0":
|
"@types/mdast@^3.0.0":
|
||||||
version "3.0.10"
|
version "3.0.10"
|
||||||
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.10.tgz#4724244a82a4598884cbbe9bcfd73dff927ee8af"
|
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.10.tgz#4724244a82a4598884cbbe9bcfd73dff927ee8af"
|
||||||
@ -9070,11 +9075,6 @@ 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…
x
Reference in New Issue
Block a user