diff --git a/src/config.js b/src/config.js index d288155ce..0b5df6e0c 100644 --- a/src/config.js +++ b/src/config.js @@ -1,28 +1,27 @@ -let config = { -} +let config = {}; -const setConf = function (cnf) { +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; diff --git a/src/logger.js b/src/logger.js index eefaccf34..5de9e320a 100644 --- a/src/logger.js +++ b/src/logger.js @@ -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 = () => {} +export const setLogLevel = function(level) { + 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} : `; +}; diff --git a/src/utils.js b/src/utils.js index 462e7040c..357865fbf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,5 @@ -import * as d3 from 'd3' -import { logger } from './logger' +import * as d3 from 'd3'; +import { logger } from './logger'; /** * @function detectType @@ -18,34 +18,34 @@ import { logger } from './logger' * @param {string} text The text defining the graph * @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) +export const detectType = function(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 @@ -54,23 +54,23 @@ export const detectType = function (text) { * @param {array} arr The array to search * @returns {number} the array index containing the substring or -1 if not present **/ -export const isSubstringInArray = function (str, arr) { +export const isSubstringInArray = function(str, arr) { for (let i = 0; i < arr.length; i++) { - if (arr[i].match(str)) return i + if (arr[i].match(str)) return i; } - return -1 -} + return -1; +}; export const interpolateToCurve = (interpolate, defaultCurve) => { if (!interpolate) { - return defaultCurve + return defaultCurve; } - const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}` - return d3[curveName] || defaultCurve -} + const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`; + return d3[curveName] || defaultCurve; +}; export default { detectType, isSubstringInArray, interpolateToCurve -} +}; diff --git a/src/utils.spec.js b/src/utils.spec.js index 43341fb6d..86de39513 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -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') - }) - 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') - }) +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'); + }); + 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'); + }); - 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') - }) - 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') - }) -}) + 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'); + }); + 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'); + }); +}); -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) - }) - 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) - }) -}) +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); + }); + 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); + }); +});