Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
wechuli committed Jan 7, 2024
1 parent 6150653 commit 9134df0
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/utils/inputsExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,44 @@ interface IminimalCheck{
}

function parseChecksArray(input: string): IminimalCheck[] {
// Return an empty array if the input is "-1"
if (input === "-1") {
return [];
}

// Trim the input to remove any leading/trailing whitespace
const trimmedInput = input.trim();
try{

// Check if the input starts with '[{', indicating a JSON array of objects
if (trimmedInput.startsWith('[{') && trimmedInput.endsWith('}]')) {
return JSON.parse(trimmedInput);
}
// Return an empty array if the input is "-1"
if (input === "-1") {
return [];
}

// Check if the input starts with a '{', indicating a JSON-like object
else if (trimmedInput.startsWith('{')) {
// Split the string by '},{', then add the missing braces back to each element
return trimmedInput.split('},{').map(element => {
if (!element.startsWith('{')) element = '{' + element;
if (!element.endsWith('}')) element = element + '}';
return JSON.parse(element);
});
}
// Trim the input to remove any leading/trailing whitespace
const trimmedInput = input.trim();

// Check if the input starts with '[{', indicating a JSON array of objects
if (trimmedInput.startsWith('[{') && trimmedInput.endsWith('}]')) {
return JSON.parse(trimmedInput);
}

// Check if the input starts with a '{', indicating a JSON-like object
else if (trimmedInput.startsWith('{')) {
// Split the string by '},{', then add the missing braces back to each element
return trimmedInput.split('},{').map(element => {
if (!element.startsWith('{')) element = '{' + element;
if (!element.endsWith('}')) element = element + '}';
return JSON.parse(element);
});
}

// Otherwise, assume it's a comma-separated list
else {
return trimmedInput.split(',').map(element => {
return {name: element.trim(),app_id: -1};
});
}

// Otherwise, assume it's a comma-separated list
else {
return trimmedInput.split(',').map(element => {
return {name: element.trim(),app_id: -1};
});
}
catch(error:any){
throw new Error("Error parsing checks array: " + error.message);
}

}

export const sanitizedInputs = inputsParser();
Expand Down

0 comments on commit 9134df0

Please sign in to comment.