Skip to content

Instantly share code, notes, and snippets.

View lxe's full-sized avatar
🌮
Taco

Aleksey Smolenchuk lxe

🌮
Taco
View GitHub Profile
@lxe
lxe / camels.js
Created March 6, 2013 16:29
Camels
module.exports = {
deCamel: function (camelCaseKey) {
return camelCaseKey.replace(/([^_])([A-Z])/g, function(l0, l1, l2) {
return l1 + '_' + l2.toLowerCase()
});
},
reCamel: function (underscores_key) {
return underscores_key.replace(/([^_])_([^A-Z])/g, function(l0, l1, l2) {
return l1 + l2.toUpperCase()

lxe's javascript style guide.

Can be applied to C as well.

Focus on the first 3 headings.

1. Consistency

Keep things consistent. Don't vary your indent, spacing, braces, etc... across lines, files and project.

2. Readability

Make sure your code is easy on the eyes. If you're supposedly showing someone what your code does, line by line, make sure they aren't struggling to follow it.

var generateForeignName = function() {
return [2, 4].map(function(numSyls) {
var i = Math.floor(Math.random() * numSyls) + 2
, syl = 'bdfghklmnprstj'
, vow = 'aouei'
, name = '';
while (i--) {
name += syl[Math.floor(Math.random() * (syl.length - i))];
name += vow[Math.floor(Math.random() * (vow.length - i))];
var r = new RegExp(
// ^ - not the following...
// \/\? - slash and a question mark
// + - 1 or more of them
'[^/?]+'
// Followed by (but don't capture this!)...
// ?= - don't capture!
// ^ - not the following...
// / - a slash
var fs = require('fs');
var ws = fs.createWriteStream('file.out.json');
ws.on('open', function() {
ws.write(JSON.stringify({
foo : 'bar'
}));
ws.end();
});
@lxe
lxe / morse.js
Last active December 18, 2015 10:39
http://morseroulette.com/ text-to-morse
function morse(s, speed) {
s = s.toLowerCase().replace(/[^\w ]/g, '');
var codes = {
'a' : '.-',
'b' : '-...',
'c' : '-.-.',
'd' : '-..',
'e' : '.',
'f' : '..-.',
var express = require('express')
, app = express()
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
// Freeze the route to /foo/static
app.get('/foo/static', function(req, res, next) {
if (!req.accepts('html')) {
var _ = require('lodash');
// Detect and fire all async functions in a plain JS object.
// Assume all functions take [callback(data)] as argument
//
// 1. Traverse the object:
// 2. If a function is found, replace with a AsyncResolver instance which:
// 2.1 - Fires the function
// 2.2 - Increments asyncsCreated
// 3. When each AsyncResolver callback fires, it does this:
@lxe
lxe / ludicrousspeed.sh
Last active December 29, 2015 00:39
ludicrousspeed.sh
#!/bin/bash
# Some tweaks to OSX tcp/fd setting
# to ease load testing
#
# Disclaimer: I have no idea what i'm doing
#
# Descrease port hold time
@lxe
lxe / nodesite.md
Last active December 29, 2015 08:59

How to build a node.js site

Prerequisites:

General tools and skills:

  • Have a computer.
  • Be familiar with JavaScript, HTML, CSS.
  • Be familiar with a terminal. If you're on windows, PowerShell will probably do, but I have had great success with cygwin and console.
  • Be familiar with a text editor in which you can adjust automatic indentation and line endings. Vi/vim, Sublime Text or Notepad++ will do. Use an indent of 2 spaces and UNIX-style line endings.