Skip to content

Commit

Permalink
Feat: Versione 1.1.7 - nuova logica controllo ordinamento voci
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-vernocchi committed Nov 28, 2024
1 parent 765eb0d commit ae5a97c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 92 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pa-website-validator-ng",
"version": "1.1.6",
"version": "1.1.7",
"main": "index.js",
"type": "module",
"scripts": {
Expand Down
16 changes: 12 additions & 4 deletions src/audits/municipality_menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,31 @@ class MenuAudit extends Audit {
result.missing_menu_voices = missingMandatoryElements.join(", ");

const orderResult = checkOrderLoose(menuItem, foundMenuElements);
result.wrong_order_menu_voices =
orderResult.elementsNotInSequence.join(", ");
if (!orderResult.inOrder) {
if (
orderResult.singleMove.length > 0 &&
orderResult.singleMove.length <= 2
) {
result.wrong_order_menu_voices = orderResult.singleMove.join(", ");
} else {
result.wrong_order_menu_voices = "Più di una voce non in ordine";
}
}

const containsMandatoryElementsResult =
missingMandatoryElements.length === 0;

if (
foundMenuElements.length === 4 &&
containsMandatoryElementsResult &&
orderResult.numberOfElementsNotInSequence === 0
orderResult.inOrder
) {
this.score = 1;
} else if (
foundMenuElements.length > 4 &&
foundMenuElements.length < 8 &&
containsMandatoryElementsResult &&
orderResult.numberOfElementsNotInSequence === 0
orderResult.inOrder
) {
this.score = 0.5;
}
Expand Down
26 changes: 20 additions & 6 deletions src/audits/municipality_service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,35 @@ class ServiceAudit extends Audit {
}

item.missing_elements = missingMandatoryItems.join(", ");
item.wrong_order_elements = orderResult.elementsNotInSequence.join(", ");

const missingVoicesAmount = missingMandatoryItems.length;
const voicesNotInCorrectOrderAmount =
orderResult.numberOfElementsNotInSequence;
if (!orderResult.inOrder) {
if (
orderResult.singleMove.length > 0 &&
orderResult.singleMove.length <= 2
) {
item.wrong_order_elements = orderResult.singleMove.join(", ");
} else {
item.wrong_order_elements = "Più di una voce non in ordine";
}
}

if (missingVoicesAmount > 2 || voicesNotInCorrectOrderAmount > 1) {
const missingVoicesAmount = missingMandatoryItems.length;
const moreThanAVoice =
!orderResult.inOrder && orderResult.singleMove.length === 0;
const onlyOneVoice =
!orderResult.inOrder &&
orderResult.singleMove.length > 0 &&
orderResult.singleMove.length <= 2;

if (missingVoicesAmount > 2 || moreThanAVoice) {
if (this.score > 0) {
this.score = 0;
}

this.wrongItems.push(item);
} else if (
(missingVoicesAmount > 0 && missingVoicesAmount <= 2) ||
voicesNotInCorrectOrderAmount === 1
onlyOneVoice
) {
if (this.score > 0.5) {
this.score = 0.5;
Expand Down
13 changes: 11 additions & 2 deletions src/audits/school_first_level_menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,17 @@ class SchoolFirstLevelMenuAudit extends Audit {
mandatoryPrimaryMenuItems,
foundMenuElements,
);
result.wrong_order_menu_voices =
orderResult.elementsNotInSequence.join(", ");

if (!orderResult.inOrder) {
if (
orderResult.singleMove.length > 0 &&
orderResult.singleMove.length <= 2
) {
result.wrong_order_menu_voices = orderResult.singleMove.join(", ");
} else {
result.wrong_order_menu_voices = "Più di una voce non in ordine";
}
}

const containsMandatoryElementsResult =
missingMandatoryElements.length === 0;
Expand Down
25 changes: 19 additions & 6 deletions src/audits/school_service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,34 @@ class SchoolServiceAudit extends Audit {
}

item.missing_elements = missingMandatoryItems.join(", ");
item.wrong_order_elements = orderResult.elementsNotInSequence.join(", ");
if (!orderResult.inOrder) {
if (
orderResult.singleMove.length > 0 &&
orderResult.singleMove.length <= 2
) {
item.wrong_order_elements = orderResult.singleMove.join(", ");
} else {
item.wrong_order_elements = "Più di una voce non in ordine";
}
}

const missingVoicesAmount = missingMandatoryItems.length;
const voicesNotInCorrectOrderAmount =
orderResult.numberOfElementsNotInSequence;

if (missingVoicesAmount > 2 || voicesNotInCorrectOrderAmount > 1) {
const moreThanAVoice =
!orderResult.inOrder && orderResult.singleMove.length === 0;
const onlyOneVoice =
!orderResult.inOrder &&
orderResult.singleMove.length > 0 &&
orderResult.singleMove.length <= 2;

if (missingVoicesAmount > 2 || moreThanAVoice) {
if (this.score > 0) {
this.score = 0;
}

this.wrongItems.push(item);
} else if (
(missingVoicesAmount > 0 && missingVoicesAmount <= 2) ||
voicesNotInCorrectOrderAmount === 1
onlyOneVoice
) {
if (this.score > 0.5) {
this.score = 0.5;
Expand Down
4 changes: 2 additions & 2 deletions src/types/crawler-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export interface Cookie {
}

export interface OrderResult {
numberOfElementsNotInSequence: number;
elementsNotInSequence: string[];
inOrder: boolean;
singleMove: string[];
}

export interface VocabularyResult {
Expand Down
105 changes: 36 additions & 69 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,42 +294,51 @@ const toMenuItem = (str: string): MenuItem => ({
regExp: new RegExp(`^${str}$`),
});

const checkOrderLoose = (
mandatoryElements: MenuItem[],
foundElements: string[],
): OrderType => {
const newMandatoryElements = mandatoryElements.filter((e) =>
foundElements.some((f) => e.regExp.test(f)),
);
const newFoundElements = foundElements.filter((e) =>
newMandatoryElements.some((f) => f.regExp.test(e)),
);
const arraysAreEqual = (arr1: MenuItem[], arr2: string[]) => {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (!arr1[i].regExp.test(arr2[i])) {
return false;
}
}
return true;
};

const elementsNotInSequence: string[] = [];
const checkOrder = (mandatoryElements: MenuItem[], foundElements: string[]) => {
const singleMove: string[] = [];
if (arraysAreEqual(mandatoryElements, foundElements)) {
return {
inOrder: true,
singleMove,
};
}

function checkOrderRecursive(correctArray: MenuItem[], checkArray: string[]) {
for (let i = 0; i < correctArray.length; i++) {
if (!correctArray[i].regExp.test(checkArray[i])) {
elementsNotInSequence.push(checkArray[i]);
for (const value of mandatoryElements.values()) {
const tmpMandatoryElements = mandatoryElements.filter(
(item) => item !== value,
);

checkOrderRecursive(
correctArray.filter((el) => el.name !== checkArray[i]),
checkArray.filter((el) => el !== checkArray[i]),
);
return;
}
const tmpFoundElements = foundElements.filter(
(item) => !value.regExp.test(item),
);

if (arraysAreEqual(tmpMandatoryElements, tmpFoundElements)) {
const valueFound = foundElements.filter((item) =>
value.regExp.test(item),
)[0];
singleMove.push(valueFound);
}
}

checkOrderRecursive(newMandatoryElements, newFoundElements);

return {
numberOfElementsNotInSequence: elementsNotInSequence.length,
elementsNotInSequence: elementsNotInSequence,
inOrder: false,
singleMove,
};
};

const checkOrder = (
const checkOrderLoose = (
mandatoryElements: MenuItem[],
foundElements: string[],
): OrderType => {
Expand All @@ -339,50 +348,8 @@ const checkOrder = (
const newFoundElements = foundElements.filter((e) =>
newMandatoryElements.some((f) => f.regExp.test(e)),
);
let numberOfElementsNotInSequence = 0;
const elementsNotInSequence = [];

//The first element is always in the right order
for (let i = 1; i < newFoundElements.length; i++) {
const indexInMandatory = newMandatoryElements.findIndex((e) =>
e.regExp.test(newFoundElements[i]),
);
let isInSequence = true;

if (indexInMandatory !== newMandatoryElements.length - 1) {
if (i === newFoundElements.length - 1) {
isInSequence = false;
} else if (
!newMandatoryElements[indexInMandatory + 1].regExp.test(
newFoundElements[i + 1],
)
) {
isInSequence = false;
}
}

if (indexInMandatory !== 0) {
if (i === 0) {
isInSequence = false;
} else if (
!newMandatoryElements[indexInMandatory - 1].regExp.test(
newFoundElements[i - 1],
)
) {
isInSequence = false;
}
}

if (!isInSequence) {
numberOfElementsNotInSequence++;
elementsNotInSequence.push(newFoundElements[i]);
}
}

return {
numberOfElementsNotInSequence: numberOfElementsNotInSequence,
elementsNotInSequence: elementsNotInSequence,
};
return checkOrder(newMandatoryElements, newFoundElements);
};

const missingMenuItems = (
Expand Down

0 comments on commit ae5a97c

Please sign in to comment.