Add clone methods to all criterion classes (#5109)

This commit is contained in:
WithoutPants
2024-08-02 18:32:11 +10:00
committed by GitHub
parent 6a5dc4e774
commit c79f299d1a
5 changed files with 81 additions and 25 deletions

View File

@@ -89,14 +89,7 @@ export abstract class Criterion<V extends CriterionValue> {
this.value = value;
}
public clone(): Criterion<V> {
const newCriterion = new (this.constructor as new (
type: CriterionOption,
value: V
) => Criterion<V>)(this.criterionOption, this.value);
newCriterion.modifier = this.modifier;
return newCriterion;
}
public abstract clone(): Criterion<V>;
public static getModifierLabel(intl: IntlShape, modifier: CriterionModifier) {
const modifierMessageID = modifierMessageIDs[modifier];
@@ -519,6 +512,13 @@ export class StringCriterion extends Criterion<string> {
super(type, "");
}
public clone() {
const newCriterion = new StringCriterion(this.criterionOption);
newCriterion.modifier = this.modifier;
newCriterion.value = this.value;
return newCriterion;
}
protected getLabelValue(_intl: IntlShape) {
return this.value;
}
@@ -714,6 +714,17 @@ export function createMandatoryNumberCriterionOption(
}
export class NumberCriterion extends Criterion<INumberValue> {
constructor(type: CriterionOption) {
super(type, { value: undefined, value2: undefined });
}
public clone() {
const newCriterion = new NumberCriterion(this.criterionOption);
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
public get value(): INumberValue {
return this._value;
}
@@ -772,10 +783,6 @@ export class NumberCriterion extends Criterion<INumberValue> {
return true;
}
constructor(type: CriterionOption) {
super(type, { value: undefined, value2: undefined });
}
}
export class DurationCriterionOption extends MandatoryNumberCriterionOption {
@@ -796,6 +803,13 @@ export class DurationCriterion extends Criterion<INumberValue> {
super(type, { value: undefined, value2: undefined });
}
public clone() {
const newCriterion = new DurationCriterion(this.criterionOption);
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
public toCriterionInput(): IntCriterionInput {
return {
modifier: this.modifier,
@@ -869,6 +883,17 @@ export function createDateCriterionOption(value: CriterionType) {
}
export class DateCriterion extends Criterion<IDateValue> {
constructor(type: CriterionOption) {
super(type, { value: "", value2: undefined });
}
public clone() {
const newCriterion = new DateCriterion(this.criterionOption);
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
public encodeValue() {
return {
value: this.value.value,
@@ -915,10 +940,6 @@ export class DateCriterion extends Criterion<IDateValue> {
return true;
}
constructor(type: CriterionOption) {
super(type, { value: "", value2: undefined });
}
}
export class TimestampCriterionOption extends CriterionOption {
@@ -968,6 +989,17 @@ export function createMandatoryTimestampCriterionOption(value: CriterionType) {
}
export class TimestampCriterion extends Criterion<ITimestampValue> {
constructor(type: CriterionOption) {
super(type, { value: "", value2: undefined });
}
public clone() {
const newCriterion = new TimestampCriterion(this.criterionOption);
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
public encodeValue() {
return {
value: this.value?.value,
@@ -1025,8 +1057,4 @@ export class TimestampCriterion extends Criterion<ITimestampValue> {
return true;
}
constructor(type: CriterionOption) {
super(type, { value: "", value2: undefined });
}
}

View File

@@ -33,6 +33,13 @@ export class PerformersCriterion extends Criterion<ILabeledValueListValue> {
super(PerformersCriterionOption, { items: [], excluded: [] });
}
public clone() {
const newCriterion = new PerformersCriterion();
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
override get modifier(): CriterionModifier {
return this._modifier;
}

View File

@@ -29,6 +29,13 @@ export class PhashCriterion extends Criterion<IPhashDistanceValue> {
super(PhashCriterionOption, { value: "", distance: 0 });
}
public clone() {
const newCriterion = new PhashCriterion();
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
protected getLabelValue() {
const { value, distance } = this.value;
if (

View File

@@ -40,6 +40,18 @@ export const RatingCriterionOption = new CriterionOption({
export class RatingCriterion extends Criterion<INumberValue> {
ratingSystem: RatingSystemOptions;
constructor(ratingSystem: RatingSystemOptions) {
super(RatingCriterionOption, { value: 0, value2: undefined });
this.ratingSystem = ratingSystem;
}
public clone() {
const newCriterion = new RatingCriterion(this.ratingSystem);
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
public get value(): INumberValue {
return this._value;
}
@@ -76,9 +88,4 @@ export class RatingCriterion extends Criterion<INumberValue> {
return `${convertToRatingFormat(value, this.ratingSystem) ?? 0}`;
}
}
constructor(ratingSystem: RatingSystemOptions) {
super(RatingCriterionOption, { value: 0, value2: undefined });
this.ratingSystem = ratingSystem;
}
}

View File

@@ -27,6 +27,13 @@ export class StashIDCriterion extends Criterion<IStashIDValue> {
});
}
public clone() {
const newCriterion = new StashIDCriterion();
newCriterion.modifier = this.modifier;
newCriterion.value = { ...this.value };
return newCriterion;
}
public get value(): IStashIDValue {
return this._value;
}