Skip to content

Instantly share code, notes, and snippets.

@gistfrojd
Last active March 24, 2016 07:48
Show Gist options
  • Save gistfrojd/a0fd9a29e73c09fba222 to your computer and use it in GitHub Desktop.
Save gistfrojd/a0fd9a29e73c09fba222 to your computer and use it in GitHub Desktop.
Javascript Styleguide (these files has moved to https://github.com/Frojd/Manual)
/**
* Example of a Email utils library
*
* Usage:
* var EmailUtils = require("./utils");
* EmailUtils.validateEmail("hello@frojd.se");
*/
"use strict";
function validateEmail(email) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(email);
}
function validateEmailList(list) {
var l = list.length;
var email;
while (l--) {
email = list[l];
if (! validateEmail(email)) {
return false;
}
}
return true;
}
module.exports = {
validateEmail: validateEmail,
validateEmailList: validateEmailList
};
/**
* A simple store class that can both set and get values.
*/
"use strict";
var Gzip = require("gzip");
function Store(options) {
this.store = {};
}
Store.prototype.get = function(key) {
return Gzip.unpack(this.store[key]);
}
Store.prototype.set = function(key, value) {
return this.store[key] = Gzip.pack(value);
}
module.exports = Store;
/**
* Example clickhandler
*/
"use strict";
var $ = require("jquery");
function ToggleView(options) {
var self = this;
self.selector = options.selector;
self.el = $(self.selector);
self.clickCounter = 0;
$(self.selector).on("click", $.proxy(self.clickHandler, self));
}
ToggleView.prototype.clickHandler = function(e) {
var self = this;
self.toggle();
};
ToggleView.prototype.toggle = function() {
var self = this;
self.clickCounter++;
console.log("clicked: ", self.clickCounter);
if(self.clickCounter > 5) {
self.unbindToggle();
console.log("clickHandler is unbinded");
}
};
ToggleView.prototype.unbindToggle = function() {
var self = this;
self.el.unbind("click", self.clickHandler);
};
module.exports = ToggleView;
/**
* Example of a project start file.
*/
/* globals alert */
"use strict";
var MegaMenu = require("./views/megamenu");
var Utils = require("./utils");
if (! Utils.isIE()) {
var megaMenu = new MegaMenu({selector: ".js-megamenu"});
} else {
alert("Step away from the computer!");
}
/**
* Example of a project start file
*/
"use strict";
var $ = require("jquery");
var ToggleView = require("./views/toggleview");
if($(".js-toggle").length) {
var toggleView = new ToggleView({selector: ".js-toggle"});
}

Code guide

Editing

  • For indentation use 4 spaces

  • Always try to limit your lines to 80 characters unless it breaks readability, then it can go up to a max of 120 characters

  • End every file with a empty line at the bottom

  • Avoid extraneous whitespace

      YES:    spam(cat[1], {dog: 2})
      NO:     spam( cat[ 1 ], { dog: 2 } )
    
  • Use early return when creating methods

  • Make sure everything passes hinting

Naming

  • When creating a locally referenced scope variable use the variable name self

  • Always use camelCase

  • When naming your modules/packages, only use lowercase without any -_. character. Example: /awesomepackage/cutecat.js

      YES: `/awesomepackage/cutecat.js`
      NO: `/awesomePackage/cuteCat.js`
    

Imports

  • Place all imports at the top

  • Separate top imports with your implementation with two blank lines

  • Use a blank line between node and distributed imports and project specific

  • Imports should be declared as vars on a new line. Example:

     var env = require("node-env-file");
     var winston = require("winston");
    
     var fundamentet = require("./fundamentet");
    
  • When organizing imports, use the following order:

    1. Core modules shipped in Node
    2. Third part modules
    3. Project modules

Logging

  • Always remove console.log messages when checking in your code (unless it is really neccessary).
  • TODO: Look into log wrappers

Exports

  • Put all your export declarations last
  • Always use module.exports when exporting, not export

Comments

  • Try to keep the inline comments to a minimum
  • When creating a new module, always write a documentation in the header that explains what the module does

Callbacks

  • When defining callbacks, always include a error parameter first
  • Make sure that you always return the result of your callbacks
  • Use early returns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment