fix(id-generator): re-write generator to allow it to function properly after compiling in cli

This commit is contained in:
Ahmad Nassri
2021-04-12 15:56:17 -04:00
parent 1b3971d6da
commit 84f664de14
3 changed files with 16 additions and 14 deletions

View File

@@ -78,7 +78,7 @@ const init = function() {
mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig }); mermaidAPI.updateSiteConfig({ gantt: mermaid.ganttConfig });
} }
const idGeneratior = utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed); const idGeneratior = new utils.initIdGeneratior(conf.deterministicIds, conf.deterministicIDSeed);
let txt; let txt;

View File

@@ -793,17 +793,19 @@ export const configureSvgSize = function(svgElem, height, width, useMaxWidth) {
d3Attrs(svgElem, attrs); d3Attrs(svgElem, attrs);
}; };
export const initIdGeneratior = function(deterministic, seed) { export const initIdGeneratior = class iterator {
if (!deterministic) return { next: () => Date.now() }; constructor (deterministic, seed) {
class iterator { this.deterministic = deterministic;
constructor() { this.seed = seed;
return (this.count = seed ? seed.length : 0);
this.count = seed ? seed.length : 0
} }
next() { next() {
if (!this.deterministic) return Date.now();
return this.count++; return this.count++;
} }
}
return new iterator();
}; };
export default { export default {

View File

@@ -256,7 +256,7 @@ describe('when calculating SVG size', function() {
describe('when initializing the id generator', function () { describe('when initializing the id generator', function () {
it('should return a random number generator based on Date', function (done) { it('should return a random number generator based on Date', function (done) {
const idGenerator = utils.initIdGeneratior(false) const idGenerator = new utils.initIdGeneratior(false)
expect(typeof idGenerator.next).toEqual('function') expect(typeof idGenerator.next).toEqual('function')
const lastId = idGenerator.next() const lastId = idGenerator.next()
setTimeout(() => { setTimeout(() => {
@@ -266,7 +266,7 @@ describe('when initializing the id generator', function () {
}); });
it('should return a non random number generator', function () { it('should return a non random number generator', function () {
const idGenerator = utils.initIdGeneratior(true) const idGenerator = new utils.initIdGeneratior(true)
expect(typeof idGenerator.next).toEqual('function') expect(typeof idGenerator.next).toEqual('function')
const start = 0 const start = 0
const lastId = idGenerator.next() const lastId = idGenerator.next()
@@ -275,7 +275,7 @@ describe('when initializing the id generator', function () {
}); });
it('should return a non random number generator based on seed', function () { it('should return a non random number generator based on seed', function () {
const idGenerator = utils.initIdGeneratior(true, 'thisIsASeed') const idGenerator = new utils.initIdGeneratior(true, 'thisIsASeed')
expect(typeof idGenerator.next).toEqual('function') expect(typeof idGenerator.next).toEqual('function')
const start = 11 const start = 11
const lastId = idGenerator.next() const lastId = idGenerator.next()