Skip to content

Instantly share code, notes, and snippets.

@vidocco
Last active August 4, 2018 13:23
Show Gist options
  • Save vidocco/be5c031d688de3c6da982516883f208e to your computer and use it in GitHub Desktop.
Save vidocco/be5c031d688de3c6da982516883f208e to your computer and use it in GitHub Desktop.
{
"name": "pairer",
"version": "1.0.0",
"description": "a pairer for CW",
"main": "pairer.js",
"scripts": {
"start": "node ./pairer.js"
},
"author": "Jack Thibaut",
"license": "MIT",
"bin": "./pairer.js"
}
#!/usr/bin/env node
const fs = require('fs')
const promisify = require('util').promisify
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.pause()
const read = promisify(fs.readFile)
const write = promisify(fs.writeFile)
const ask = (question) => new Promise((resolve, reject) => {
rl.resume()
rl.question(question, answer => {
rl.pause()
return resolve(answer)
})
})
const pairs = async ([a, b, input = './students.json', output = './previous.json']) => {
const { seniors, juniors } = await read(input)
.then(res => JSON.parse(res.toString('utf-8')))
.catch(() => ({seniors: false, juniors: false}))
if (!seniors && !juniors) {
const answer = await ask('No student file was found, do you want to create a new one? (Y/N) ')
if (answer === 'Y') {
const fileName = await ask('student file name: (\'students.json\') ')
.then(res => res || 'students.json')
const group1 = await ask('how many students in first group? ')
const group1Name = await ask('name of the first group? ')
const group1Arr = []
for (let i = 0; i < group1; i++) {
const student = await ask('student name: ')
group1Arr.push(student)
}
const group2 = await ask('how many students in second group? ')
const group2Name = await ask('name of the second group? ')
const group2Arr = []
for (let i = 0; i < group2; i++) {
const student = await ask('student name: ')
group2Arr.push(student)
}
await write(`./${fileName}`, JSON.stringify({
[group1Name]: group1Arr,
[group2Name]: group2Arr
}))
return pairs([0, 0, `./${fileName}`])
} else {
process.exit(0)
}
}
const division = juniors.length > seniors.length
? juniors.length / seniors.length
: seniors.length / juniors.length
const color = juniors.length > seniors.length
? '\x1b[32m'
: '\x1b[33m'
const iteratee = juniors.length > seniors.length
? seniors
: juniors
const opposite = juniors.length > seniors.length
? juniors
: seniors
const previous = await read(output)
.then(res => JSON.parse(res))
.catch(() => iteratee.reduce((acc, el) => {
acc[el] = []
return acc
}, {}))
const matched = new Set()
const newPairs = shuffle(iteratee).reduce((acc, el) => {
const matches = opposite.reduce((curr,pot) => {
if (!matched.has(pot) && curr.length < division && !previous[el].includes(pot)) {
curr.push(pot)
matched.add(pot)
}
return curr
}, [])
acc[el] = matches
previous[el].push(...matches)
return acc
}, {})
write(output, JSON.stringify(previous))
const single = Object.keys(newPairs).find(el => newPairs[el].length < division)
let loggingFormat = newPairs
if (single) {
const pairedIt = shuffle(Object.keys(newPairs)).reduce((acc, el) => !acc && el !== single ? el : acc, '')
loggingFormat = Object.keys(newPairs).reduce((acc, el) => {
return el !== pairedIt && el !== single
? {...acc, [el] : newPairs[el]}
: acc
} ,{[`${pairedIt} & ${single}`]: newPairs[pairedIt]})
}
console.log('=============================================')
console.log('= PAIRS FOR TODAY =')
console.log('=============================================')
Object.keys(loggingFormat).forEach(el => {
console.log(pad(`${color}${el}\x1b[0m: ${loggingFormat[el]}`, 51))
})
console.log('=============================================')
rl.close()
}
const pad = (str, length) => {
const padding = (length - str.length) / 2
let borders = '='
for (let i = 0; i < padding; i++) {
borders += ' '
}
return `${borders}${str}${padding%1 ? borders.slice(1) + '=' : borders.slice(1) + ' ='}`
}
const shuffle = (a) => {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
pairs(process.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment