HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

JS Data Fetcher and Stripper for Statistics App

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
appstripperstatisticsforanddatafetcher

Problem

This is pretty much the first actual (not "hello world") program I'm writing. It's a rewrite of this app and my aim was to increase the fetch speed, and increase the cache storage efficency. I've more or less done this through multiple simultaneous fetches and stripping and compressing the game data.

```
document.getElementById("btn1").addEventListener("click", function () {fetchOGS(document.getElementById("uname").value); });

if (location.hash.split("=")[0] === "#username") {
fetchOGS(location.hash.split("=")[1].split("&")[0]);
}

var LZString; //Comes from a compression library
var fdata = {}; //Final processed data that is to be graphed

function fetchOGS(user) {
var username;
var userID;
var udata = Number.isNaN(parseInt(user)) ?
fetch("https://online-go.com/api/v1/players/?username__icontains=" + user + "&format=json"):
fetch("https://online-go.com/api/v1/players/?id__icontains=" + user + "&format=json");

udata
.then(jsonHttpHandler)
.then(
(response) => {
console.log(response);
username = response.results[0].username;
userID = response.results[0].id;
fdata[username] = {};
fdata[username].user = response.results[0];
return username;
})
.then(
(username) => {
console.log("Reading Local Storage...");
fdata[username] = localStorage.getItem(username) === null?
console.log("Cache miss")||{}:
JSON.parse(LZString.decompressFromUTF16(localStorage.getItem(username)));
//Initialize data arrays, if they aren't initialized yet
fdata[username]=fdata[username]?fdata[username]:{};
fdata[username].game=fdata[username].game?fdata[username].game:[]; fdata[username].score=fdata[username].score?fdata[username].score:[];

appendLocalStora

Solution

I'm not a JavaScript expert, and overall it looks good, but there are a few things I would change (mostly style):

fdata[username].game.push(gameID);nament.push(true);


I almost missed that nament.push(true); call, let's break that to a new line. You can always run a minify-script later to shrink the byte-size.

I would alter this slightly:

var udata = Number.isNaN(parseInt(user)) ?
    fetch("https://online-go.com/api/v1/players/?username__icontains=" + user + "&format=json"):
    fetch("https://online-go.com/api/v1/players/?id__icontains=" + user + "&format=json");


It's hard to read at the moment, a lot goes on:

var fetchParam = Number.isNaN(parseInt(user)) ? "username" : "id";
var udata =
    fetch("https://online-go.com/api/v1/players/?" + fetchParam + "__icontains=" + user + "&format=json");


It also shrinks the byte-size, which is good.

fdata[username].game=fdata[username].game?fdata[username].game:[];                    fdata[username].score=fdata[username].score?fdata[username].score:[];


Why is that a single line? Let's break it up so that we don't miss that bit that gets hidden by scrolling.

Every single URL call starts with:

https://online-go.com/api/v1/players/


And contains:

format=json


Let's encapsulate those to variables and use them:

var urlBase = "https://online-go.com/api/v1/players/";
var format = "format=json";

var fetchParam = Number.isNaN(parseInt(user)) ? "username" : "id";
var udata = fetch(urlBase + "?" + fetchParam + "__icontains=" + user + "&" + format);


We can avoid repetition here:

fdata[username].score.push(
    Number.isNaN(parseInt(response.results[i].outcome[0])) ?
        response.results[i].outcome.split(" ")[0] :
        response.results[i].outcome[0]
);


To:

var outcome = response.results[i].outcome;
fdata[username].score.push(
    Number.isNaN(parseInt(outcome[0])) ? outcome.split(" ")[0] : outcome[0];
};


Shorter, sweeter and more maintainable.

Overall, excellent start. I hope someone who is much more acquainted with JavaScript than I comes along and gives you more advice. :)

Code Snippets

fdata[username].game.push(gameID);nament.push(true);
var udata = Number.isNaN(parseInt(user)) ?
    fetch("https://online-go.com/api/v1/players/?username__icontains=" + user + "&format=json"):
    fetch("https://online-go.com/api/v1/players/?id__icontains=" + user + "&format=json");
var fetchParam = Number.isNaN(parseInt(user)) ? "username" : "id";
var udata =
    fetch("https://online-go.com/api/v1/players/?" + fetchParam + "__icontains=" + user + "&format=json");
fdata[username].game=fdata[username].game?fdata[username].game:[];                    fdata[username].score=fdata[username].score?fdata[username].score:[];
https://online-go.com/api/v1/players/

Context

StackExchange Code Review Q#152699, answer score: 4

Revisions (0)

No revisions yet.