JSFiddle - React, Tailwind, and code Playground
by dshilkret
JavaScript
var item = {};
item.title = "example"; // This is now the expected title to check against
var ext = "pdf";
var title = "example.pdf"; // This is now the primary string we're working with
let char = "."; // The character to search for
var result = getIsTitleMatch(char, title, item.title, ext );
console.log(result);
function getIsTitleMatch(char, compareToStr, compareStr, ext) {
var retVal;
let lastIndex = compareToStr.lastIndexOf(char);
if (lastIndex !== -1) {
let leftPart = compareToStr.substring(0, lastIndex);
let rightPart = compareToStr.substring(lastIndex + 1);
if (leftPart === compareStr && (rightPart === ext || rightPart === "")) {
retVal = true;
console.log("The compareToStr exactly matches the compareStr, and if an extension exists, it matches.");
console.log("Title (left part):", leftPart); // Expected output based on compareToStr
console.log("Extension (right part):", rightPart); // Expected output based on ext
} else if (leftPart !== compareStr) {
retVal = false;
console.log("The compareToStr does not match the compareStr.");
} else if (rightPart !== ext) {
retVal = false;
console.log("The extension does not match.");
}
} else {
if (compareToStr === compareStr) {
retVal = true;
console.log("The compareToStr matches the compareStr, and no extension requirement to check.");
} else {
retVal = false;
console.log("The compareToStr does not match the compareStr.");
}
}
return retVal;
}