Skip to content

Commit

Permalink
fix: issue with optional types being mutated instead of cloned
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 23, 2024
1 parent 6e87653 commit 54bd279
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/translator-tags/src/util/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Sorted<T> {

if (b) {
if (Array.isArray(b)) {
return addSorted(this.compare, b, a) as Many<T>;
return addSorted(this.compare, [...b], a) as Many<T>;
}

return joinRepeatable(this.compare, b, a);
Expand Down Expand Up @@ -199,14 +199,23 @@ function unionSortedRepeatable<T>(
let aIndex = 0;
let bIndex = 0;

// Since both sides are Repeated<T> we can safely assume that the first 2 elements are present in either array.
const result: Many<T> = [
compare(a[aIndex], b[bIndex]) <= 0 ? a[aIndex++] : b[bIndex++],
compare(a[aIndex], b[bIndex]) <= 0 ? a[aIndex++] : b[bIndex++],
];
const result = [] as unknown as Many<T>;

while (aIndex < aLen && bIndex < bLen) {
result.push(compare(a[aIndex], b[bIndex]) <= 0 ? a[aIndex++] : b[bIndex++]);
const aValue = a[aIndex];
const bValue = b[bIndex];
const delta = compare(aValue, bValue);
if (delta === 0) {
aIndex++;
bIndex++;
result.push(aValue);
} else if (delta < 0) {
aIndex++;
result.push(aValue);
} else {
bIndex++;
result.push(bValue);
}
}

if (aLen === bLen && aIndex === aLen) {
Expand Down

0 comments on commit 54bd279

Please sign in to comment.