You can optimize the provided FORM object by:
- Using Template Literals: Replace the
.concat method with template literals for better readability and cleaner syntax.
- Creating a Helper Function: Introduce a helper function to reduce repetition when constructing the keys.
- Consistent Structure: Ensure all properties follow a consistent pattern for maintainability.
Here's the optimized version:
// Helper function to construct keys
const buildKey = (stepEnum, field) => field ? `${stepEnum}.${field}` : `${stepEnum}`;
// Exported FORM object
export const FORM = {
contract: {
effectDate: buildKey(AffaireStepEnum.CONTRACT, EContractFields.EFFECT_DATE),
contractType: buildKey(AffaireStepEnum.CONTRACT, EContractFields.CONTRACT_TYPE),
paymentMode: buildKey(AffaireStepEnum.CONTRACT, EContractFields.PAYMENT_MODE),
expirationDate: buildKey(AffaireStepEnum.CONTRACT, EContractFields.EXPIRATION_DATE),
},
activity: {
activitySector: buildKey(AffaireStepEnum.ACTIVITY, EActivityFields.SECTOR),
activity: buildKey(AffaireStepEnum.ACTIVITY, EActivityFields.ACTIVITY),
precision: buildKey(AffaireStepEnum.ACTIVITY, EActivityFields.PRECISION),
},
insuredObjects: {
isCustomer: buildKey(AffaireStepEnum.PRICE, 'isCustomer'),
turnover: buildKey(AffaireStepEnum.PRICE, 'turnover'),
ownershipType: buildKey(AffaireStepEnum.PRICE, 'ownershipType'),
holding: buildKey(AffaireStepEnum.PRICE, 'holding'),
content: buildKey(AffaireStepEnum.PRICE, 'content'),
merchandise: buildKey(AffaireStepEnum.PRICE, 'merchandise'),
hardware: buildKey(AffaireStepEnum.PRICE, 'hardware'),
elevatorAndGenerators: buildKey(AffaireStepEnum.PRICE, 'elevatorAndGenerators'),
questions: buildKey(AffaireStepEnum.PRICE, 'questions'),
},
guarantees: {
discount: buildKey(AffaireStepEnum.GUARANTEES, 'discount'),
guarantees: buildKey(AffaireStepEnum.GUARANTEES),
},
price: 'price', // If 'price' is a constant string, it can remain as is
};
Explanation of Changes
-
Template Literals: Replaced ''.concat(...) with ${...} for better readability.
// Before
effectDate: "".concat(AffaireStepEnum.CONTRACT, ".").concat(EContractFields.EFFECT_DATE),
// After
effectDate: `${AffaireStepEnum.CONTRACT}.${EContractFields.EFFECT_DATE}`,
-
Helper Function (buildKey): This function abstracts the key construction logic, handling cases where a field might not be provided.
const buildKey = (stepEnum, field) => field ? `${stepEnum}.${field}` : `${stepEnum}`;
-
Consistency: Ensured all keys are constructed using the buildKey function, making the structure consistent and easier to maintain.
-
Literal String (price): Kept the price property as a plain string since it doesn't follow the same pattern as others. If price is meant to follow the same pattern, you can adjust it accordingly:
price: buildKey(AffaireStepEnum.PRICE, 'price'),
Benefits of Optimization
- Readability: Template literals make the code easier to read and understand.
- Maintainability: Using a helper function reduces repetition, making it easier to manage and update the key construction logic.
- Scalability: Adding new fields or steps becomes simpler with the helper function in place.
This optimized approach ensures your FORM object is clean, maintainable, and scalable for future enhancements.