JSFiddle - React, Tailwind, and code Playground

by nate

JavaScript

// An array of unsorted path objects
// (These are ways of describing a position in a Go game
// tree that can include variations)

var paths = [
    { m: 10 },
    { m: 6 },
    { m: 24 },
    { m: 222, 219: 1, 220: 2, 221: 1 },
    { m: 222, 219: 1, 220: 1, 221: 1 },
    { m: 222, 219: 1, 220: 1, 221: 2 },
    { m: 222, 219: 2, 220: 1, 221: 1 },
    { m: 224, 219: 2, 220: 1, 221: 1 },
    { m: 226, 219: 2, 220: 1, 221: 1 },
    { m: 106 },
    { m: 107, 106: 2 },
    { m: 107 },
    { m: 222 },
    { m: 214 },
    { m: 28, 22: 1 },
    { m: 28, 10: 1, 12: 2 },
    { m: 28, 10: 1, 8: 2, 9:2 },
    { m: 28, 12: 1, 8: 1},
    { m: 41 },
    { m: 36, 34: 2 }
];

// Find the lowest key value
// If no key values are numbers, returns undefined
minKey = function (obj) {
    var min;
    
    Object.keys(obj).forEach(function (key, i) {
        var num = Number(key);
        
        if (!isNaN(num)) {
            min = (!min) ? num : Math.min(min, num);
        }
    });

    return min;
};

function comparePaths(a, b) {       
    var aM = minKey(a) || a.m;
    var bM = minKey(b) || b.m;
    
    function compareVariations () {
        if (
            typeof a[aM] === 'undefined'
            && typeof b[bM] === 'undefined'
        ) {
            // These are the same move
            return 0;
        }
        
        var aKeys = Object.keys(a).filter(function(key) { key !== 'm'; });
        var bKeys = Object.keys(b).filter(function(key) { key !== 'm'; });
        
        function compareKeys(aKeys, bKeys) {
            var aKey = (aKeys.length) ? aKeys[0] : 0;
            var bKey = (bKeys.length) ? bKeys[0] : 0;
            
            // If the lowest keys are different, use them to sort
            if (aKey !== bKey) {
                return aKey - bKey;
            } else {
                // If the VALUES of the lowest keys are different,
                // use them to sort
                if (a[aKey] !== b[bKey]) {
                    return a[aKey] -...