-
Notifications
You must be signed in to change notification settings - Fork 0
/
nickname-checker.js
65 lines (55 loc) · 1.86 KB
/
nickname-checker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* This file handles reading the nickname CSV files and parsing them to an array
*/
const path = require('path')
// Path to nicknames folder
const dataDir = path.join(process.cwd(), 'data', 'nicknames')
const fs = require('fs')
/**
* Reads the CSV file and storing it to memory in an easily accessible form
* We're dealing with relatively small data which could easily be stored on memory
* The speed of the transaction is important because it should happen almost instantly
* Re-reading the file everytime you make a transaction is slower than accessing the memory
* @param {Array} data The array to push the data in to
* @param {String} loc File location relative to the root directory, enabling adding data values for multiple CSVs
* Data will be stored at an array I.E.
* [
* [name1, name2, name3, name4],
* [name1, name2, name3, name4]
* ]
*/
const readSingleCsv = (data, loc) => {
let content = fs.readFileSync(loc, { encoding: 'utf-8' })
content = content.toLowerCase().replace(/ /g, '').replace(/\r/g, '')
content = content.split('\n')
for (let i = 0; i < content.length; i++) {
content[i] = content[i].split(',')
data.push(content[i])
}
// Parse the content
// data.push(parse(content, {
// skip_empty_lines: true
// }).slice())
}
const readCsvs = data => {
const files = fs.readdirSync(dataDir)
files.forEach(file => readSingleCsv(data, `${dataDir}/${file}`))
}
// Initialising data array
const data = []
readCsvs(data)
// Check inside the data array if there exists any array with the two names specified in it
const checkNickname = (name1, name2) => {
name1 = name1.toLowerCase()
name2 = name2.toLowerCase()
if (name1 === name2) return true
for (let i = 0; i < data.length; i++) {
if (data[i].includes(name1) && data[i].includes(name2)) {
return true
}
}
return false
}
module.exports = {
checkNickname
}