Read the guideline before start
Implement a function accepting 2 arguments: state and actions. The function
should change the state basing on the given actions array.
-
stateis an object. You are supposed to add, change, or delete its properties instead of creating a new object -
actionsis an array of objects. Each object in this array has the next properties:typecontains a string: either'addProperties','removeProperties'or'clear';- The second property of each object depends on
typeand may be one of the following:- if
typeisaddProperties, second property isextraData. It contains an object withkey: valuepairs to add to the state; - if
typeisremoveProperties, second property iskeysToRemove. It contains an array with the list of property names (keys) to remove from thestate; (Not existing properties should be ignored) - if
typeisclearyou should remove all the properties from the state. No second property in this case;
- if
Example of usage:
If state is {foo: 'bar', bar: 'foo'}, then
transformState(state, [
{
type: 'addProperties',
extraData: {
name: 'Jim',
hello: 'world',
}
},
{
type: 'removeProperties',
keysToRemove: ['bar', 'hello'],
},
{
type: 'addProperties',
extraData: { another: 'one' },
}
])
should modify the state, doing the following:
- add two properties to the
state - then remove keys
barandhellofrom thestate - and finally add another one property to the
state
After these operations the object state will have the following look
{
foo: 'bar',
name: 'Jim',
another: 'one',
}
Another example:
const state = { x: 1 };
transformState(state, [
{ type: 'addProperties', extraData: { yet: 'another property' } }
{ type: 'clear' },
{ type: 'addProperties', extraData: { foo: 'bar', name: 'Jim' } }
]);
// state === { foo: 'bar', name: 'Jim' }