Code Monkey home page Code Monkey logo

js_stateful-object's Introduction

Transforming state

Read the guideline before start

❗️❗️❗️ DON'T FORGET TO PROOFREAD YOUR CODE WITH CHECKLIST BEFORE SENDING YOUR PULL REQUEST❗️❗️❗️

Task description

Implement a function accepting 2 arguments: state and actions. The function should change the state basing on the given actions array.

  • state is an object. You are supposed to add, change, or delete its properties instead of creating a new object

  • actions is an array of objects. Each object in this array has the next properties:

    • type contains a string: either 'addProperties', 'removeProperties' or 'clear';
    • The second property of each object depends on type and may be one of the following:
      • if type is addProperties, second property is extraData. It contains an object with key: value pairs to add to the state;
      • if type is removeProperties, second property is keysToRemove. It contains an array with the list of property names (keys) to remove from the state; (Not existing properties should be ignored)
      • if type is clear you should remove all the properties from the state. No second property in this case;

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 bar and hello from the state
  • 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' }

js_stateful-object's People

Contributors

denys-cheporniuk avatar solaryasha avatar yuriiholiuk avatar danheim avatar danmysak avatar varseniuk avatar mgrinko avatar vpolets avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.