fix: Use lodash memoize

This commit is contained in:
Sidharth Vinod
2022-09-09 16:34:57 +05:30
parent 1c6328cc1b
commit 5aae45dc97
4 changed files with 33 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ import utils from './utils';
import assignWithDepth from './assignWithDepth';
import { detectType } from './diagram-api/detectType';
import { addDiagrams } from './diagram-api/diagram-orchestration';
import { memoize } from 'lodash';
addDiagrams();
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 () {
it('should return the same value', function () {
const fib = utils.memoize(function (n, canary) {
canary.flag = true;
if (n < 2) {
return 1;
} else {
//We'll console.log a loader every time we have to recurse
return fib(n - 2, canary) + fib(n - 1, canary);
}
});
const fib = memoize(
function (n, x, canary) {
canary.flag = true;
if (n < 2) {
return 1;
} else {
//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 };
fib(10, canary);
fib(10, 'a', canary);
expect(canary.flag).toBe(true);
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);
});
});

View File

@@ -20,7 +20,7 @@ import { log } from './logger';
import { detectType } from './diagram-api/detectType';
import assignWithDepth from './assignWithDepth';
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
const d3CurveTypes = {
@@ -47,7 +47,8 @@ const anyComment = /\s*%%.*\n/gm;
* @param config
*
* ```mermaid
* %%{init: {"theme": "debug", "logLevel": 1 }}%%
*
* %%{init: {"theme": "debug", "logLevel": 1 }}%%
* graph LR
* a-->b
* b-->c
@@ -602,7 +603,7 @@ const breakString = memoize(
return { hyphenatedStrings: lines, remainingWord: currentLine };
},
(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;
return dims[index];
},
(text, config) => `${text}-${config.fontSize}-${config.fontWeight}-${config.fontFamily}`
(text, config) => `${text}${config.fontSize}${config.fontWeight}${config.fontFamily}`
);
/**