Coder Social home page Coder Social logo

js_stateful-clones's Introduction

Cloning and transforming state

Read the guideline before start

Task description

Write a function transformStateWithClones that take a state object and an actions array and return an array of the same length as actions containing all previous versions of the state. Each element of the resulting array has to represent the state produced by the next operation.

IMPORTANT! You must not modify the initial state object in any way!

  • state is an initial object. It should always remain the same.

  • 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, the second property is extraData. It contains an object with key: value pairs to add to the state;
      • if type is removeProperties, the 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 create an empty state object. No second property in this case;

Example of usage:

If state is {foo: 'bar', bar: 'foo'}, then

transformStateWithClones(state, [
  {type: 'addProperties', extraData: {name: 'Jim', hello: 'world'}},
  {type: 'removeProperties', keysToRemove: ['bar', 'hello']},
  {type: 'addProperties', extraData: {another: 'one'}}
])

must return the following array:

[
  {foo: 'bar', bar: 'foo', name: 'Jim', hello: 'world'},
  {foo: 'bar', name: 'Jim'},
  {foo: 'bar', name: 'Jim', another: 'one'}
].

The state object itself should not be modified and must remain {foo: 'bar', bar: 'foo'}.

Then after calling

transformStateWithClones(state, [
  {type: 'addProperties', extraData: {yet: 'another property'}}
  {type: 'clear'},
  {type: 'addProperties', extraData: {foo: 'bar', name: 'Jim'}}
])

we must get

[
  {foo: 'bar', bar: 'foo', yet: 'another property'},
  {},
  {foo: 'bar', name: 'Jim'}
].

the state variable must still contain {foo: 'bar', bar: 'foo'}.

js_stateful-clones's People

Contributors

danheim avatar danmysak avatar denys-cheporniuk avatar mgrinko avatar solaryasha avatar varseniuk avatar yuriiholiuk 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.