From 16d7c8f3dcc6f96c0269e13b444aafb0c9fc7c02 Mon Sep 17 00:00:00 2001 From: moanos Date: Sun, 13 Apr 2025 19:17:23 +0200 Subject: [PATCH] refactor: abstract functions to reuse independent of number of rats --- src/index.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index afca4f9..da2fd1a 100644 --- a/src/index.js +++ b/src/index.js @@ -151,23 +151,17 @@ class Validator { return MINIMUM_AREA_THREE_RATS + (numOfRats - 3) * AREA_PER_ADDITIONAL_RAT; } - cageCheck(dimensions, numRats, numFullFloors) { + failCageNumberIndependent(dimensions, numFullFloors) { + /* + Function that checks a cage independent of the number of rats. + */ let failedCriteria = {}; - if (numRats < 2 || numRats > 15) { - failedCriteria[FAILED_NUM_RATS] = this.FAIL_CRITERIA[FAILED_NUM_RATS]; - } - const baseArea = dimensions.depth * dimensions.width; if (baseArea < MINIMUM_BASE_AREA) { failedCriteria[FAILED_BASE_AREA] = this.FAIL_CRITERIA[FAILED_BASE_AREA]; } - const areaNeeded = this.overallAreaNeeded(numRats); - if (baseArea * numFullFloors < areaNeeded) { - failedCriteria[FAILED_OVERALL_AREA] = this.FAIL_CRITERIA[FAILED_OVERALL_AREA]; - } - if (dimensions.height / numFullFloors > MAXIMUM_FALL_HEIGHT) { failedCriteria[FAILED_FALL_HEIGHT] = this.FAIL_CRITERIA[FAILED_FALL_HEIGHT]; } @@ -187,6 +181,25 @@ class Validator { return failedCriteria; } + cageCheck(dimensions, numRats, numFullFloors) { + /* + Function that checks a cage based on overall criteria and the number of rats. + */ + let failedCriteria = this.failCageNumberIndependent(dimensions, numFullFloors); + + if (numRats < 2 || numRats > 15) { + failedCriteria[FAILED_NUM_RATS] = this.FAIL_CRITERIA[FAILED_NUM_RATS]; + } + + const areaNeeded = this.overallAreaNeeded(numRats); + const baseArea = dimensions.depth * dimensions.width; + if (baseArea * numFullFloors < areaNeeded) { + failedCriteria[FAILED_OVERALL_AREA] = this.FAIL_CRITERIA[FAILED_OVERALL_AREA]; + } + + return failedCriteria; + } + }