simplify this function: const getAccessAndResourceType = (resource, editMode, appType)…
- User
simplify this function: const getAccessAndResourceType = (resource, editMode, appType) => { let validations = ResourcesConfig.ResourceTypesValidationMap; let res = []; if(!resource){ //If resource URL is empty, simply return return res; } for (let key of validations) { let validity = InputHelper.checkValidity(resource, { [key]: true }); if (validity) { switch (key) { case 'validIPv4CIDR': { res = ['network', 'ipv4', 'validIPv4CIDR']; break; } case 'validFQDNWithPortAllowingAsterisk': { if (editMode && appType && appType !== 'application' && appType !== 'network') { res = []; } else { let matches = REGEX.validFQDNWithPortAllowingAsterisk.exec(resource); res = ['application', 'fqdn', 'validFQDNWithPortAllowingAsterisk']; if (matches && matches[2]) { res = ['application', 'fqdn', 'validFQDNWithPortAllowingAsterisk', true]; } } break; } case 'validUrlPattern': { let subnetPresent = REGEX.validNotResourceUrl.test(String(resource)); if (subnetPresent) { res = []; } else { res = ['application', 'url', 'validUrlPattern']; } break; } case 'validRdpPattern': { if (editMode && appType !== 'application' && appType !== 'network') { res = []; } else { res = ['application', 'url', 'validRdpPattern']; } break; } case 'validIcaPattern': { if (editMode && appType !== 'application' && appType !== 'network') { res = []; } else { res = ['application', 'url', 'validIcaPattern']; } break; } case 'validSshPattern': { if (editMode && appType !== 'application' && appType !== 'network') { res = []; } else { res = ['application', 'url', 'validSshPattern']; } break; } case 'validTcpPattern': { if (editMode && appType !== 'application' && appType !== 'network') { res = []; } else { res = ['application', 'url', 'validTcpPattern']; } break; } case 'validUdpPattern': { if (editMode && appType !== 'application' && appType !== 'network') { res = []; } else { res = ['application', 'url', 'validUdpPattern']; } break; } } break; } } return res; };
- Assistant
The
getAccessAndResourceTypefunction takes three parameters and returns an array of strings based on the validations performed on the input parameters.To simplify this function, we can remove the unnecessary checks, refactor the code for readability, and remove the redundancy in the switch cases.
Here's a simplified version of the
getAccessAndResourceTypefunction:typescript