Skip to content

Commit

Permalink
wip: Fixed regex for identifier convention checking
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobiClark committed Oct 10, 2024
1 parent 8fa65d6 commit c9cf893
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17283,7 +17283,7 @@ $("#guided-new-folder").on("click", () => {
let val = $("#add-new-folder-input").val();
const folderNameIsValid = window.evaluateStringAgainstSdsRequirements(
val,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);
if (folderNameIsValid) {
$("#add-new-folder-button").attr("disabled", false);
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/src/scripts/organize-dataset/curate-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,21 +522,21 @@ window.handleLocalDatasetImport = async (path) => {
for (let file in list.files) {
const filesIsForbiddenFilesList = window.evaluateStringAgainstSdsRequirements(
file,
"file-is-in-forbidden-files-list"
"is-forbidden-file"
);
if (filesIsForbiddenFilesList) {
forbiddenFileNames.push(file);
} else {
const fileNameIsValid = window.evaluateStringAgainstSdsRequirements(
file,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);

if (!fileNameIsValid) {
problematicFiles.push(file);
}

const fileIsHidden = window.evaluateStringAgainstSdsRequirements("file", "file-is-hidden");
const fileIsHidden = window.evaluateStringAgainstSdsRequirements("file", "is-hidden-file");
if (fileIsHidden) {
hiddenItems.push(file);
}
Expand Down
31 changes: 17 additions & 14 deletions src/renderer/src/scripts/others/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3756,7 +3756,7 @@ organizeDSaddNewFolder.addEventListener("click", function (event) {
let val = $("#add-new-folder-input").val();
const folderNameIsValid = window.evaluateStringAgainstSdsRequirements(
val,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);

if (folderNameIsValid) {
Expand Down Expand Up @@ -4190,7 +4190,7 @@ const localFolderPathAndSubFoldersHaveNoFiles = (localFolderPath) => {
const removeHiddenFilesFromDatasetStructure = (datasetStructure) => {
const currentFilesAtPath = Object.keys(datasetStructure.files);
for (const fileKey of currentFilesAtPath) {
const fileIsHidden = window.evaluateStringAgainstSdsRequirements(fileKey, "file-is-hidden");
const fileIsHidden = window.evaluateStringAgainstSdsRequirements(fileKey, "is-hidden-file");
if (fileIsHidden) {
delete datasetStructure["files"][fileKey];
}
Expand All @@ -4207,7 +4207,7 @@ const replaceProblematicFoldersWithSDSCompliantNames = (datasetStructure) => {
for (const folderKey of currentFoldersAtPath) {
const folderNameIsValid = window.evaluateStringAgainstSdsRequirements(
folderKey,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);
// If the folder name is not valid, replace it with a valid name and then recurse through the
// renamed folder to check for any other problematic folders
Expand All @@ -4233,7 +4233,7 @@ window.replaceProblematicFilesWithSDSCompliantNames = (datasetStructure) => {
for (const fileKey of currentFilesAtPath) {
const fileNameIsValid = window.evaluateStringAgainstSdsRequirements(
fileKey,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);
if (!fileNameIsValid) {
const newFileName = fileKey.replace(validSparcFolderAndFileNameRegex, "-");
Expand All @@ -4260,7 +4260,7 @@ window.replaceProblematicFilesWithSDSCompliantNames = (datasetStructure) => {
// for (const fileKey of currentFilesAtPath) {
// const fileNameIsValid = window.evaluateStringAgainstSdsRequirements(
// fileKey,
// "folder-and-file-name-is-valid"
// "folder-or-file-name-is-valid"
// );
// if (!fileNameIsValid) {
// delete datasetStructure["files"][fileKey];
Expand All @@ -4278,19 +4278,22 @@ const forbiddenFiles = {
"Thumbs.db": true,
};

const validSparcFolderAndFileNameRegex = /^[0-9A-Za-z,.-_ ]+$/;
const identifierConventionsRegex = /^[a-zA-Z0-9-_]+$/;
const validSparcFolderAndFileNameRegex = /^[0-9A-Za-z,.\-_ ]*$/;
const identifierConventionsRegex = /^[0-9A-Za-z-_]*$/;

window.evaluateStringAgainstSdsRequirements = (stringToTest, testType) => {
const tests = {
"is-valid-folder-or-file-name": validSparcFolderAndFileNameRegex.test(stringToTest),
"folder-or-file-name-is-valid": validSparcFolderAndFileNameRegex.test(stringToTest),
"string-adheres-to-identifier-conventions": identifierConventionsRegex.test(stringToTest),
"is-hidden-file": stringToTest.startsWith("."),
"is-forbidden-file": forbiddenFiles[stringToTest] === true,
"adheres-to-identifier-conventions": identifierConventionsRegex.test(stringToTest),
};
console.log(`${testType} ${stringToTest} ${tests[testType]}`);

return tests[testType];
};

// Create some test case examples
let loadingSweetAlert;
let loadingSweetAlertTimer;

Expand Down Expand Up @@ -4374,7 +4377,7 @@ window.buildDatasetStructureJsonFromImportedData = async (
const folderName = window.path.basename(pathToExplore);
const folderNameIsValid = window.evaluateStringAgainstSdsRequirements(
folderName,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);
if (!folderNameIsValid) {
problematicFolderNames.push(`${currentStructurePath}${folderName}`);
Expand Down Expand Up @@ -4414,7 +4417,7 @@ window.buildDatasetStructureJsonFromImportedData = async (

const fileIsInForbiddenFilesList = window.evaluateStringAgainstSdsRequirements(
fileName,
"file-is-in-forbidden-files-list"
"is-forbidden-file"
);

const fileIsEmpty = window.fs.fileSizeSync(pathToExplore) === 0;
Expand All @@ -4428,15 +4431,15 @@ window.buildDatasetStructureJsonFromImportedData = async (
// Check if the file name has any characters that do not comply with SPARC naming requirements
const fileNameIsValid = window.evaluateStringAgainstSdsRequirements(
fileName,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);
if (!fileNameIsValid) {
problematicFileNames.push(fileObject);
}

const fileIsHidden = window.evaluateStringAgainstSdsRequirements(
fileName,
"file-is-hidden"
"is-hidden-file"
);
if (fileIsHidden) {
hiddenItems.push(fileObject);
Expand Down Expand Up @@ -4819,7 +4822,7 @@ window.detectIrregularFolders = (localFolderPath) => {
const folderName = window.path.basename(localFolderPath);
const folderNameIsValid = window.evaluateStringAgainstSdsRequirements(
folderName,
"folder-and-file-name-is-valid"
"folder-or-file-name-is-valid"
);
if (!folderNameIsValid) {
window.irregularFolderArray.push(localFolderPath);
Expand Down

0 comments on commit c9cf893

Please sign in to comment.