Home > Software design >  Convert array of objects containing bytes of user used languages in GitHub repo to a simpler array/o
Convert array of objects containing bytes of user used languages in GitHub repo to a simpler array/o

Time:11-11

I have a data which comes from GitHub Api https://docs.github.com/en/rest/repos/repos#list-repository-languages. It is a Array of objects of every repository of specified user, the first problem is that the value shown for each language is the number of bytes of code written in that language. So it looks like this:

data = [
  {
    repoName: "Docker_Kubernetes_Labs",
    languages: {
      JavaScript: 64677,
      HTML: 17832,
      CSS: 8154,
      Dockerfile: 2859,
    },
  },
  {
    repoName: "Frontend-HTTP-MQTT-Project",
    languages: {
      JavaScript: 59038,
      SCSS: 4732,
      CSS: 4281,
      HTML: 1925,
    },
  },
  {
    repoName: "frontend-prj",
    languages: {
      JavaScript: 16730,
      SCSS: 5388,
      HTML: 1721,
    },
  },
  {
    repoName: "JavaProjects",
    languages: {
      Java: 42040,
    },
  },
];

What I would like to change, is to sum all of the languages used, and display percentages so for all repositorys it would look like this:

{
    JavaScript: 0.55,
    SCSS: 0.1,
    CSS: 0.1,
    HTML: 0.1,
    Java: 0.1,
    Dockerfile: 0.05
}

I just want it to be summing up to 100% or 1 or 100, because I want to create a Pie-chart with these data, But also I want to be able to have a list of all repositories, so the final data would look like this:

expected_output = [
  {
    languages: {
      JavaScript: 0.55,
      SCSS: 0.1,
      CSS: 0.1,
      HTML: 0.1,
      Java: 0.1,
      Dockerfile: 0.05,
    },
    repositories: [
      "Docker_Kubernetes_Labs",
      "Frontend-HTTP-MQTT-Project",
      "frontend-prj",
      "JavaProjects",
    ],
  },
];

And for me it's to much, firstly I do not know how to conver those bytes into percentages so they sum up to 100, and secondly I do not know how to display this data in a simpler way.

CodePudding user response:

You can use Array.reduce() to aggregate all languages (sum total bytes per language), then you can calculate percentages using basic math.

const data = [{
    repoName: "Docker_Kubernetes_Labs",
    languages: {
      JavaScript: 64677,
      HTML: 17832,
      CSS: 8154,
      Dockerfile: 2859,
    },
  },
  {
    repoName: "Frontend-HTTP-MQTT-Project",
    languages: {
      JavaScript: 59038,
      SCSS: 4732,
      CSS: 4281,
      HTML: 1925,
    },
  },
  {
    repoName: "frontend-prj",
    languages: {
      JavaScript: 16730,
      SCSS: 5388,
      HTML: 1721,
    },
  },
  {
    repoName: "JavaProjects",
    languages: {
      Java: 42040,
    },
  },
];


// AGGREGATE LANGUAGES
const res = data.reduce((a, b) => {
  a.repositories.push(b.repoName)
  for (const lang of Object.keys(b.languages)) {
    if (a.languages.hasOwnProperty(lang)) {
      a.languages[lang]  = b.languages[lang]
    } else {
      a.languages[lang] = b.languages[lang]
    }
  }
  return a
}, { languages: {}, repositories: [] })


// GET TOTAL BYTES
const totBytes = Object.values(res.languages).reduce((a, b) => a   b)


// CALCULATE PERCENTAGES
for (const lang of Object.keys(res.languages)) {
  res.languages[lang] = res.languages[lang] / totBytes
}

console.log(res)

CodePudding user response:

Example:

JavaScript: 64677,
HTML: 17832,
CSS: 8154,
Dockerfile: 2859,
sum = 64677   17832   8154   2859 = 93522

JavaScript: 64677 / sum = 0.69,
HTML: 17832 / sum = 0.19,
CSS: 8154 / sum = 0.09,
Dockerfile: 2859 / sum = 0.03,

To do it in code I would use reduce

const languages = {
  JavaScript: 64677,
  HTML: 17832,
  CSS: 8154,
  Dockerfile: 2859,
}

const sum = Object.values(languages).reduce((sum, r) => sum   r)
const percentages = Object.entries(languages).reduce((obj, [key, value]) => {
  obj[key] = value / sum
  return obj
}, {})

console.log(percentages)

  • Related