Skip to content

Commit 73f3701

Browse files
authored
refactor: use Object.values instead of switch (#139)
Changing way to validate enum values
2 parents 383e4a3 + 3ba0b9b commit 73f3701

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/Contexts/Shared/domain/criteria/FilterOperator.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,13 @@ export class FilterOperator extends EnumValueObject<Operator> {
1616
}
1717

1818
static fromValue(value: string): FilterOperator {
19-
switch (value) {
20-
case Operator.EQUAL:
21-
return new FilterOperator(Operator.EQUAL);
22-
case Operator.NOT_EQUAL:
23-
return new FilterOperator(Operator.NOT_EQUAL);
24-
case Operator.GT:
25-
return new FilterOperator(Operator.GT);
26-
case Operator.LT:
27-
return new FilterOperator(Operator.LT);
28-
case Operator.CONTAINS:
29-
return new FilterOperator(Operator.CONTAINS);
30-
case Operator.NOT_CONTAINS:
31-
return new FilterOperator(Operator.NOT_CONTAINS);
32-
default:
33-
throw new InvalidArgumentError(`The filter operator ${value} is invalid`);
19+
for (const operatorValue of Object.values(Operator)) {
20+
if (value === operatorValue.toString()) {
21+
return new FilterOperator(operatorValue);
22+
}
3423
}
24+
25+
throw new InvalidArgumentError(`The filter operator ${value} is invalid`);
3526
}
3627

3728
public isPositive(): boolean {

src/Contexts/Shared/domain/criteria/OrderType.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ export class OrderType extends EnumValueObject<OrderTypes> {
1313
}
1414

1515
static fromValue(value: string): OrderType {
16-
switch (value) {
17-
case OrderTypes.ASC:
18-
return new OrderType(OrderTypes.ASC);
19-
case OrderTypes.DESC:
20-
return new OrderType(OrderTypes.DESC);
21-
default:
22-
throw new InvalidArgumentError(`The order type ${value} is invalid`);
16+
for (const orderTypeValue of Object.values(OrderTypes)) {
17+
if (value === orderTypeValue.toString()) {
18+
return new OrderType(orderTypeValue);
19+
}
2320
}
21+
22+
throw new InvalidArgumentError(`The order type ${value} is invalid`);
2423
}
2524

2625
public isNone(): boolean {

0 commit comments

Comments
 (0)