Fix empty strings overwriting during scrape (#3647)

This commit is contained in:
WithoutPants
2023-04-08 08:15:09 +10:00
committed by GitHub
parent 0cd0151251
commit 9b8d124ac8
2 changed files with 24 additions and 3 deletions

View File

@@ -36,7 +36,9 @@ export class ScrapeResult<T> {
) {
this.originalValue = originalValue ?? undefined;
this.newValue = newValue ?? undefined;
const hasNewValue = this.newValue !== undefined;
// NOTE: this means that zero values are treated as null
// this is incorrect for numbers and booleans, but correct for strings
const hasNewValue = !!this.newValue;
const valuesEqual = isEqual(originalValue, newValue);
this.useNewValue = useNewValue ?? (hasNewValue && !valuesEqual);
@@ -68,6 +70,23 @@ export class ScrapeResult<T> {
}
}
// for types where !!value is a valid value (boolean and number)
export class ZeroableScrapeResult<T> extends ScrapeResult<T> {
public constructor(
originalValue?: T | null,
newValue?: T | null,
useNewValue?: boolean
) {
super(originalValue, newValue, useNewValue);
const hasNewValue = this.newValue !== undefined;
const valuesEqual = isEqual(originalValue, newValue);
this.useNewValue = useNewValue ?? (hasNewValue && !valuesEqual);
this.scraped = hasNewValue && !valuesEqual;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function hasScrapedValues(values: ScrapeResult<any>[]) {
return values.some((r) => r.scraped);