Prompt Detail:
Certainly! Here's an updated example with two sets of transformation rules, transformationRules1
and transformationRules2
:
javascriptconst USER_ID = 'id';
const USER_NAME = 'name';
const USER_EMAIL = 'email';
const USER_ADDRESS = 'address';
const USER_ADDRESS_STREET = 'street';
const USER_ADDRESS_CITY = 'city';
const USER_ADDRESS_STATE = 'state';
const USER_ADDRESS_COUNTRY = 'country';
const USER_ORDERS = 'orders';
const ORDER_ID = 'id';
const ORDER_PRODUCT = 'product';
const ORDER_PRICE = 'price';
const transformNameToUppercase = (name) => name.toUpperCase();
const transformCityToUppercase = (city) => city.toUpperCase();
const applyPriceIncrease = (price) => price * 1.1;
const converge = (rules) => (obj) =>
Object.entries(rules).reduce((acc, [key, transform]) => {
acc[key] = transform(obj[key]);
return acc;
}, {});
const user = {
[USER_ID]: 1,
[USER_NAME]: 'John Doe',
[USER_EMAIL]: '[email protected]',
[USER_ADDRESS]: {
[USER_ADDRESS_STREET]: '123 Main Street',
[USER_ADDRESS_CITY]: 'New York',
[USER_ADDRESS_STATE]: 'NY',
[USER_ADDRESS_COUNTRY]: 'USA'
},
[USER_ORDERS]: [
{
[ORDER_ID]: 1,
[ORDER_PRODUCT]: 'Phone',
[ORDER_PRICE]: 999
},
{
[ORDER_ID]: 2,
[ORDER_PRODUCT]: 'Laptop',
[ORDER_PRICE]: 1999
}
]
};
// Define transformation rules as hash maps
const transformationRules1 = {
[USER_NAME]: transformNameToUppercase,
[`${USER_ADDRESS}.${USER_ADDRESS_CITY}`]: transformCityToUppercase
};
const transformationRules2 = {
[`${USER_ORDERS}[].${ORDER_PRICE}`]: applyPriceIncrease
};
// Apply the transformation rules using the converge function
const transformUser1 = converge(transformationRules1);
const transformUser2 = converge(transformationRules2);
const transformedUser1 = transformUser1(user);
const transformedUser2 = transformUser2(user);
console.log('Transformed User 1:', transformedUser1);
console.log('Transformed User 2:', transformedUser2);
In this updated example, I've defined two sets of transformation rules, transformationRules1
and transformationRules2
, using hash maps.
transformationRules1
consists of the following transformations:
USER_NAME
field is transformed to uppercase.USER_ADDRESS_CITY
field is transformed to uppercase.
transformationRules2
consists of the following transformation:
- Each
ORDER_PRICE
field within theUSER_ORDERS
array is increased by 10%.
Both sets of transformation rules are passed to the converge
function to create separate transformation functions: transformUser1
and transformUser2
. Each transformation function can be used to transform the user
object independently.
The transformed objects, transformedUser1
and transformedUser2
, are obtained by invoking the respective transformation functions.
Finally, the transformed objects are logged to the console separately for each set of transformation rules.
Feel free to modify the transformation rules or add more rules to suit your requirements.