#931 Compliacne with code standard

This commit is contained in:
knsv
2019-09-12 12:58:57 -07:00
parent f9b30bdb43
commit 0e8164d805
4 changed files with 87 additions and 88 deletions

View File

@@ -1,28 +1,27 @@
let config = {
}
let config = {};
const setConf = function(cnf) {
// Top level initially mermaid, gflow, sequenceDiagram and gantt
const lvl1Keys = Object.keys(cnf)
const lvl1Keys = Object.keys(cnf);
for (let i = 0; i < lvl1Keys.length; i++) {
if (typeof cnf[lvl1Keys[i]] === 'object' && cnf[lvl1Keys[i]] != null) {
const lvl2Keys = Object.keys(cnf[lvl1Keys[i]])
const lvl2Keys = Object.keys(cnf[lvl1Keys[i]]);
for (let j = 0; j < lvl2Keys.length; j++) {
// logger.debug('Setting conf ', lvl1Keys[i], '-', lvl2Keys[j])
if (typeof config[lvl1Keys[i]] === 'undefined') {
config[lvl1Keys[i]] = {}
config[lvl1Keys[i]] = {};
}
// logger.debug('Setting config: ' + lvl1Keys[i] + ' ' + lvl2Keys[j] + ' to ' + cnf[lvl1Keys[i]][lvl2Keys[j]])
config[lvl1Keys[i]][lvl2Keys[j]] = cnf[lvl1Keys[i]][lvl2Keys[j]]
config[lvl1Keys[i]][lvl2Keys[j]] = cnf[lvl1Keys[i]][lvl2Keys[j]];
}
} else {
config[lvl1Keys[i]] = cnf[lvl1Keys[i]]
}
config[lvl1Keys[i]] = cnf[lvl1Keys[i]];
}
}
};
export const setConfig = conf => {
setConf(conf)
}
export const getConfig = () => config
setConf(conf);
};
export const getConfig = () => config;

View File

@@ -1,4 +1,4 @@
import moment from 'moment-mini'
import moment from 'moment-mini';
export const LEVELS = {
debug: 1,
@@ -6,7 +6,7 @@ export const LEVELS = {
warn: 3,
error: 4,
fatal: 5
}
};
export const logger = {
debug: () => {},
@@ -14,32 +14,32 @@ export const logger = {
warn: () => {},
error: () => {},
fatal: () => {}
}
};
export const setLogLevel = function(level) {
logger.debug = () => {}
logger.info = () => {}
logger.warn = () => {}
logger.error = () => {}
logger.fatal = () => {}
logger.debug = () => {};
logger.info = () => {};
logger.warn = () => {};
logger.error = () => {};
logger.fatal = () => {};
if (level <= LEVELS.fatal) {
logger.fatal = console.log.bind(console, '\x1b[35m', format('FATAL'))
logger.fatal = console.log.bind(console, '\x1b[35m', format('FATAL'));
}
if (level <= LEVELS.error) {
logger.error = console.log.bind(console, '\x1b[31m', format('ERROR'))
logger.error = console.log.bind(console, '\x1b[31m', format('ERROR'));
}
if (level <= LEVELS.warn) {
logger.warn = console.log.bind(console, `\x1b[33m`, format('WARN'))
logger.warn = console.log.bind(console, `\x1b[33m`, format('WARN'));
}
if (level <= LEVELS.info) {
logger.info = console.log.bind(console, '\x1b[34m', format('INFO'))
logger.info = console.log.bind(console, '\x1b[34m', format('INFO'));
}
if (level <= LEVELS.debug) {
logger.debug = console.log.bind(console, '\x1b[32m', format('DEBUG'))
}
logger.debug = console.log.bind(console, '\x1b[32m', format('DEBUG'));
}
};
const format = (level) => {
const time = moment().format('HH:mm:ss.SSS')
return `${time} : ${level} : `
}
const format = level => {
const time = moment().format('HH:mm:ss.SSS');
return `${time} : ${level} : `;
};

View File

@@ -1,5 +1,5 @@
import * as d3 from 'd3'
import { logger } from './logger'
import * as d3 from 'd3';
import { logger } from './logger';
/**
* @function detectType
@@ -19,33 +19,33 @@ import { logger } from './logger'
* @returns {string} A graph definition key
*/
export const detectType = function(text) {
text = text.replace(/^\s*%%.*\n/g, '\n')
logger.debug('Detecting diagram type based on the text ' + text)
text = text.replace(/^\s*%%.*\n/g, '\n');
logger.debug('Detecting diagram type based on the text ' + text);
if (text.match(/^\s*sequenceDiagram/)) {
return 'sequence'
return 'sequence';
}
if (text.match(/^\s*gantt/)) {
return 'gantt'
return 'gantt';
}
if (text.match(/^\s*classDiagram/)) {
return 'class'
return 'class';
}
if (text.match(/^\s*gitGraph/)) {
return 'git'
return 'git';
}
if (text.match(/^\s*info/)) {
return 'info'
return 'info';
}
if (text.match(/^\s*pie/)) {
return 'pie'
return 'pie';
}
return 'flowchart'
}
return 'flowchart';
};
/**
* @function isSubstringInArray
@@ -56,21 +56,21 @@ export const detectType = function (text) {
**/
export const isSubstringInArray = function(str, arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].match(str)) return i
}
return -1
if (arr[i].match(str)) return i;
}
return -1;
};
export const interpolateToCurve = (interpolate, defaultCurve) => {
if (!interpolate) {
return defaultCurve
}
const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`
return d3[curveName] || defaultCurve
return defaultCurve;
}
const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`;
return d3[curveName] || defaultCurve;
};
export default {
detectType,
isSubstringInArray,
interpolateToCurve
}
};

View File

@@ -1,39 +1,39 @@
/* eslint-env jasmine */
import utils from './utils'
import utils from './utils';
describe('when detecting chart type ', function() {
it('should handle a graph defintion', function() {
const str = 'graph TB\nbfs1:queue'
const type = utils.detectType(str)
expect(type).toBe('flowchart')
})
const str = 'graph TB\nbfs1:queue';
const type = utils.detectType(str);
expect(type).toBe('flowchart');
});
it('should handle a graph defintion with leading spaces', function() {
const str = ' graph TB\nbfs1:queue'
const type = utils.detectType(str)
expect(type).toBe('flowchart')
})
const str = ' graph TB\nbfs1:queue';
const type = utils.detectType(str);
expect(type).toBe('flowchart');
});
it('should handle a graph defintion with leading spaces and newline', function() {
const str = ' \n graph TB\nbfs1:queue'
const type = utils.detectType(str)
expect(type).toBe('flowchart')
})
const str = ' \n graph TB\nbfs1:queue';
const type = utils.detectType(str);
expect(type).toBe('flowchart');
});
it('should handle a graph defintion for gitGraph', function() {
const str = ' \n gitGraph TB:\nbfs1:queue'
const type = utils.detectType(str)
expect(type).toBe('git')
})
})
const str = ' \n gitGraph TB:\nbfs1:queue';
const type = utils.detectType(str);
expect(type).toBe('git');
});
});
describe('when finding substring in array ', function() {
it('should return the array index that contains the substring', function() {
const arr = ['stroke:val1', 'fill:val2']
const result = utils.isSubstringInArray('fill', arr)
expect(result).toEqual(1)
})
const arr = ['stroke:val1', 'fill:val2'];
const result = utils.isSubstringInArray('fill', arr);
expect(result).toEqual(1);
});
it('should return -1 if the substring is not found in the array', function() {
const arr = ['stroke:val1', 'stroke-width:val2']
const result = utils.isSubstringInArray('fill', arr)
expect(result).toEqual(-1)
})
})
const arr = ['stroke:val1', 'stroke-width:val2'];
const result = utils.isSubstringInArray('fill', arr);
expect(result).toEqual(-1);
});
});