Skip to content

Instantly share code, notes, and snippets.

/*
I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
so it's better encapsulated. Now you can have multiple random number generators
and they won't stomp all over eachother's state.
If you want to use this as a substitute for Math.random(), use the random()
method like so:
var m = new MersenneTwister();
@monkyz
monkyz / gist:4695747
Created February 2, 2013 02:15
nasty code
function addThree(x) {return x + 3;};
function composed(func) {
return function(x) {
return func(func(x))
}
};
composed(addThree)(4);
@monkyz
monkyz / gist:3935315
Created October 22, 2012 23:04
javascript boolean logic
// => means:"evaluates into"
true && true => true;
true && false => false;
false && true => false;
false && false => false
-----------------------------------------------------------------------------------------------------------
false || false => false;
true || false => true;
false || true => true;
@monkyz
monkyz / gist:3909302
Created October 18, 2012 01:09
javascript cash register
var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item, quantity) {
switch (item) {
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
@monkyz
monkyz / gist:3903591
Created October 17, 2012 03:43
express.js security and general node.js security(on going)
Add app.enable('trust proxy') as I have express running behind nginx:
------------------------------------------------------------------------------------------------------------
Express behind proxies
Using Express behind a reverse proxy such as Varnish or Nginx is trivial, however it does require configuration. By enabling the "trust proxy" setting via app.enable('trust proxy'), Express will have knowledge that it's sitting behind a proxy and that the X-Forwarded-* header fields may be trusted, which otherwise may be easily spoofed.
Enabling this setting has several subtle effects. The first of which is that X-Forwarded-Proto may be set by the reverse proxy to tell the app that it is https or simply http. This value is reflected by req.protocol.
The second change this makes is the req.ip and req.ips values will be populated with X-Forwarded-For's list of addresses.
http://expressjs.com/guide.html
@monkyz
monkyz / gist:3903538
Created October 17, 2012 03:26
better install stylus -g
npm install stylus -g
than after issue the following command:
stylus -w && nodemon app.js
http://learnboost.github.com/stylus/docs/executable.html
@monkyz
monkyz / gist:3889223
Created October 14, 2012 17:15
stylus questions 2
body {
width: 100%;
clearfix();
}
@monkyz
monkyz / gist:3888840
Created October 14, 2012 14:57
questions about stylus
if i have this:
body {
font: 14px/1.5 Helvetica, arial, sans-serif;
#logo {
border-radius: 5px;
}
}
would be the same as
@monkyz
monkyz / gist:3887131
Created October 14, 2012 03:24
coverting stylus to normal css syntax with colons, etc
/////////////////
// Semantic.gs // for Stylus: http://learnboost.github.com/stylus/
/////////////////
// Defaults which you can freely override
column-width = 60px
gutter-width = 20px
columns = 12
// Utility variable — you should never need to modify this
@monkyz
monkyz / gist:3886998
Created October 14, 2012 02:04
installing nib
//first, require nib in the beginning of the file. note that requiring stylus was needed due to the second code change below:
, nib = require('nib')
, stylus = require('stylus');
//second change:
app.use(stylus.middleware({ src: __dirname + '/public',
compile: compile
}));
//third change, added the function compile: