I'm new to JS and am trying to write a perfectly clean source code. I'm using gulp to concatenate and minify my files, as well as removing comments. So the end result will always be the same in production environment: a single line without spaces. But for a dev environment, I'm trying to achieve clean code.
I'm building a small style guide template engine, based on Jekyll. But I need JS for several features, like dynamic styling of components. I might be overzealous but here is one of my modules with its source indentation and formatting:
$(function() {
/* Dynamic Styling ------------------ */
// Links
$('.field-style a')
.each(function() {
var base =
$(this)
.next('pre')
.children('code')
.text();
var replaced =
base
.replace(
/ |{|}|^a |\r?\n|\r/g,
'');
$(this)
.attr('style', replaced);
});
// Color box
$('.field-style .colored')
.each(function() {
var base =
$(this)
.next('pre')
.children('code')
.text();
var replaced =
base
.replace(
/\D+/g,
'');
$(this)
.css('background', '#' + replaced);
});
// LESS code blocks
if ($('.field-style .less')
.length) {
$('.field-style .less')
.html($('.field-style .less')
.html()
.replace(
/(none)|(decimal)|(inside)|(hidden)/g,
'<span class="value">$&</span>'));
$('.field-style .less')
.html($('.field-style .less')
.html()
.replace(
/(em)|(px)/g,
'<span class="unit">$&</span>'));
};
/* Code blocks trigger ------------------ */
// Button markup
var button =
'<button type="button" href="javascript:;"' +
'class="btn btn-primary show-code">' +
'Voir CSS</button>';
// Button generator
$('pre')
.before(button);
// Animation
$('.show-code')
.on('click', function() {
$(this)
.next('pre')
.slideToggle();
return false;
});
});
As you can see, I indented every method as well as every var
. I also break lines in replace method for each argument
What would you say about this indentation level?