Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
ben-bradley / EventsClass.js
Created September 26, 2013 15:54
A simple way to extend the EventEmitter to classes with the class.extend module in NodeJS
/*
* A simple way to extend the EventEmitter to classes with the class.extend module in NodeJS
*/
var Class = require('class.extend'),
events = require('events');
var Events = Class.extend({
init: function() { events.EventEmitter.call(this); }
});
@ben-bradley
ben-bradley / Guvna.js
Created September 26, 2013 16:00
A copy of the Guvna to stick into bradleyit.com
function Guvna(options) {
// error checking
if (!options.list || !options.callback || !options.done) {
console.log('ERROR: missing required option - { list: [], callback: fn(), done: fn() }');
return false;
}
// assign vars
this.list = options.list;
@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);
@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 / 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 / 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 / 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 / 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 / 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 / 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);
};