Skip to content

Commit

Permalink
refactor: extends the input type of zonesNormalize function
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Nov 5, 2024
1 parent 4702fe6 commit b0e9ba7
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/zones/zonesNormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,26 @@ export interface ZonesNormalizeOptions {
* @param options - options
* @returns array of zones
*/
export function zonesNormalize(
zones: FromTo[] = [],
export function zonesNormalize<Zone extends FromTo = FromTo>(
zones: Zone[] = [],
options: ZonesNormalizeOptions = {},
): FromTo[] {
): Zone[] {
const { exclusions = [] } = options;
let { from = Number.NEGATIVE_INFINITY, to = Number.POSITIVE_INFINITY } =
options;

if (from > to) [from, to] = [to, from];

zones = zones
.map((zone: FromTo) =>
zone.from > zone.to ? { from: zone.to, to: zone.from } : { ...zone },
.map((zone: Zone) =>
zone.from > zone.to
? { ...zone, from: zone.to, to: zone.from }
: { ...zone },
)
.sort((a, b) => {
if (a.from !== b.from) return a.from - b.from;
return a.to - b.to;
});
if (zones.length === 0) {
zones.push({ from, to });
}

for (const zone of zones) {
if (from > zone.from) zone.from = from;
Expand Down Expand Up @@ -78,7 +77,7 @@ export function zonesNormalize(
const normalizedExclusions = zonesNormalize(exclusions);

let currentExclusionIndex = 0;
const results: FromTo[] = [];
const results: Zone[] = [];
for (
let zoneIndex = 0;
zoneIndex < beforeExclusionsZones.length;
Expand Down Expand Up @@ -112,13 +111,15 @@ export function zonesNormalize(
continue;
}
results.push({
...zone,
from: normalizedExclusions[currentExclusionIndex].to,
to: zone.to,
});
}
// we cut in the middle, we need to create more zones, annoying !
if (normalizedExclusions[currentExclusionIndex].from > zone.from) {
results.push({
...zone,
from: zone.from,
to: normalizedExclusions[currentExclusionIndex].from,
});
Expand Down

0 comments on commit b0e9ba7

Please sign in to comment.