Skip to content

Commit

Permalink
Slice.normalize() refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 10, 2024
1 parent af5c1ce commit bbd2a89
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions src/structs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,40 +95,56 @@ export class Slice {
* @returns {NormalizedSlice} The normalized slice parameters.
*/
public normalize(containerLength: number): NormalizedSlice {
// TODO: Need refactor
let step = this.step ?? 1;

if (step === 0) {
throw new IndexError("Step cannot be 0.");
}
if (step > 0) {
let start = this.start ?? 0;
let end = this.end ?? containerLength;

let defaultEnd = (step < 0 && this.end === undefined) ? -1 : undefined;
[start, end, step] = [Math.round(start), Math.round(end), Math.round(step)];

let start = this.start ?? (step > 0 ? 0 : containerLength - 1);
let end = this.end ?? (step > 0 ? containerLength : -1);
start = normalizeIndex(start, containerLength, false);
end = normalizeIndex(end, containerLength, false);

start = Math.round(start);
end = Math.round(end);
step = Math.round(step);
if (start >= containerLength) {
start = end = containerLength - 1;
}

start = normalizeIndex(start, containerLength, false);
end = normalizeIndex(end, containerLength, false);
start = this.squeezeInBounds(start, 0, containerLength - 1);
end = this.squeezeInBounds(end, 0, containerLength);

if (step > 0 && start >= containerLength) {
start = end = containerLength - 1;
} else if (step < 0 && start < 0) {
start = end = 0;
defaultEnd = 0;
}
if (end < start) {
end = start;
}

return new NormalizedSlice(start, end, step);
} else if (step < 0) {
let start = this.start ?? containerLength - 1;
let end = this.end ?? -1;

[start, end, step] = [Math.round(start), Math.round(end), Math.round(step)];

start = normalizeIndex(start, containerLength, false);

if (!(this.end === undefined)) {
end = normalizeIndex(end, containerLength, false);
}

if (start < 0) {
start = end = 0;
}

start = this.squeezeInBounds(start, 0, containerLength - 1);
end = this.squeezeInBounds(end, -1, containerLength);

start = this.squeezeInBounds(start, 0, containerLength - 1);
end = this.squeezeInBounds(end, step > 0 ? 0 : -1, containerLength);
if (end > start) {
end = start;
}

if ((step > 0 && end < start) || (step < 0 && end > start)) {
end = start;
return new NormalizedSlice(start, end, step);
}

return new NormalizedSlice(start, defaultEnd ?? end, step);
throw new IndexError("Step cannot be 0.");
}

/**
Expand Down

0 comments on commit bbd2a89

Please sign in to comment.