Last active
July 12, 2021 14:17
-
-
Save kentcdodds/11293570 to your computer and use it in GitHub Desktop.
JSHint Options Chooser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Just paste this into the console on http://www.jshint.com/docs/options/ | |
(function() { | |
var i, row, link, span, extraCol, checkbox, value; | |
var rows = document.querySelectorAll('table.options tr'); | |
var links = document.querySelectorAll('table.options a'); | |
// add checkboxes | |
for (var i = 0; i < rows.length; i++) { | |
row = rows[i]; | |
extraCol = document.createElement('td'); | |
checkbox = document.createElement('input'); | |
value = document.createElement('input'); | |
value.value = 'true'; | |
value.style['margin-top'] = '16px'; | |
value.style.width = '40px'; | |
checkbox.type = 'checkbox'; | |
checkbox.dataset.option = row.querySelector('.name a').innerText; | |
extraCol.appendChild(checkbox); | |
extraCol.appendChild(value); | |
extraCol.style['padding-right'] = '26px'; | |
row.insertBefore(extraCol, row.firstChild); | |
} | |
// clear links to make tabbing through checkboxes quick and easy | |
for (i = 0; i < links.length; i++) { | |
span = document.createElement('span'); | |
link = links[i]; | |
span.innerHTML = link.innerHTML; | |
link.parentNode.replaceChild(span, link); | |
} | |
// add output stuff | |
var parent = document.querySelectorAll('table.options:last-child')[0].parentNode; | |
var output = document.createElement('textarea'); | |
output.setAttribute('rows', '14'); | |
output.setAttribute('cols', '26'); | |
output.style.display = 'block'; | |
output.style['margin-top'] = '16px'; | |
var button = document.createElement('button'); | |
button.innerText = 'Generate .jshintrc'; | |
button.addEventListener('click', function() { | |
var checkbox, val; | |
var checkboxes = document.querySelectorAll('table.options tr [type=checkbox]'); | |
var selectedOptions = {}; | |
for (i = 0; i < checkboxes.length; i++) { | |
checkbox = checkboxes[i]; | |
if (checkbox.checked) { | |
val = checkbox.nextSibling.value; | |
// Type coercion | |
if (!isNaN(val)) val = ~~val; | |
if (val === 'true') val = true; | |
if (val === 'false') val = false; | |
selectedOptions[checkbox.dataset.option] = val; | |
} | |
} | |
output.value = JSON.stringify(selectedOptions, null, 2); | |
}); | |
parent.appendChild(button); | |
parent.appendChild(output); | |
})(); |
CommandoBM
commented
Jul 12, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment