JSFiddle - React, Tailwind, and code Playground

by nickkell

HTML

<script src="http://underscorejs.org/underscore.js"></script>

JavaScript

var windowed = function (arr, size) {
    var len = arr.length,
        resized = [],
        loop = function (currentIndex, currentSize) {
            if (currentSize === size) {
                loop(currentIndex + size, 0);
            } else if (currentIndex + currentSize < len) {
                if (resized[currentIndex]) {
                    resized[currentIndex].push(arr[currentIndex + currentSize]);
                } else {
                    resized[currentIndex] = [arr[currentIndex + currentSize]];
                }
                loop(currentIndex, currentSize + 1);
            }
        };

    if (len > 0 && size > 2) {
        loop(0, 0);
    } else {
        resized = arr;
    }

    return resized;
};

var reverse = function (arr) {
    return _.reduce(arr, function (acc, next) {
        return [].concat.apply([next], acc);
    }, []);
};

var orderedByCount = function (theseResults) {
    // theseResults			
    // |> List.rev
    var rev = reverse(theseResults);
    // |> Seq.take 1
    var take1 = rev.slice(0, 1);
    // |> Seq.collect id
    var flatten = _.flatten(take1);
    // |> Seq.sortBy(fun i -> i.count)
    var sortBy = _.sortBy(flatten, 'count');
    // |> List.ofSeq
    // |> List.rev
    
    var rev = reverse(sortBy);
    // |> List.map(fun i -> i.name)
    var map = _.pluck(rev, 'name');
    // |> Seq.windowed 1
    var _windowed = windowed(map, 2);
    //|> Seq.map(fun window ->
    //    theseResults
    //    |> List.map(fun x -> 
    //        x |> List.filter(fun n -> 
    //            window |> Array.exists(fun wn -> wn = n.name))))
    var map = _.map(_windowed, function (window) {
        return _.map(theseResults, function (x) {
            return _.filter(x, function (n) {
                return _.contains(window, n.name);
            });
        });
    });
    // |> Seq.collect id
    //var flatten = _.flatten(map);
    // |> Seq.collect id    
    //var flatten = _.flatten(map);
    // |> List.ofSeq
    return...