Deaths thus far in 'A Song of Ice and Fire'

by David Thomas

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.17.7/js/jquery.tablesorter.min.js"></script>
<header>
<h1>A naively, and briefly, cobbled together table featuring all the characters that have so-far died in George R.R. Martin's <em>A Song of Ice and Fire</em> series</h1>
    <p>Source of inspiration taken from this question: <a href="http://scifi.stackexchange.com/q/66717/613">Is there a table of all deaths in A Song of Ice and Fire?</a>.</p>
    <p>Clicking on the table-headers will allow for sorting.</p>
</header>
<table id="asoiafDeaths">
    <thead>
        <tr>
            <th>Character</th>
            <th>title</th>
            <th>died in</th>
            <th></th>
        </tr>
    </thead>
    <tbody id="results">
    </tbody>
</table>

<ol id="source">
    <li data-book="A Game of Thrones">Ser Waymar Royce, Will, Gared, Lord Jon Arryn, Mycah, Lady, Ser Hugh Of The Vale, Jyk, Kurleket, Mohor, Morrec, Chiggen, Lharys, Heward, Wyl, Jory Cassel, Hali, Wallen, Stiv, Tregar, Ser Vardis Egen, Viserys Targaryen, King Robert Baratheon, Fat Tom, Varly, Cayn, Desmond, Hullen, Porther, Othor, Jafer Flowers, Lord Vance, Ser Raymun Darry, Masha Heddle, Ser Jaremy Rykker, Khal Ogo, Khal Fogo, Ulf Son Of Ulmar, Conn Son Of Coratt, Lord Halys Hornwood, Eddard Karstark, Torrhen Karstark, Daryn Hornwood, Quaro, Qotho, Haggo, Cohollo, Lord Eddard Stark, Septa Mordane, Vayon Poole, Rhaego, Eroeh, Khal Drogo, Lord Andros Brax, Mirri Maz Duur</li>
    <li data-book="A Clash of Kings">Maester Cressen, Praed, Ser Burton Crakehall, Barra, Allar Deem, Doreah, Woth, Dobber, Qyle, Murch, Koss, Gerren, Kurz, Reysen, Urreg, Yoren, Lommy Greenhands, King Renly Baratheon, Ser Robar Royce, Ser Emmon Cuy, Ser Stevron Frey, Ser Stafford Lannister, Chiswyck, Benfred Tallhart, Weese, High Septon (Fat One), Ser Preston Greenfield, Ser Aron Santagar, Alfyn Crowkiller, Ser Cortnay Penrose, Lord Leo Lefford, Lady Donella Hornwood, Alebelly, Mikken, Ser Amory Lorch,...

CSS

table {
    width: 90%;
    margin: 1em auto;
    border-collapse: collapse;
}
th {
    border-bottom: 2px solid #000;
    cursor: pointer;
}
th:first-letter {
    text-transform: uppercase;
}
th, td {
    text-align: left;
    background-color: inherit;
}
tr {
    box-shadow: inset 0 0 0 2px #fff;
}
tr.AGameOfThrones td:first-child {
    border-left: 5px solid #597579
}

tr.AGameOfThrones td:last-child {
    border-right: 5px solid #597579
}
tr.AClashOfKings td:first-child {
    border-left: 5px solid #D09916;
}

tr.AClashOfKings td:last-child {
    border-right: 5px solid #D09916;
}
tr.AStormOfSwords td:first-child {
    border-left: 5px solid #777777;
}
tr.AStormOfSwords td:last-child {
    border-right: 5px solid #777777;
}
tr.AFeastForCrows td:first-child {
    border-left: 5px solid #212121
}
tr.AFeastForCrows td:last-child {
    border-right: 5px solid #212121
}
tr.ADanceWithDragons td:first-child {
    border-left: 5px solid #F0863A;
}
tr.ADanceWithDragons td:last-child {
    border-right: 5px solid #F0863A;
}

tr.king,
tr.queen {
    background-color: rgba(255,0,0,0.8);
}

tr.prince,
tr.princess {
    background-color: rgba(255,0,0,0.5);
}

tr.lord,
tr.lady,
tr.khal,
tr.khaleesi {
    background-color: rgba(255,210,0, 0.8);
}
tr.ser {
    background-color: rgba(255,255,0, 0.8);
}

tr.maester {
    background-color: rgba(150,255,150,0.8);
}

tr.admiral {
    background-color: rgba(100,100,255, 0.8);
}

tr.captain {
    background-color: rgba(100,100,225,0.5)
}

.bookNumber {
    text-align: center;
}

td.bookNumber {
    color: #777;
}
td.bookNumber::before {
    content: 'Book '
}

#source {
    display: none;
}

JavaScript

var chars = [],
    titles = [
        'King',
        'Queen',
        'Khal',
        'Khaleesi',
        'Lord',
        'Lady',
        'Ser',
        'Prince',
        'Princess',
        'Maester',
        'Admiral',
        'Captain'
    ],
    bookOrder = [
        'A Game of Thrones',
        'A Clash of Kings',
        'A Storm of Swords',
        'A Feast for Crows',
        'A Dance with Dragons',
        'The Winds of Winter',
        'A Dream of Spring'
    ];

function charDeaths() {
    var names = this.textContent,
        reg = new RegExp(titles.join('|')),
        name, title, check, book = this.getAttribute('data-book'),
        namesArr = names.replace(/(, )/g, ',').split(','),
        sortingLetterIndex = function (charName) {
            return charName.charAt(reg.test(charName) ? charName.indexOf(' ') + 1 : 0);
        };
    namesArr.sort(function (a, b) {
        return sortingLetterIndex(a).charCodeAt(0) - sortingLetterIndex(b).charCodeAt(0);
    });
    namesArr.forEach(function (el, i, arr) {
        check = reg.test(el);
        title = check ? el.match(reg)[0] : false;
        name = check ? el.replace(/([a-zA-Z])+ /i, '') : el;
        chars.push({
            'name': name.trim(),
                'title': 'string' == typeof title ? title.trim() : title,
                'book': book
        });
        return el;
    });
}

$('#source li').each(charDeaths);

var frag = document.createDocumentFragment(),
    tr = document.createElement('tr'),
    td = document.createElement('td'),
    n, t, i, b, nTr, bookClass;

chars.forEach(function(a){
    nTr = tr.cloneNode();
    nTr.classList.add(a.book.replace(/( [a-zA-Z])/g, function (a) {
        return a.trim().charAt(0).toUpperCase();
    }));
    if (a.title) {
        nTr.classList.add(a.title.toLowerCase());
    }
    n = '<td class="character">' + a.name + '</td>';
    t = '<td class="charTitle">' + (a.title ? a.title : '') + '</td>';
    i = '<td class="bookTitle">' + a.book +...