Giter VIP home page Giter VIP logo

api-conversion-tool's Introduction

API Conversion Tool

tests

A function to convert an api provider's response data into in-house standard format.

Standard Solution Explanation

The standard solution loops the given data and builds a map of the individual attributes, then returns it.

const conversion = function (data) {
  const dataMap = {};

  data.attributes.forEach((attr) => {
    const key = attr["key"];
    const value = attr["value"];
    dataMap[key] = value;
  });

  return dataMap;
};

To build a hash/map is a common pattern, is algorithmically efficient and automatically scales for any amount of objects in the array. Variables have been made for readability.

Extended Solution Explanation

However, the happy path solution assumes the provider has given the information in the precise format of:

  • attributes array
  • containing objects
  • with keys
    • key
    • value

But we know apis can be updated, not follow their own schema or have unexpected data, so I've devised some further tests to give sufficient user feedback if there is an issue. This is not an exhaustive list but meant to show that consideration has been taken for edge cases. It means our code will inform us of any changes we may miss and in the worst case ensures we provide users with feedback that reflects we are aware of an issue on the provider's response.

Other considerations could include:

  • trimming whitespace from data
  • provide a list of missing/extra data keys received than expected
const conversion = function (data) {
  if (!data.attributes)
    return { message: "no 'attributes' array from provider" };

  const dataMap = {};
  let errorCount = 0;

  data.attributes.forEach((attr) => {
    const key = attr["key"];
    const value = attr["value"];

    if (!key || !value) {
      errorCount += 1;
    } else {
      dataMap[key] = value;
    }
  });
  if (errorCount > 0) {
    dataMap["unexpected_values"] = errorCount;
  }

  return dataMap;
};

Input - Output: Example Tests

The first test follows the happy path for the example given. I have then added further examples to highlight where I believe the data may not come back as expected and how to deal with that.

1. This is the standard happy path information given to convert.

input example:

{
  "attributes": [
    {
      "key": "email",
      "value": "[email protected]"
    },
    {
      "key": "name",
      "value": "James Dean"
    },
    {
      "key": "shoesize",
      "value": 10
    }
  ]
}

output example:

{
  "email": "[email protected]",
  "name": "James Dean",
  "shoesize": 10
}

2. array not called 'attributes'

input example:

{
  "newTitle": [
    {
      "key": "email",
      "value": "[email protected]"
    },
    {
      "key": "name",
      "value": "James Dean"
    },
    {
      "key": "shoesize",
      "value": 10
    }
  ]
}

output example:

{
  "message": "no 'attributes' array from provider"
}

3. missing expected "key" and "value" keys

input example:

{
  "attributes": [
    {
      "different": "email",
      "value": "[email protected]"
    },
    {
      "key": "name",
      "not_value": "James Dean"
    },
    {
      "key": "shoesize",
      "value": 10
    }
  ]
}

output example:

{
  "shoesize": 10,
  "unexpected_values": 2
}

api-conversion-tool's People

Contributors

josephclander avatar

Watchers

 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.