/reverse-nested-object

Reverse nested object with single key in every layer

Primary LanguageJavaScript

Reverse Nested Objects

Question

// Please write a function to reverse the following nested input value into output value

// Input:
let inputValue = {
  hired: {
    be: {
      to: {
        deserve: 'I'
      }
    }
  }
};

// Output:
let outputValue = {
  I: {
    deserve: {
      to: {
        be: 'hired'
      }
    }
  }
};

Unit Test

The unit test contains 5 scenerios:

☑️ Reverse nested objects with single key in every layer

☑️ Empty object at the innermost layer will be ignore

☑️ Return empty object when inputValue is an empty object

☑️ Throw error when the innermost layer is an array

☑️ Throw error when inputValue is an array

Test Result

test-result

How to run the test

Please follow these steps to see the test results:

  1. Download

    git clone https://github.com/Kaikai8888/reverse-nested-object.git
    
  2. Install Jest

    npm install
  3. Run test

    npm run test