Skip to content

Instantly share code, notes, and snippets.

View lxe's full-sized avatar
🦀
Crab

Aleksey Smolenchuk lxe

🦀
Crab
View GitHub Profile
@lxe
lxe / tips.js.md
Last active January 2, 2016 00:09

JavaScript Tips

General advice on what to do and not to do.

1. return early to avoid unnecessary nesting.

Wrong:

function foo (err) {
 if (!err) {
@lxe
lxe / mr.js
Created January 13, 2014 20:08
// mr.js -- very small request maker
var http = require('http')
, https = require('https')
, url = require('url')
var mr = module.exports = function mr (uri, callback) {
uri = typeof(uri) === 'string' ? url.parse(uri, true) : uri;
uri.method = uri.method || 'GET';
uri.headers = { host : uri.host, accept : 'text/html' };
@lxe
lxe / test.md
Created January 13, 2014 20:49
  1. Install frontend2 and any theme pack
  2. Navigate to the frontend2 directory and run:
brunch build --production
export DISPATCH_ASSETROOT=`pwd public`
  1. Navigate to dispatch directory and run dispatch (nodemon)
// Server for serving 'index.html' and static assets
// great for testing client-based apps (Ember/angular/backbone etc..)
var port = parseInt(process.argv[2], 10) || 3000;
require('http').createServer(function(req, res) {
var file = /\.(png|jpg|gif|css|js|map)$/.test(req.url) ? req.url.slice(1) : 'index.html';
require('fs').readFile(file, function(err, data) {
var mime = {
'html' : 'text/html', 'png' : 'image/png', 'gif' : 'image/gif',
'jpg' : 'image/jpeg', 'css' : 'text/css', 'js' : 'text/javascript'
} [file.split('.').pop()] || 'application/octet-stream';
// run with "mocha mocha-proc.js"
var spawn = require('child_process').spawn
, should = require('should')
describe('your thing', function () {
it('should do some stuff', function (done) {
var ls = spawn('ls', ['-lh', '/usr']);
# var var var vs , , ,
- , , ,
- Leading Commas
# declare on top of scope or near usage
- Consistency is not compromised -- doesn't matter
# Whenever makes sense name anonymous functions
- _.map([], function () {
@lxe
lxe / runme.js
Last active August 29, 2015 14:00
spawn test
console.log('test test')
var fs = require('fs')
, path = require('path')
, app = require('express')()
, Busboy = require('busboy');
function saveFiles(req, done) {
var busboy = new Busboy({ headers : req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log(fieldname, file, filename, encoding, mimetype);
// Pipe to fs or db, or res, or anywhere you want!
widgetCollections.map(function (widgetCollection) {
return Object.keys(widgetCollection).reduce(function (widgets, widgetKey) {
if (widget.isFrobbable()) {
widgets.push({
widgetName: widgetKey,
widgetValue: decombabulateWidgetValue({
decombobulator: config.getDecombobilator(),
verbose: false
}, widgetCollection[widgetKey])
});
// Long-polling chat server + client.
var chats = [];
require('http').createServer(function(req, res) {
if (/\/c/.test(req.url)) return res.end(chats.join('\n'));
var m = req.url.split('=');
if (m.length > 1) chats.unshift(m[1].replace(/\+/g, ' '));
res.end('<html><body><form><input name="m"></form><pre></pre><script>'
+ 'var r=new XMLHttpRequest;(function e(){r.open("GET","/c",true);r.send();r.onreadystatechange=function(){if(r.readyState==4){document.getElementsByTagName("pre")[0].innerHTML=r.responseText;setTimeout(e,1e3)}}})();document.forms[0].m.focus()'
+ '</script></body></html>')
}).listen(parseInt(process.env.PORT, 10)|| 3000);