Spaces:
Runtime error
Runtime error
File size: 3,621 Bytes
56b6519 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
import jsonData from '../files/desc_cwe_en_es.json';
import parent_to_child from '../files/parent_to_child.json';
const cweObject: CWETranslateType[] = JSON.parse(jsonData);
const typedCWEData: CWEParentChildType[] = parent_to_child;
type CWETranslateType = {
'CWE-ID': string;
esp_name: string;
en_name: string;
};
type CWEParentChildType = {
id: number;
parent_id: null | number;
name: string;
children?: CWEParentChildType[];
};
type CWERelated = {
cwe: string;
cweParent?: string;
cweGrandParent?: string;
};
type CWERelatedObjects = {
cwe: CWETranslateType;
cweParent?: CWETranslateType;
cweGrandParent?: CWETranslateType;
};
type CWENumbers = {
priority: number;
label: string;
score: number;
};
const GetCWENameByLanguage = (language: string, cweDataArray: CWENumbers[]) => {
const findByNameRecursive = (
data: CWEParentChildType[],
nameToFind: string,
): CWEParentChildType | undefined => {
for (const item of data) {
if (item.name === nameToFind) {
return item;
}
if (item.children) {
const foundInChildren = findByNameRecursive(item.children, nameToFind);
if (foundInChildren) {
return foundInChildren;
}
}
}
return undefined;
};
const CWERelateds: CWERelated[] = cweDataArray.map(item => ({
cwe: item.label,
}));
CWERelateds.forEach(item => {
const foundCWE = findByNameRecursive(typedCWEData, item.cwe);
if (foundCWE && foundCWE.parent_id !== null) {
const parentName = `CWE-${foundCWE.parent_id}`;
item.cweParent = parentName;
const parent = findByNameRecursive(typedCWEData, parentName);
if (parent && parent.parent_id !== null) {
item.cweGrandParent = `CWE-${parent.parent_id}`;
}
}
});
const cweObjects: CWERelatedObjects[] = CWERelateds.map(item => {
const foundCWE: CWERelatedObjects = {
cwe: cweObject.find(itemIter => itemIter['CWE-ID'] === item.cwe) ?? {
'CWE-ID': '',
esp_name: '',
en_name: '',
},
};
if (item.cweParent) {
foundCWE.cweParent = cweObject.find(
itemIter => itemIter['CWE-ID'] === item.cweParent,
) ?? {
'CWE-ID': '',
esp_name: '',
en_name: '',
};
}
if (item.cweGrandParent) {
foundCWE.cweGrandParent = cweObject.find(
itemIter => itemIter['CWE-ID'] === item.cweGrandParent,
) ?? {
'CWE-ID': '',
esp_name: '',
en_name: '',
};
}
return foundCWE;
});
let traduccion: CWERelated[];
if (language.toLowerCase().includes('es')) {
traduccion = cweObjects.map(item => {
const result: CWERelated = {
cwe: `${item.cwe['CWE-ID']}: ${item.cwe.esp_name}`,
};
if (item.cweParent?.esp_name) {
result.cweParent = `${item.cweParent['CWE-ID']}: ${item.cweParent.esp_name}`;
}
if (item.cweGrandParent?.esp_name) {
result.cweGrandParent = `${item.cweGrandParent['CWE-ID']}: ${item.cweGrandParent.esp_name}`;
}
return result;
});
} else {
traduccion = cweObjects.map(item => {
const result: CWERelated = {
cwe: `${item.cwe['CWE-ID']}: ${item.cwe.en_name}`,
};
if (item.cweParent?.en_name) {
result.cweParent = `${item.cweParent['CWE-ID']}: ${item.cweParent.en_name}`;
}
if (item.cweGrandParent?.en_name) {
result.cweGrandParent = `${item.cweGrandParent['CWE-ID']}: ${item.cweGrandParent.en_name}`;
}
return result;
});
}
return traduccion;
};
export default GetCWENameByLanguage;
|