User:Chlod/Scripts/StormCountTally.js

// Storm Count Tally // Author: Chlod // Version: 1.0.0-REL

// Counts the total amount of storms on the WPTC Storm Count Tally. This makes // counting typhoons easier and require less time, as long as the table is kept // up to date.

mw.loader.using( 'oojs-ui-core' ).done(function {	// Find our tally	var tally = document.getElementById("wptc-sct-table");	var button = document.getElementById("wptc-sct-button");	// Doesn't have a tally. Skip.	if (tally == null || button == null) return;	// Instance counting function	function instances(arr) { var a = [], b = [], prev; arr.sort; for (var i = 0; i < arr.length; i++) { if (arr[i] !== prev) { a.push(arr[i]); b.push(1); } else { b[b.length - 1]++; } prev = arr[i]; } return [a, b]; }

// Total counting function function count(a) { var b = a.split("\t"); b = b.map(c => c.trim); b = b.filter(c => `${c}`.length > 0); var r = instances(b); console.dir({"TD": +(r[1][0]) + +(r[1][1]) + +(r[1][2]), "TS": +(r[1][1]) + +(r[1][2]), "TY": +(r[1][2])}) } function calculate { var counts = {}; var rows = document.querySelectorAll("[data-wptc-stc]"); for (var row of rows) { var children = [...row.children].slice(1); var rowName = row.getAttribute("data-wptc-stc").trim; var countsString = ""; for (var child of children) { countsString += `${child.innerText.trim}\t`; }			counts[rowName] = count(countsString); }		alert(JSON.stringify(counts)); }	var b = new OO.ui.ButtonWidget( { 		label: "Calculate"	} ); b.$element[0].setAttribute("type", "button"); b.$element[0].onclick = calculate; button.appendChild(b.$element); });