Replace var with const

This commit is contained in:
Tyler Long
2017-09-14 20:27:52 +08:00
parent d31e03d04e
commit 2e0bfaeb74
4 changed files with 88 additions and 89 deletions

View File

@@ -2,15 +2,15 @@ import { logger } from '../../logger'
import utils from '../../utils'
import d3 from '../../d3'
var vertices = {}
var edges = []
var classes = []
var subGraphs = []
var tooltips = {}
var subCount = 0
var direction
let vertices = {}
let edges = []
let classes = []
let subGraphs = []
let tooltips = {}
let subCount = 0
let direction
// Functions to be run after graph rendering
var funs = []
let funs = []
/**
* Function called by parser when a node definition has been found
* @param id
@@ -19,7 +19,7 @@ var funs = []
* @param style
*/
export const addVertex = function (id, text, type, style) {
var txt
let txt
if (typeof id === 'undefined') {
return
@@ -65,7 +65,7 @@ export const addVertex = function (id, text, type, style) {
*/
export const addLink = function (start, end, type, linktext) {
logger.info('Got edge...', start, end)
var edge = { start: start, end: end, type: undefined, text: '' }
const edge = { start: start, end: end, type: undefined, text: '' }
linktext = type.text
if (typeof linktext !== 'undefined') {
@@ -153,19 +153,19 @@ export const setClass = function (id, className) {
}
}
var setTooltip = function (id, tooltip) {
const setTooltip = function (id, tooltip) {
if (typeof tooltip !== 'undefined') {
tooltips[id] = tooltip
}
}
var setClickFun = function (id, functionName) {
const setClickFun = function (id, functionName) {
if (typeof functionName === 'undefined') {
return
}
if (typeof vertices[id] !== 'undefined') {
funs.push(function (element) {
var elem = d3.select(element).select('#' + id)
const elem = d3.select(element).select('#' + id)
if (elem !== null) {
elem.on('click', function () {
window[functionName](id)
@@ -175,13 +175,13 @@ var setClickFun = function (id, functionName) {
}
}
var setLink = function (id, linkStr) {
const setLink = function (id, linkStr) {
if (typeof linkStr === 'undefined') {
return
}
if (typeof vertices[id] !== 'undefined') {
funs.push(function (element) {
var elem = d3.select(element).select('#' + id)
const elem = d3.select(element).select('#' + id)
if (elem !== null) {
elem.on('click', function () {
window.open(linkStr, 'newTab')
@@ -244,8 +244,8 @@ export const getClasses = function () {
return classes
}
var setupToolTips = function (element) {
var tooltipElem = d3.select('.mermaidTooltip')
const setupToolTips = function (element) {
let tooltipElem = d3.select('.mermaidTooltip')
if (tooltipElem[0][0] === null) {
tooltipElem = d3.select('body')
.append('div')
@@ -253,18 +253,18 @@ var setupToolTips = function (element) {
.style('opacity', 0)
}
var svg = d3.select(element).select('svg')
const svg = d3.select(element).select('svg')
var nodes = svg.selectAll('g.node')
const nodes = svg.selectAll('g.node')
nodes
.on('mouseover', function () {
var el = d3.select(this)
var title = el.attr('title')
const el = d3.select(this)
const title = el.attr('title')
// Dont try to draw a tooltip if no data is provided
if (title === null) {
return
}
var rect = this.getBoundingClientRect()
const rect = this.getBoundingClientRect()
tooltipElem.transition()
.duration(200)
@@ -278,7 +278,7 @@ var setupToolTips = function (element) {
tooltipElem.transition()
.duration(500)
.style('opacity', 0)
var el = d3.select(this)
const el = d3.select(this)
el.classed('hover', false)
})
}
@@ -310,11 +310,11 @@ export const defaultStyle = function () {
*/
export const addSubGraph = function (list, title) {
function uniq (a) {
var prims = { 'boolean': {}, 'number': {}, 'string': {} }
var objs = []
const prims = { 'boolean': {}, 'number': {}, 'string': {} }
const objs = []
return a.filter(function (item) {
var type = typeof item
const type = typeof item
if (item === ' ') {
return false
}
@@ -322,29 +322,28 @@ export const addSubGraph = function (list, title) {
})
}
var nodeList = []
let nodeList = []
nodeList = uniq(nodeList.concat.apply(nodeList, list))
var subGraph = { id: 'subGraph' + subCount, nodes: nodeList, title: title }
const subGraph = { id: 'subGraph' + subCount, nodes: nodeList, title: title }
subGraphs.push(subGraph)
subCount = subCount + 1
return subGraph.id
}
var getPosForId = function (id) {
var i
for (i = 0; i < subGraphs.length; i++) {
const getPosForId = function (id) {
for (let i = 0; i < subGraphs.length; i++) {
if (subGraphs[i].id === id) {
return i
}
}
return -1
}
var secCount = -1
var posCrossRef = []
var indexNodes2 = function (id, pos) {
var nodes = subGraphs[pos].nodes
let secCount = -1
const posCrossRef = []
const indexNodes2 = function (id, pos) {
const nodes = subGraphs[pos].nodes
secCount = secCount + 1
if (secCount > 2000) {
return
@@ -358,13 +357,13 @@ var indexNodes2 = function (id, pos) {
}
}
var count = 0
var posCount = 1
let count = 0
let posCount = 1
while (count < nodes.length) {
var childPos = getPosForId(nodes[count])
const childPos = getPosForId(nodes[count])
// Ignore regular nodes (pos will be -1)
if (childPos >= 0) {
var res = indexNodes2(id, childPos)
const res = indexNodes2(id, childPos)
if (res.result) {
return {
result: true,

View File

@@ -8,22 +8,22 @@ describe('when parsing a gantt diagram it', function () {
})
it('should handle an dateFormat definition', function () {
var str = 'gantt\ndateFormat yyyy-mm-dd'
const str = 'gantt\ndateFormat yyyy-mm-dd'
parser.parse(str)
})
it('should handle an dateFormat definition', function () {
var str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid'
const str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid'
parser.parse(str)
})
it('should handle an dateFormat definition', function () {
var str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid'
const str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid'
parser.parse(str)
})
it('should handle an section definition', function () {
var str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid'
const str = 'gantt\ndateFormat yyyy-mm-dd\ntitle Adding gantt diagram functionality to mermaid'
parser.parse(str)
})
@@ -39,7 +39,7 @@ describe('when parsing a gantt diagram it', function () {
* params bapa - a unique bapap
*/
it('should handle a task definition', function () {
var str = 'gantt\n' +
const str = 'gantt\n' +
'dateFormat yyyy-mm-dd\n' +
'title Adding gantt diagram functionality to mermaid\n' +
'section Documentation\n' +

View File

@@ -11,7 +11,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2013-01-12')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-12', 'YYYY-MM-DD').toDate())
expect(tasks[0].id).toEqual('id1')
@@ -21,7 +21,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-03', 'YYYY-MM-DD').toDate())
expect(tasks[0].id).toEqual('id1')
@@ -31,7 +31,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2h')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-01 2:00', 'YYYY-MM-DD hh:mm').toDate())
expect(tasks[0].id).toEqual('id1')
@@ -41,7 +41,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2m')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-01 00:02', 'YYYY-MM-DD hh:mm').toDate())
expect(tasks[0].id).toEqual('id1')
@@ -51,7 +51,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2s')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-01 00:00:02', 'YYYY-MM-DD hh:mm:ss').toDate())
expect(tasks[0].id).toEqual('id1')
@@ -61,7 +61,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2w')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-15', 'YYYY-MM-DD').toDate())
expect(tasks[0].id).toEqual('id1')
@@ -74,7 +74,7 @@ describe('when using the ganttDb', function () {
ganttDb.addTask('test1', 'id1,2013-01-01,2w')
ganttDb.addTask('test2', 'id2,after id1,1d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[1].startTime).toEqual(moment('2013-01-15', 'YYYY-MM-DD').toDate())
expect(tasks[1].id).toEqual('id2')
@@ -86,7 +86,7 @@ describe('when using the ganttDb', function () {
ganttDb.addSection('testa1')
ganttDb.addTask('test1', 'id1,2013-01-01,2w')
ganttDb.addTask('test2', 'id2,after id3,1d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[1].startTime).toEqual(new Date((new Date()).setHours(0, 0, 0, 0)))
expect(tasks[1].id).toEqual('id2')
expect(tasks[1].task).toEqual('test2')
@@ -96,7 +96,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', '2013-01-01,2013-01-12')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-12', 'YYYY-MM-DD').toDate())
expect(tasks[0].id).toEqual('task1')
@@ -107,7 +107,7 @@ describe('when using the ganttDb', function () {
ganttDb.setDateFormat('YYYY-MM-DD')
ganttDb.addSection('testa1')
ganttDb.addTask('test1', '2013-01-01,4d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[0].startTime).toEqual(moment('2013-01-01', 'YYYY-MM-DD').toDate())
expect(tasks[0].endTime).toEqual(moment('2013-01-05', 'YYYY-MM-DD').toDate())
expect(tasks[0].id).toEqual('task1')
@@ -120,7 +120,7 @@ describe('when using the ganttDb', function () {
ganttDb.addTask('test1', 'id1,2013-01-01,2w')
ganttDb.addTask('test2', 'after id1,1d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[1].startTime).toEqual(moment('2013-01-15', 'YYYY-MM-DD').toDate())
expect(tasks[1].id).toEqual('task1')
@@ -132,7 +132,7 @@ describe('when using the ganttDb', function () {
ganttDb.addTask('test1', 'id1,2013-01-01,2w')
ganttDb.addTask('test2', '2013-01-26')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[1].startTime).toEqual(moment('2013-01-15', 'YYYY-MM-DD').toDate())
expect(tasks[1].endTime).toEqual(moment('2013-01-26', 'YYYY-MM-DD').toDate())
@@ -145,7 +145,7 @@ describe('when using the ganttDb', function () {
ganttDb.addTask('test1', 'id1,2013-01-01,2w')
ganttDb.addTask('test2', '2d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[1].startTime).toEqual(moment('2013-01-15', 'YYYY-MM-DD').toDate())
expect(tasks[1].endTime).toEqual(moment('2013-01-17', 'YYYY-MM-DD').toDate())
@@ -160,7 +160,7 @@ describe('when using the ganttDb', function () {
ganttDb.addSection('testa2')
ganttDb.addTask('test3', 'id3,after id1,2d')
var tasks = ganttDb.getTasks()
const tasks = ganttDb.getTasks()
expect(tasks[1].startTime).toEqual(moment('2013-01-17', 'YYYY-MM-DD').toDate())
expect(tasks[1].endTime).toEqual(moment('2013-01-18', 'YYYY-MM-DD').toDate())

View File

@@ -8,11 +8,11 @@ describe('when parsing a gitGraph', function () {
parser.yy.clear()
})
it('should handle a gitGraph defintion', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(1)
expect(parser.yy.getCurrentBranch()).toBe('master')
@@ -21,13 +21,13 @@ describe('when parsing a gitGraph', function () {
})
it('should handle a gitGraph defintion with empty options', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'options\n' +
'end\n' +
'commit\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(parser.yy.getOptions()).toEqual({})
expect(Object.keys(commits).length).toBe(1)
@@ -37,14 +37,14 @@ describe('when parsing a gitGraph', function () {
})
it('should handle a gitGraph defintion with valid options', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'options\n' +
'{"key": "value"}\n' +
'end\n' +
'commit\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(parser.yy.getOptions()['key']).toBe('value')
expect(Object.keys(commits).length).toBe(1)
expect(parser.yy.getCurrentBranch()).toBe('master')
@@ -53,14 +53,14 @@ describe('when parsing a gitGraph', function () {
})
it('should not fail on a gitGraph with malformed json', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'options\n' +
'{"key": "value"\n' +
'end\n' +
'commit\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(1)
expect(parser.yy.getCurrentBranch()).toBe('master')
expect(parser.yy.getDirection()).toBe('LR')
@@ -68,11 +68,11 @@ describe('when parsing a gitGraph', function () {
})
it('should handle set direction', function () {
var str = 'gitGraph BT:\n' +
const str = 'gitGraph BT:\n' +
'commit\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(1)
expect(parser.yy.getCurrentBranch()).toBe('master')
@@ -81,48 +81,48 @@ describe('when parsing a gitGraph', function () {
})
it('should checkout a branch', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'branch new\n' +
'checkout new\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(0)
expect(parser.yy.getCurrentBranch()).toBe('new')
})
it('should add commits to checked out branch', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'branch new\n' +
'checkout new\n' +
'commit\n' +
'commit\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(2)
expect(parser.yy.getCurrentBranch()).toBe('new')
var branchCommit = parser.yy.getBranches()['new']
const branchCommit = parser.yy.getBranches()['new']
expect(branchCommit).not.toBeNull()
expect(commits[branchCommit].parent).not.toBeNull()
})
it('should handle commit with args', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit "a commit"\n'
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(1)
var key = Object.keys(commits)[0]
const key = Object.keys(commits)[0]
expect(commits[key].message).toBe('a commit')
expect(parser.yy.getCurrentBranch()).toBe('master')
})
it('it should reset a branch', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n' +
'commit\n' +
'branch newbranch\n' +
@@ -132,7 +132,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(3)
expect(parser.yy.getCurrentBranch()).toBe('newbranch')
expect(parser.yy.getBranches()['newbranch']).toEqual(parser.yy.getBranches()['master'])
@@ -140,7 +140,7 @@ describe('when parsing a gitGraph', function () {
})
it('reset can take an argument', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n' +
'commit\n' +
'branch newbranch\n' +
@@ -150,15 +150,15 @@ describe('when parsing a gitGraph', function () {
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(3)
expect(parser.yy.getCurrentBranch()).toBe('newbranch')
var master = commits[parser.yy.getBranches()['master']]
const master = commits[parser.yy.getBranches()['master']]
expect(parser.yy.getHead().id).toEqual(master.parent)
})
it('it should handle fast forwardable merges', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n' +
'branch newbranch\n' +
'checkout newbranch\n' +
@@ -169,7 +169,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(3)
expect(parser.yy.getCurrentBranch()).toBe('master')
expect(parser.yy.getBranches()['newbranch']).toEqual(parser.yy.getBranches()['master'])
@@ -177,7 +177,7 @@ describe('when parsing a gitGraph', function () {
})
it('it should handle cases when merge is a noop', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n' +
'branch newbranch\n' +
'checkout newbranch\n' +
@@ -187,7 +187,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(3)
expect(parser.yy.getCurrentBranch()).toBe('newbranch')
expect(parser.yy.getBranches()['newbranch']).not.toEqual(parser.yy.getBranches()['master'])
@@ -195,7 +195,7 @@ describe('when parsing a gitGraph', function () {
})
it('it should handle merge with 2 parents', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n' +
'branch newbranch\n' +
'checkout newbranch\n' +
@@ -207,7 +207,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(5)
expect(parser.yy.getCurrentBranch()).toBe('master')
expect(parser.yy.getBranches()['newbranch']).not.toEqual(parser.yy.getBranches()['master'])
@@ -215,7 +215,7 @@ describe('when parsing a gitGraph', function () {
})
it('it should handle ff merge when history walk has two parents (merge commit)', function () {
var str = 'gitGraph:\n' +
const str = 'gitGraph:\n' +
'commit\n' +
'branch newbranch\n' +
'checkout newbranch\n' +
@@ -230,7 +230,7 @@ describe('when parsing a gitGraph', function () {
parser.parse(str)
var commits = parser.yy.getCommits()
const commits = parser.yy.getCommits()
expect(Object.keys(commits).length).toBe(6)
expect(parser.yy.getCurrentBranch()).toBe('newbranch')
expect(parser.yy.getBranches()['newbranch']).toEqual(parser.yy.getBranches()['master'])