I'm coding a simple chat browser application with Node.js. To keep multiple users from using the same name, I store all user's names in an array
userList.push(socketName);
and every time someone connects, I look for his name in this array
function inList(name, list) {
if(list.indexOf(name) === -1)
return false;
return true;
}
This work perfectly. My problem is I also want to check a white list in an external text file, which have a single name per line. My code for that is
let temp = fs.readFileSync('./list.txt', 'UTF-8');
var whiteList = temp.split('\n');
if(inList(socketName, whiteList)) {
//let user join
}
It is the same logic, I'm just loading the list from a file. In this case, function inList() will return true only if the name tested is in the last line of the file. Any of the other names in the list will cause it to return false, and I have no idea why.
I thought the string.split('\n') could leave the '\n' on the returned array, so I tried printing all whiteList[] elements on console and on another file and found nothing.
I'm actually not that experienced with javascript and would appreciate any help
Thanks!
edit: Solved it as suggested by @Barmar using
var whiteList = temp.split('\n').map(s => s.trim());
Thank you everyone for your help!