JSFiddle - React, Tailwind, and code Playground

by darcyclarke

JavaScript

$.ajax({
    url: 'https://api.github.com/repos/MatthewWagerfield/Cinnamon/git/trees/master?recursive=1',
    type: 'GET',
    data: {},
    dataType: 'jsonp',
    success: function(response){
       var repo = {
                name : 'default',
                folders : [],
                files : []
            },
           test = function(arr, func){
                $.each(arr, function(){
                    if(func(this))
                        return true;
                });
                return false;
            };
        $.each(response.data.tree, function(){
            var path = this.path,
                arr = path.split('/'),
                file = arr[(arr.length - 1)],
                ctx = repo;
            arr = arr.slice(0,-1);
            if(arr.length >= 1){
                $.each(arr, function(i){
                    var name = this,
                        index = 0,
                        func = function(_this){ return (_this.name === name) };
                    if(!test(ctx.folders, func)){
                        ctx.folders.push({
                            name : name,
                            folders : [],
                            files : []
                        });
                        index = ctx.folders.length-1;
                    } else {
                        index = ctx.folders[name].index;
                    }
                    ctx = ctx.folders[index];
                });
            }
            ctx.files.push(file);
        });
        console.log(repo);
    },
    error: function(e){
        console.log(e);
    }
});