Skip to content

Instantly share code, notes, and snippets.

View werelax's full-sized avatar

Elías Alonso werelax

View GitHub Profile
@werelax
werelax / proxy-site.example.com
Last active April 13, 2021 18:22
Simple nginx config examples
upstream app_server {
server 127.0.0.1:7000;
keepalive 64;
}
server {
listen 80;
# listen 443 ssl;
server_name viviz.bettr.es www.viviz.bettr.es;
@werelax
werelax / redact.js
Created January 24, 2015 11:27
Redact
// utils
var slice = Array.prototype.slice;
var concat = Array.prototype.concat;
var flatten = function(l) { return concat.apply([], l); };
// really stupid DSL
function nodeOrTextNode(n) {
if (typeof(n) === "object")
@werelax
werelax / tiny_klass.js
Created September 23, 2014 22:34
Really small JavaScript class library
function extend() {
return Array.prototype.reduce.call(arguments, function(acc, el) {
for (var k in el) if (el.hasOwnProperty(k)) acc[k] = el[k];
return acc;
});
}
function klass(parent, props) {
var F = (props && props.init) || function() { return parent.apply(this, arguments); };
F.prototype = extend(Object.create(parent.prototype), props);
@werelax
werelax / router.js
Created July 18, 2014 08:53
Backbone.Router extension with before and after hooks
define(function(require, exports, module) {
"use strict";
var Backbone = require("backbone"),
_ = require("underscore");
function maybeApply(fn, ctx, args) {
if (fn) {
return fn.apply(ctx, args);
}
@werelax
werelax / router.js
Created July 18, 2014 08:30
Backbone.Router extension with "cascade" routing. [WARNING: has some problems because the routes cannot be removed!]
define(function(require, exports, module) {
"use strict";
var Backbone = require("backbone"),
_ = require("underscore");
/* The extractor determines the order of the cascading */
/* shift = bottom first */
var extractor = "shift";
/* pop = top first */
@werelax
werelax / example.js
Created April 15, 2014 15:58
A nice way to gain some control over Backbone.View#events callbacks
// Use invoker to generate the invoker directly
// and be able to manipulate it. The key is that
// the invoker allways invokes on "this".
var invoker = function(method) {
var args = [].slice.call(arguments, 1);
return function() {
var newargs = [].slice.call(arguments);
return this[method].apply(this, args.concat(newargs));
};
@werelax
werelax / bot.js
Last active August 29, 2015 13:59
little script to automate user interactions in development
/* jshint ignore:start */
var bot = (function() {
var timeline = 0;
return function bot (timeout) {
function evter(evt, sel) {
var args = [].slice.call(arguments, 2);
return function() { var $n; ($n = $(sel))[evt].apply($n, args); };
}
return function(evt, node) {
@werelax
werelax / collision.js
Created March 23, 2014 00:04
circle collision detection
function dynamicCollision(agent1, agent2) {
function getAngle(v) {
return Math.atan2(v.y, v.x);
}
function normalize(v) {
var m = vdistance({ x: 0, y: 0 }, v);
return { x: v.x/m, y: v.y/m };
}
@werelax
werelax / intersection.js
Created March 22, 2014 19:23
Segments intersection
function segmentIntersection(a, b) {
var denom = (((b[1].y - b[0].y) * (a[1].x - a[0].x)) -
((b[1].x - b[0].x) * (a[1].y - a[0].y)));
if (denom === 0) { return null; }
var ua = ((((b[1].x - b[0].x) * (a[0].y - b[0].y)) -
((b[1].y - b[0].y) * (a[0].x -b[0].x))) / denom);
var ub = ((((a[1].x - a[0].x) * (a[0].y - b[0].y)) -
((a[1].y - a[0].y) * (a[0].x - b[0].x))) / denom);
@werelax
werelax / server.js
Last active August 29, 2015 13:57
node.js + mongo + simpleauth.js recipe
/* jshint node: true, laxcomma: true */
/* global __dirname */
/* Dependencies */
"use strict";
var express = require("express")
, Q = require("q")
, auth = require("./simpleauth")
, MongoClient = require("mongodb").MongoClient