Check whether the extension is allowed or not using js strings

I have a system where the user specifies the extension and the system would return true if the given extension is allowed and false if its not allowed. The system has a preset list of file extensions that determine whether the given extension is allowed and disallowed.

If allowed list is empty then it means all are allowed except the ones in disallowed list. If disallowed list is empty then it means all are allowed and there is no disallowing of extensions. When both are empty then all are allowed.

I have tried with

var allowed = "";
var disallowed = "xlsx, docx, png";

function testAllowedOrNot(str) {
  var disallowedArray = disallowed.split(",").map(function(ext) {
    return ext.trim().toLowerCase()
  });

  var allowedArray = allowed.split(",").map(function(ext) {
    return ext.trim().toLowerCase()
  });

  return allowedArray.includes(str) && !disallowedArray.includes(str)

}

console.log(testAllowedOrNot("csv")) //must be true
console.log(testAllowedOrNot("xlsx")) //must be false
console.log(testAllowedOrNot("ico")) //must be true
console.log(testAllowedOrNot("jpg")) //must be true
console.log(testAllowedOrNot("docx")) //must be false

But its returning all as false. How can I change my code so I get the intended output.

Answer

Read the comments why your code did not work.

You are missing the test for empty allowedArray

I had to rewrite the code in case you have an allowed

It does not make much sense to have BOTH allowed and disallowed so perhaps rethink this?

const allowed = "";
const disallowed = "xlsx,docx,png"
const disallowedArray = disallowed.split(",").map(ext => ext.trim().toLowerCase()).filter(ext => ext !== "");
const allowedArray = allowed.split(",").map(ext => ext.trim().toLowerCase()).filter(ext => ext !== "")
const testAllowedOrNot = str => {
  if (allowedArray.length > 0) return allowedArray.includes(str)
  return disallowedArray.length === 0 || !disallowedArray.includes(str)
};

console.log(testAllowedOrNot("csv")) //must be true
console.log(testAllowedOrNot("xlsx")) //must be false
console.log(testAllowedOrNot("ico")) //must be true
console.log(testAllowedOrNot("jpg")) //must be true
console.log(testAllowedOrNot("docx")) //must be false