@dra2020/district-analytics 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -9382,17 +9382,22 @@ const U = __importStar(__webpack_require__(/*! ./utils */ "./src/utils.ts"));
9382
9382
  const D = __importStar(__webpack_require__(/*! ./_data */ "./src/_data.ts"));
9383
9383
  function doPopulationDeviation(s, bLog = false) {
9384
9384
  let test = s.getTest(4 /* PopulationDeviation */);
9385
- // Compute the min, max, and average district populations,
9386
- // excluding the dummy 'unassigned' 0 and N+1 summary "districts."
9385
+ let targetSize = s.state.totalPop / s.state.nDistricts;
9386
+ // Compute the min & max district populations
9387
+ // ... excluding the dummy the 'unassigned' 0 and N+1 summary "districts"
9387
9388
  let totPopByDistrict = s.districts.statistics[D.DistrictField.TotalPop];
9388
9389
  totPopByDistrict = totPopByDistrict.slice(1, -1);
9389
- let min = U.minArray(totPopByDistrict);
9390
- let max = U.maxArray(totPopByDistrict);
9391
- let total = U.sumArray(totPopByDistrict);
9392
- // Calculate the raw population deviation.
9393
- // The target size is the average population.
9394
- let avg = total / s.state.nDistricts;
9395
- let popDev = (max - min) / avg;
9390
+ // Remove empty districts
9391
+ totPopByDistrict = totPopByDistrict.filter(x => x > 0);
9392
+ let min = 0;
9393
+ let max = 0;
9394
+ // If there's more than 1 non-empty district, calculate a non-zero deviation
9395
+ if (totPopByDistrict.length > 1) {
9396
+ min = U.minArray(totPopByDistrict);
9397
+ max = U.maxArray(totPopByDistrict);
9398
+ }
9399
+ // Calculate the raw population deviation
9400
+ let popDev = (max - min) / targetSize;
9396
9401
  // Round the raw value to the desired level of precision
9397
9402
  popDev = U.trim(popDev);
9398
9403
  // Populate the test entry
@@ -9402,7 +9407,7 @@ function doPopulationDeviation(s, bLog = false) {
9402
9407
  let totalPop = s.districts.statistics[D.DistrictField.TotalPop];
9403
9408
  let popDevPct = s.districts.statistics[D.DistrictField.PopDevPct];
9404
9409
  let summaryRow = s.districts.numberOfRows() - 1;
9405
- totalPop[summaryRow] = avg; // aka "target size"
9410
+ totalPop[summaryRow] = targetSize;
9406
9411
  popDevPct[summaryRow] = popDev;
9407
9412
  return test;
9408
9413
  }