const Table = require(‘cli-table’)
const log = console.log
const mockdata = require(‘./mock.json’)
const mockdata2 = require(‘./mock2.json’)
// Make the data random by shuffling
function randomizeArr(arr) {
let cIndex = arr.length, tempValue, randomIndex
while (cIndex != 0) {
randomIndex = Math.floor(Math.random() * cIndex)
cIndex -= 1
tempValue = arr[cIndex]
arr[cIndex] = arr[randomIndex]
arr[randomIndex] = tempValue
}
return arr
}
const data = randomizeArr(mockdata)
const data2 = randomizeArr(mockdata2)
// Save the drawing data here
let drawingResult = []
// Loop over the data, I know it O(n^2) and you should avoid that
for (let i = 0;i < data.length;i++) {
for (let j = 0;j < data2.length;j++) {
const cIndex = data[i]
const againstIndex = data2[j]
// This is the match up logic, to follow the procedure (see README)
if (againstIndex.subcity == cIndex.subcity) {
const drawOne = drawingResult.map(c => c.lane_id).indexOf(againstIndex.subcity)
const drawTwo = drawingResult.map(c => c.subcity).indexOf(cIndex.lane_id)
if (drawOne === -1 && drawTwo === -1) {
cIndex.subcity = againstIndex.subcity ? drawingResult.push(cIndex, againstIndex) :
drawingResult.push(cIndex,againstIndex)
}
}
}
}
// Help me to create a beautiful table in terminal
const table = new Table({
head: [‘#’, ‘Home (first leg)’, ‘Away (first leg)’],
colWidths: [10, 50, 50]
})
let counter = 1
for (let i = 0;i < drawingResult.length;i += 2) {
table.push([counter, drawingResult[i].plate, drawingResult[i + 1].lane_id])
counter++
}
log(table.toString())