Skip to content

Instantly share code, notes, and snippets.

@Rockyspade
Forked from kentcdodds/chooser.js
Created August 4, 2016 05:22
Show Gist options
  • Save Rockyspade/8a13fd609ffc4b379aaf4f8cc8272077 to your computer and use it in GitHub Desktop.
Save Rockyspade/8a13fd609ffc4b379aaf4f8cc8272077 to your computer and use it in GitHub Desktop.
JSHint Options Chooser
// 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);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment