<commit_before>// visualize.js -- Code for wrangling ETF data.

// ----------------------------------------------------------------------------
// Globals
// ----------------------------------------------------------------------------

// Initialized when data is loaded.
var all_etfs = null;

// Modified as user adds/removes ETFs from the ETF drawer.
var selected_etfs = null;


// ----------------------------------------------------------------------------
// Dataset wrangling
// ----------------------------------------------------------------------------

var parseDate = d3.timeParse("%m/%d/%Y");

// This function loads our CSV dataset, parsing into per-row JSON objects.
function load_dataset(callback) {
    d3.json("data/historical_nav_details.json", function(d) {
        // Build a JSON object for each row.
        var data = {};

        for (var key in d) {
            if (d.hasOwnProperty(key)) {
                data[key] = {
                    ticker: key,
                    proshares_name: d[key]['Name'],
                    holdings: d[key]['Holdings'],
                    holdings_long: d[key]['HoldingLong'],
                    holdings_short: d[key]['HoldingsShort'],
                    sectors: d[key]['Sectors'],
                    sectors_long: d[key]['SectorsLong'],
                    sectors_short: d[key]['SectorsShort'],
                    sub_sectors: d[key]['SubSectors'],
                    countries: d[key]['Countries']
                };
            }
        }
        // Set the resulting list of objects to a global.
        all_etfs = data;

        // If a callback was provided, call it.
        if (typeof(callback) === "function" && callback) {
            callback();
        }
    });
}

// This function loads a specific etf, parsing the arrays properly
function load_etf(etfTicker, callback) {
    // Upper case Ticker
    etfTicker = etfTicker.toUpperCase();

    // Check if ETF already loaded. If so, skip loading again
    if (all_etfs.hasOwnProperty(etfTicker)) {
        if (all_etfs[etfTicker].hasOwnProperty('NAV')) {
            // If a callback was provided, call it.
            if (typeof(callback) === "function" && callback) {
                callback();
            }
            return;
        }
    } else {
        // Don't populate if etf is not in all_etfs.
        return;
    }

    d3.json("data/json_files_tickers/" + etfTicker + ".json", function(d) {
        // Build a JSON object for ticker.
        var date = [];
        var nav = [];
        var yhv = [];

        var dates = d['Date'];
        dates.forEach(function(o) {
            date.push(parseDate(o))
        });
        var navs = d['NAV'];
        navs.forEach(function(o) {
            nav.push(+o)
        });
        var yhvs = d['YHV'];
        yhvs.forEach(function(o) {
            yhv.push(+o)
        });

        all_etfs[etfTicker]['date'] = date;
        all_etfs[etfTicker]['nav'] = nav;
        all_etfs[etfTicker]['yhv'] = yhv;

        // If a callback was provided, call it.
        if (typeof(callback) === "function" && callback) {
            callback();
        }
    });
}


// ----------------------------------------------------------------------------
// Miscellaneous functions
// ----------------------------------------------------------------------------

// Function to show unique ProShares/Ticker names.
function display_proshares() {
    // Display ProShares names in console.
    // ERROR HERE HAD TO COMMENT OUT var names = all_etfs.map(function(d) { return d.proshares_name });
    // Filter to just the unique values.
    // ERROR HERE HAD TO COMMENT OUTconsole.log(d3.set(names).values());
    // Repeat the process for ticker names.
    // ERROR HERE HAD TO COMMENT OUTvar tickers = all_etfs.map(function(d) { return d.ticker });
    // ERROR HERE HAD TO COMMENT OUTconsole.log(d3.set(tickers).values());
}


// ----------------------------------------------------------------------------
// Initialization functions
// ----------------------------------------------------------------------------

// This function runs once the document has loaded/rendered.
$(document).ready(function () {
    // Use dumb-but-effective callback chaining to get things done.
    load_dataset(function (){
        console.info(all_etfs);
        display_proshares();
    });
});<commit_msg>Prevent undefined or null values from being added to an object when loading the dataset. Makes it easy later to use .hasOwnProperty() on the allEtfs. Also renamed variables to be JavaScript formatting.<commit_after>// visualize.js -- Code for wrangling ETF data.

// ----------------------------------------------------------------------------
// Globals
// ----------------------------------------------------------------------------

// Initialized when data is loaded.
var allETFs = null;

// Modified as user adds/removes ETFs from the ETF drawer.
var selectedETFs = null;


// ----------------------------------------------------------------------------
// Dataset wrangling
// ----------------------------------------------------------------------------

var parseDate = d3.timeParse("%m/%d/%Y");

// This function loads our CSV dataset, parsing into per-row JSON objects.
function loadDataset(callback) {
    d3.json("data/historical_nav_details.json", function(d) {
        // Function to help add values to object so no empty or undefined values are added
        function addValue(value, tickerObject, key) {
            if (value !== undefined && value !== null && Object.keys(value).length > 0) {
                tickerObject[key] = value
            }
        }

        // Build a JSON object for each row.
        var data = {};

        for (var key in d) {
            if (d.hasOwnProperty(key)) {
                var tickerObj = {
                    ticker: key,
                    proshares_name: d[key]['Name']
                };

                addValue(d[key]['Holdings'], tickerObj, 'holdings');
                addValue(d[key]['HoldingLong'], tickerObj, 'holdingsLong');
                addValue(d[key]['HoldingsShort'], tickerObj, 'holdingsShort');
                addValue(d[key]['Sectors'], tickerObj, 'sectors');
                addValue(d[key]['SectorsLong'], tickerObj, 'sectorsLong');
                addValue(d[key]['SectorsShort'], tickerObj, 'sectorsShort');
                addValue(d[key]['SubSectors'], tickerObj, 'subSectors');
                addValue(d[key]['Countries'], tickerObj, 'countries');

                data[key] = tickerObj;
            }
        }
        // Set the resulting list of objects to a global.
        allETFs = data;

        // If a callback was provided, call it.
        if (typeof(callback) === "function" && callback) {
            callback();
        }
    });
}

// This function loads a specific etf, parsing the arrays properly
function loadETF(etfTicker, callback) {
    // Upper case Ticker
    etfTicker = etfTicker.toUpperCase();

    // Check if ETF already loaded. If so, skip loading again
    if (allETFs.hasOwnProperty(etfTicker)) {
        if (allETFs[etfTicker].hasOwnProperty('NAV')) {
            // If a callback was provided, call it.
            if (typeof(callback) === "function" && callback) {
                callback();
            }
            return;
        }
    } else {
        // Don't populate if etf is not in allETFs.
        return;
    }

    d3.json("data/json_files_tickers/" + etfTicker + ".json", function(d) {
        // Build a JSON object for ticker.
        var date = [];
        var nav = [];
        var yhv = [];

        var dates = d['Date'];
        dates.forEach(function(o) {
            date.push(parseDate(o));
        });
        var navs = d['NAV'];
        navs.forEach(function(o) {
            nav.push(+o);
        });
        var yhvs = d['YHV'];
        yhvs.forEach(function(o) {
            yhv.push(+o);
        });

        allETFs[etfTicker]['date'] = date;
        allETFs[etfTicker]['nav'] = nav;
        allETFs[etfTicker]['yhv'] = yhv;

        // If a callback was provided, call it.
        if (typeof(callback) === "function" && callback) {
            callback();
        }
    });
}


// ----------------------------------------------------------------------------
// Miscellaneous functions
// ----------------------------------------------------------------------------

// Function to show unique ProShares/Ticker names.
function displayProshares() {
    // Display ProShares names in console.
    // ERROR HERE HAD TO COMMENT OUT var names = allETFs.map(function(d) { return d.proshares_name });
    // Filter to just the unique values.
    // ERROR HERE HAD TO COMMENT OUTconsole.log(d3.set(names).values());
    // Repeat the process for ticker names.
    // ERROR HERE HAD TO COMMENT OUTvar tickers = allETFs.map(function(d) { return d.ticker });
    // ERROR HERE HAD TO COMMENT OUTconsole.log(d3.set(tickers).values());
}


// ----------------------------------------------------------------------------
// Initialization functions
// ----------------------------------------------------------------------------

// This function runs once the document has loaded/rendered.
$(document).ready(function () {
    // Use dumb-but-effective callback chaining to get things done.
    loadDataset(function (){
        console.info(allETFs);
        displayProshares();
    });
});
{'fraction_non_alphanumeric': 0.24345883854499042, 'fraction_numerical': 0.001169963837481387, 'mean_word_length': 1.3829194120628485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4843904', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2422, 'n_words': 658}