Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
ben-bradley / Main.html
Last active August 29, 2015 14:01
npmjs download count for generator-beardme
<h3>Beards grown this week: <span id='downloads' class='fa fa-circle-o-notch fa-spin'></span></h3>
@ben-bradley
ben-bradley / ascii-bacon.js
Created May 19, 2014 17:39
bacon ascii art
module.exports = "While you wait, how about some tasty bacon?\n\n"+
" __ _.._ \n"+
" .-'__`-._.'.--.'.__., \n"+
" /--' '-._.' '-._./ \n"+
" /__.--._.--._.'``-.__/ \n"+
" '._.-'-._.-._.-''-..' \n"+
"\n";
@ben-bradley
ben-bradley / ws-sendJson.js
Created May 15, 2014 04:14
ws sendJson sugar
ws.prototype.sendJson = function(data, options, callback) {
this.send.call(this, JSON.stringify(data), options, callback);
};
@ben-bradley
ben-bradley / perf-server.js
Last active August 29, 2015 14:01
nodejs-tcp-server.js
var net = require('net');
var server = net.createServer();
server.on('connection', function(socket) {
console.log('connected!');
var start = Math.floor(new Date().getTime()/1000);
socket.readings = {};
@ben-bradley
ben-bradley / express-mocha.js
Last active August 29, 2015 14:01
wrap mocha in child_process.spawn() to grab JSON output
/* npm insatll -g mocha
* npm install express
* node ./<this>
* put tests in ./test/ and they can be called by hitting http://localhost:3030/test/:file
* you can test all tests by hitting http://localhost:3030/test/all
*/
var express = require('express'),
app = express();
var spawn = require('child_process').spawn;
@ben-bradley
ben-bradley / zerocountdetector.js
Last active August 29, 2015 14:00
awesome technique for dealing with a counter at zero
[ 0, 1, 2, 3, 4, 5 ].forEach(function(y) { var z = --y || 'wut!?'; console.log(z); })
/* output:
-1
"wut!?"
1
2
3
4
*/
@ben-bradley
ben-bradley / varan.js
Created April 2, 2014 05:20
Faye & Express integration
/* http://en.wikipedia.org/wiki/Varan */
var Faye = require('faye'),
express = require('express'),
ping = require('ping');
var bayeux = new Faye.NodeAdapter({ mount: '/faye', timeout: 45 }),
client = bayeux.getClient(),
app = express();
app.configure(function() {
@ben-bradley
ben-bradley / printer_mischief.js
Last active December 30, 2015 22:59
HP Printer LCD customizations
/****************************************************************************
* Inspired by https://github.com/joshuah/insert-coin/blob/master/printer.js *
****************************************************************************/
var net = require('net');
var printers = [
{ ip: '192.168.1.10', lcdRows: 2, lcdCols: 16 }
];
@ben-bradley
ben-bradley / Cisco_IOS_Regex_hints.js
Last active December 30, 2015 12:29
This bit of JavaScript regex will parse the interfaces out of an entire Cisco IOS config!
var config = fs.readFileSync('configFile.txt').toString();
var interfaces = config.match(/interface .+[\r]*\n [\s\S]+?[^\ ]!/g);
console.log(interfaces):
/*
[
"interface Vlan1\n description ...\n!",
"interface Vlan2\n description ...\n!",
...
@ben-bradley
ben-bradley / fakeAsync.js
Created September 26, 2013 16:02
Simple function to simulate async
/********************************************/
/* insert random delays to simulate latency */
/********************************************/
function delay(callback, n) {
var r = Math.floor(Math.random()*(n || 5000));
setTimeout(function() { callback(); }, r);
}
// specify a max on the delay timer range
delay(function() { console.log('BLARGH!'); }, 10000);