mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-15 05:19:58 +02:00
#931 Compliacne with code standard
This commit is contained in:
@@ -1,28 +1,27 @@
|
|||||||
let config = {
|
let config = {};
|
||||||
}
|
|
||||||
|
|
||||||
const setConf = function (cnf) {
|
const setConf = function(cnf) {
|
||||||
// Top level initially mermaid, gflow, sequenceDiagram and gantt
|
// 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++) {
|
for (let i = 0; i < lvl1Keys.length; i++) {
|
||||||
if (typeof cnf[lvl1Keys[i]] === 'object' && cnf[lvl1Keys[i]] != null) {
|
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++) {
|
for (let j = 0; j < lvl2Keys.length; j++) {
|
||||||
// logger.debug('Setting conf ', lvl1Keys[i], '-', lvl2Keys[j])
|
// logger.debug('Setting conf ', lvl1Keys[i], '-', lvl2Keys[j])
|
||||||
if (typeof config[lvl1Keys[i]] === 'undefined') {
|
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]])
|
// 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 {
|
} else {
|
||||||
config[lvl1Keys[i]] = cnf[lvl1Keys[i]]
|
config[lvl1Keys[i]] = cnf[lvl1Keys[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const setConfig = conf => {
|
export const setConfig = conf => {
|
||||||
setConf(conf)
|
setConf(conf);
|
||||||
}
|
};
|
||||||
export const getConfig = () => config
|
export const getConfig = () => config;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import moment from 'moment-mini'
|
import moment from 'moment-mini';
|
||||||
|
|
||||||
export const LEVELS = {
|
export const LEVELS = {
|
||||||
debug: 1,
|
debug: 1,
|
||||||
@@ -6,7 +6,7 @@ export const LEVELS = {
|
|||||||
warn: 3,
|
warn: 3,
|
||||||
error: 4,
|
error: 4,
|
||||||
fatal: 5
|
fatal: 5
|
||||||
}
|
};
|
||||||
|
|
||||||
export const logger = {
|
export const logger = {
|
||||||
debug: () => {},
|
debug: () => {},
|
||||||
@@ -14,32 +14,32 @@ export const logger = {
|
|||||||
warn: () => {},
|
warn: () => {},
|
||||||
error: () => {},
|
error: () => {},
|
||||||
fatal: () => {}
|
fatal: () => {}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const setLogLevel = function (level) {
|
export const setLogLevel = function(level) {
|
||||||
logger.debug = () => {}
|
logger.debug = () => {};
|
||||||
logger.info = () => {}
|
logger.info = () => {};
|
||||||
logger.warn = () => {}
|
logger.warn = () => {};
|
||||||
logger.error = () => {}
|
logger.error = () => {};
|
||||||
logger.fatal = () => {}
|
logger.fatal = () => {};
|
||||||
if (level <= LEVELS.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) {
|
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) {
|
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) {
|
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) {
|
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 format = level => {
|
||||||
const time = moment().format('HH:mm:ss.SSS')
|
const time = moment().format('HH:mm:ss.SSS');
|
||||||
return `${time} : ${level} : `
|
return `${time} : ${level} : `;
|
||||||
}
|
};
|
||||||
|
44
src/utils.js
44
src/utils.js
@@ -1,5 +1,5 @@
|
|||||||
import * as d3 from 'd3'
|
import * as d3 from 'd3';
|
||||||
import { logger } from './logger'
|
import { logger } from './logger';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function detectType
|
* @function detectType
|
||||||
@@ -18,34 +18,34 @@ import { logger } from './logger'
|
|||||||
* @param {string} text The text defining the graph
|
* @param {string} text The text defining the graph
|
||||||
* @returns {string} A graph definition key
|
* @returns {string} A graph definition key
|
||||||
*/
|
*/
|
||||||
export const detectType = function (text) {
|
export const detectType = function(text) {
|
||||||
text = text.replace(/^\s*%%.*\n/g, '\n')
|
text = text.replace(/^\s*%%.*\n/g, '\n');
|
||||||
logger.debug('Detecting diagram type based on the text ' + text)
|
logger.debug('Detecting diagram type based on the text ' + text);
|
||||||
if (text.match(/^\s*sequenceDiagram/)) {
|
if (text.match(/^\s*sequenceDiagram/)) {
|
||||||
return 'sequence'
|
return 'sequence';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.match(/^\s*gantt/)) {
|
if (text.match(/^\s*gantt/)) {
|
||||||
return 'gantt'
|
return 'gantt';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.match(/^\s*classDiagram/)) {
|
if (text.match(/^\s*classDiagram/)) {
|
||||||
return 'class'
|
return 'class';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.match(/^\s*gitGraph/)) {
|
if (text.match(/^\s*gitGraph/)) {
|
||||||
return 'git'
|
return 'git';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.match(/^\s*info/)) {
|
if (text.match(/^\s*info/)) {
|
||||||
return 'info'
|
return 'info';
|
||||||
}
|
}
|
||||||
if (text.match(/^\s*pie/)) {
|
if (text.match(/^\s*pie/)) {
|
||||||
return 'pie'
|
return 'pie';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'flowchart'
|
return 'flowchart';
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function isSubstringInArray
|
* @function isSubstringInArray
|
||||||
@@ -54,23 +54,23 @@ export const detectType = function (text) {
|
|||||||
* @param {array} arr The array to search
|
* @param {array} arr The array to search
|
||||||
* @returns {number} the array index containing the substring or -1 if not present
|
* @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++) {
|
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) => {
|
export const interpolateToCurve = (interpolate, defaultCurve) => {
|
||||||
if (!interpolate) {
|
if (!interpolate) {
|
||||||
return defaultCurve
|
return defaultCurve;
|
||||||
}
|
}
|
||||||
const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`
|
const curveName = `curve${interpolate.charAt(0).toUpperCase() + interpolate.slice(1)}`;
|
||||||
return d3[curveName] || defaultCurve
|
return d3[curveName] || defaultCurve;
|
||||||
}
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
detectType,
|
detectType,
|
||||||
isSubstringInArray,
|
isSubstringInArray,
|
||||||
interpolateToCurve
|
interpolateToCurve
|
||||||
}
|
};
|
||||||
|
@@ -1,39 +1,39 @@
|
|||||||
/* eslint-env jasmine */
|
/* eslint-env jasmine */
|
||||||
import utils from './utils'
|
import utils from './utils';
|
||||||
|
|
||||||
describe('when detecting chart type ', function () {
|
describe('when detecting chart type ', function() {
|
||||||
it('should handle a graph defintion', function () {
|
it('should handle a graph defintion', function() {
|
||||||
const str = 'graph TB\nbfs1:queue'
|
const str = 'graph TB\nbfs1:queue';
|
||||||
const type = utils.detectType(str)
|
const type = utils.detectType(str);
|
||||||
expect(type).toBe('flowchart')
|
expect(type).toBe('flowchart');
|
||||||
})
|
});
|
||||||
it('should handle a graph defintion with leading spaces', function () {
|
it('should handle a graph defintion with leading spaces', function() {
|
||||||
const str = ' graph TB\nbfs1:queue'
|
const str = ' graph TB\nbfs1:queue';
|
||||||
const type = utils.detectType(str)
|
const type = utils.detectType(str);
|
||||||
expect(type).toBe('flowchart')
|
expect(type).toBe('flowchart');
|
||||||
})
|
});
|
||||||
|
|
||||||
it('should handle a graph defintion with leading spaces and newline', function () {
|
it('should handle a graph defintion with leading spaces and newline', function() {
|
||||||
const str = ' \n graph TB\nbfs1:queue'
|
const str = ' \n graph TB\nbfs1:queue';
|
||||||
const type = utils.detectType(str)
|
const type = utils.detectType(str);
|
||||||
expect(type).toBe('flowchart')
|
expect(type).toBe('flowchart');
|
||||||
})
|
});
|
||||||
it('should handle a graph defintion for gitGraph', function () {
|
it('should handle a graph defintion for gitGraph', function() {
|
||||||
const str = ' \n gitGraph TB:\nbfs1:queue'
|
const str = ' \n gitGraph TB:\nbfs1:queue';
|
||||||
const type = utils.detectType(str)
|
const type = utils.detectType(str);
|
||||||
expect(type).toBe('git')
|
expect(type).toBe('git');
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
describe('when finding substring in array ', function () {
|
describe('when finding substring in array ', function() {
|
||||||
it('should return the array index that contains the substring', function () {
|
it('should return the array index that contains the substring', function() {
|
||||||
const arr = ['stroke:val1', 'fill:val2']
|
const arr = ['stroke:val1', 'fill:val2'];
|
||||||
const result = utils.isSubstringInArray('fill', arr)
|
const result = utils.isSubstringInArray('fill', arr);
|
||||||
expect(result).toEqual(1)
|
expect(result).toEqual(1);
|
||||||
})
|
});
|
||||||
it('should return -1 if the substring is not found in the array', function () {
|
it('should return -1 if the substring is not found in the array', function() {
|
||||||
const arr = ['stroke:val1', 'stroke-width:val2']
|
const arr = ['stroke:val1', 'stroke-width:val2'];
|
||||||
const result = utils.isSubstringInArray('fill', arr)
|
const result = utils.isSubstringInArray('fill', arr);
|
||||||
expect(result).toEqual(-1)
|
expect(result).toEqual(-1);
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
Reference in New Issue
Block a user