concurso de función garble

Recuerde ese mensaje a distancia que apunta cómo:

En busca de una investigación en Cmabrigde Uinervtisy, no se pierde nada en lo que se refiere a las letras en un mundo, lo único que debe saber es que el primer y el último lugar están en la parte derecha. El primer conjunto puede ser una tarea completa y usted puede evaluarlo por completo. Tihs es bcuseae the huamn mnid deos, no crió ervey lteter por istlef, sino el mundo como un wlohe.

De todos modos, estoy tratando de hacer una función que haga eso a una página entera. Hay algunas reglas para esta función.

menos de 4 caracteres se quedan solos.los caracteres no alfanuméricos no cuentan como parte de la palabra.las palabras con guiones son realmente dos palabraslas palabras deben ser confusas si longitud> = 4 (no puede ser como el original)El primer y el último carácter permanecen igual y solo los caracteres del medio se confunden (Gracias Hersheezy)el texto siempre debe ser aleatorio y producir un empate único en cada ejecuciónJavaScript puro e itera en todos los nodos de textoEl código más dulce más corto gana.

De todos modos, parece bastante simple de implementar, ¿qué hay de comenzar un concurso para ver quién podría hacer el código más claro y claro para llevar a cabo esta tarea? Siéntase libre de pedir prestado sin el reconocimiento de mi código (definitivamente tengo)

Si me perdí algo, agréguelo en los comentarios. De todos modos, trabajé en ello muy hackly y aquí estoy mostrando mi trabajo menos que normal

MANIFESTACIÓN

var i, j, words, textNodes, punct = /[^a-zA-Z0-9]/;

Array.prototype.shuffle = function() {
    for (var i = 0; i < this.length; i++) {
        var j = i;
        while (j == i) {
            j = Math.floor(Math.random() * this.length);
        }
        var tmp = this[i];
        this[i] = this[j];
        this[j] = tmp;
    }
    return this;
};

String.prototype.shuffle = function() {
    return this.split('').shuffle().join('');
};

function transverse(element, array) {
    if (!array) array = [];
    if (element.nodeType === 3) {
        array.push(element);
    } else {
        for (var i = 0; i < element.childNodes.length; i++) {
            transverse(element.childNodes[i], array);
        }
    }
    return array;
}

function garble(str) {
    if (!str) return '';
    str = str.trim();
    if (/-/.test(str)) {
        str = str.split('-');
        for (var i = 0; i < str.length; i++) {
            str[i] = garble(str[i]);
        }
        return str.join('-')
    }
    if (punct.test(str.charAt(0))) {
        return str.charAt(0) + garble(str.slice(1));
    }
    if (punct.test(str.charAt(str.length - 1))) {
        return garble(str.slice(0, -1)) + str.charAt(str.length - 1);
    }
    if (str.length < 4) return str;
    if (str.length === 4) return str.charAt(0) + str.charAt(2) + str.charAt(1) + str.charAt(3)
    return str.charAt(0) + str.substr(1, str.length - 2).shuffle() +
        str.charAt(str.length - 1);
}


window.onload = function() {
    textNodes = transverse(document.documentElement);
    for (i = 0; i < textNodes.length; i++) {
        words = textNodes[i].data.split(' ');
        for (j = 0; j < words.length; j++) {
            words[j] = garble(words[j]);
        }
        textNodes[i].data = words.join(' ');
    }
};

Respuestas a la pregunta(4)

Su respuesta a la pregunta