Skip to content

Instantly share code, notes, and snippets.

View aclave1's full-sized avatar

Alex Clavelle aclave1

  • Meta
  • Bay Area, California
View GitHub Profile
@aclave1
aclave1 / dropdown.js
Last active January 4, 2016 18:09
Simple Javascript dropdown box
/**
* AddDropdown : appends a dropdown box to an element, and provides a callback to further style the element and the dropdown
*
@param elem an html node object, or the id of an html node object
@param css an object literal containing css to apply to the dropdown box
@param callback allows the user to do more with the dropdown box
*/
function AddDropdown(elem,css,callback){
@aclave1
aclave1 / EqualityStack.js
Last active August 29, 2015 13:55
Equality Stack
/**
Constructor function which keeps an array of objects, and makes sure they are all equal.
Used for checking consistency between many objects easily.
for example:
create a bunch of objects that should be exactly the same.
var data1 = object.serialize();
@aclave1
aclave1 / Synchronize.js
Created February 2, 2014 07:42
Javascript data synchronization
/**
Used to synchronize multiple types of storage and their timestamps.
options{
data : {}, the state that all storage should take.
date: int, the modified date that all storage should take
interval : int, sync interval in milliseconds.
callbacks : [], an ARRAY of callback functions which are to each be executed with data
and date as params. Each callback should be used to update a single storage medium.
}
@aclave1
aclave1 / Draggable.js
Created February 2, 2014 07:45
Simple Javascript draggable element
/**
Pass in a dom ID string or a dom node and to make it draggable.
*/
var Draggable = function(elem) {
var that = this;
var el = (typeof elem === 'string') ? document.getElementById(elem) : elem;
var offX, offY;
this.mouseDown = function(e) {
offX = e.clientX - parseInt(el.offsetLeft);
offY = e.clientY - parseInt(el.offsetTop);
@aclave1
aclave1 / ParticleGen.js
Created February 2, 2014 17:39
Simple Javascript particle Generator
/**
*
options:(OBJECT){
type : element type we're shooting.
x : x coordinate
y : y coordinate
color : color of element
}
*/
@aclave1
aclave1 / LinkedList.js
Created February 4, 2014 05:39
Simple Javascript Linked List
var LinkedList = function() {
var head, tail;
var Node = function(dat) {
var _node = this,
_next = null,
_previous = null,
_data = dat;
@aclave1
aclave1 / app.js
Last active August 29, 2015 14:04
Simplest possible node.js web server
var http = require('http');
var fileReader = require('fs');
http.createServer(function(req,res){
var path = __dirname+req.url;
fileReader.readFile(path,function(err,fl){
res.end(fl);
});
}).listen(8080);
@aclave1
aclave1 / angularretry.js
Last active October 21, 2019 20:34
Angularjs auto retry after request failure and resolution
var app = angular.module('app', []);
app
.factory('AuthToken', [
'$http',
'$q',
'$window',
function($http, $q, $window) {
var authToken = {};
//gets the token and sets it on the windows session storage
@aclave1
aclave1 / PromiseMock.spec.js
Last active August 29, 2015 14:05
Unopinionated promise mocking
describe('An object which relies on promises', function () {
it('Should pass if provided the proper parameters', function () {
/**
* our real object would normally depend on promiseDependency,
* but we're replacing it with our mock promise chain here.
*/
sut.promiseDependency = PromiseMocks.ResolveMock();
var params = {
name : 'alex'
@aclave1
aclave1 / ngbootstrap
Created October 31, 2014 02:28
Angular bootstrapping snippet
angular.element(document).ready(function() {
angular.bootstrap(document, ['app']);
});