Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extends the input type of zonesNormalize function #267

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/zones/zonesNormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,29 @@ 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 });
zones.push({ from, to } as Zone);
}

for (const zone of zones) {
Expand Down Expand Up @@ -78,7 +81,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 +115,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
10 changes: 5 additions & 5 deletions src/zones/zonesWithPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ZonesWithPointsOptions {
to?: number;
}

export interface FromToWithNumberOfPoints extends FromTo {
export interface ZoneWithNumberOfPoints extends FromTo {
numberOfPoints: number;
}

Expand All @@ -27,19 +27,19 @@ export interface FromToWithNumberOfPoints extends FromTo {
* @param options - options
* @returns array of zones with points
*/
export function zonesWithPoints(
zones: FromTo[] = [],
export function zonesWithPoints<Zone extends FromTo = FromTo>(
zones: Zone[] = [],

/**
* total number of points to distribute between zones
* @default 10
*/
numberOfPoints = 10,
options: ZonesWithPointsOptions = {},
): FromToWithNumberOfPoints[] {
): ZoneWithNumberOfPoints[] {
if (zones.length === 0) return [];
const normalizedZones = zonesNormalize(zones, options);
const zonesWithNumberOfPoints: FromToWithNumberOfPoints[] = [];
const zonesWithNumberOfPoints: ZoneWithNumberOfPoints[] = [];

const totalSize = normalizedZones.reduce((previous, current) => {
return previous + (current.to - current.from);
Expand Down
Loading