diff --git a/src/mermaid.js b/src/mermaid.js index 64016e26a..94fec80fc 100644 --- a/src/mermaid.js +++ b/src/mermaid.js @@ -78,7 +78,7 @@ const init = function() { mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig }); } - const nextId = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed); + const nextId = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed).next; let txt; diff --git a/src/utils.js b/src/utils.js index 471fddd44..18e35f738 100644 --- a/src/utils.js +++ b/src/utils.js @@ -791,12 +791,16 @@ export const configureSvgSize = function(svgElem, height, width, useMaxWidth) { }; export const initIdGeneratior = function(deterministic, seed) { - if (!deterministic) return () => Date.now(); - const iterator = function() { - return this.count++; - }; - iterator.seed = seed ? seed.length : 0; - return iterator; + if (!deterministic) return { next: () => Date.now() }; + class iterator { + constructor() { + return (this.count = seed ? seed.length : 0); + } + next() { + return this.count++; + } + } + return new iterator(); }; export default { @@ -820,5 +824,6 @@ export default { generateId, random, memoize, - runFunc + runFunc, + initIdGeneratior }; diff --git a/src/utils.spec.js b/src/utils.spec.js index 756ad3794..66b13a276 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -253,3 +253,34 @@ describe('when calculating SVG size', function() { expect(attrs.get('width')).toEqual(200); }); }); + +describe('when initializing the id generator', function () { + it('should return a random number generator based on Date', function (done) { + const idGenerator = utils.initIdGeneratior(false) + expect(typeof idGenerator.next).toEqual('function') + const lastId = idGenerator.next() + setTimeout(() => { + expect(idGenerator.next() > lastId).toBe(true) + done() + }, 5) + }); + + it('should return a non random number generator', function () { + const idGenerator = utils.initIdGeneratior(true) + expect(typeof idGenerator.next).toEqual('function') + const start = 0 + const lastId = idGenerator.next() + expect(start).toEqual(lastId) + expect(idGenerator.next()).toEqual(lastId +1) + }); + + it('should return a non random number generator based on seed', function () { + const idGenerator = utils.initIdGeneratior(true, 'thisIsASeed') + expect(typeof idGenerator.next).toEqual('function') + const start = 11 + const lastId = idGenerator.next() + expect(start).toEqual(lastId) + expect(idGenerator.next()).toEqual(lastId +1) + }); + +})