Skip to content

Instantly share code, notes, and snippets.

View letsgetrandy's full-sized avatar

Randy letsgetrandy

View GitHub Profile
'use strict';
var INTERVAL = 50;
class Bubble {
constructor() {
this.x = parseInt(window.innerWidth * Math.random(), 10);
this.y = parseInt(window.innerHeight * Math.random(), 10);
this.xDir = Math.random() > 0.5 ? 1 : -1;
this.yDir = Math.random() > 0.5 ? 1 : -1;
this.size = parseInt(Math.random() * 100) + 40;
// return an array of argument names
function getArgNames(fn) {
var matches = fn.toString().match(/\(([a-z_, ]+)\)/i);
if (matches.length > 1) {
return matches[1].replace(/\s+/g, '').split(',');
}
return [];
}
// some sample implementation
function Handler(fn) {
this.func = fn;
return this;
}
Handler.prototype.handle = function(errMsg, callback) {
this.exceptions[errMsg] = callback;
}
Handler.prototype.try = function(context) {
context = context || this;
try {
function yo(fn) {
if (fn instanceof Function) {
this.listeners.push(fn);
} else {
_.each(this.listeners, function (listener) {
listener(fn);
})
}
}
// language exceptions
var exceptions = {
"are": "were",
"eat": "ate",
"go": "went",
"have": "had",
"inherit": "inherited",
"is": "was",
"run": "ran",
"sit": "sat",
function Color(r, g, b) {
if (Array.isArray(r)) {
colors = r;
} else if (!g && !b) {
colors = this.asRGB(r);
} else {
colors = [r, g, b];
}
this.r = colors[0];
this.g = colors[1];
@letsgetrandy
letsgetrandy / rangemap.js
Last active August 29, 2015 14:07
Provide values for ranges
var Rangemap = function(points, values) {
this.points = points || [];
this.values = values || [];
};
Rangemap.prototype = {
"equalInRange": true,
"compare": function(a, b) {
if (a === b) {
return this.equalInRange;
@letsgetrandy
letsgetrandy / brototype.js
Last active February 1, 2019 17:52
Bro, do you even?
if (Object.defineProperty) {
Object.defineProperty(Object.prototype, 'doYouEven', {
value: function(key) {
var props = (key || '').split('.'),
item = this;
for (var i=0; i<props.length; i++) {
item = item[props[i]];
if (typeof item === 'undefined') return false;
}
return true;
@letsgetrandy
letsgetrandy / all.js
Created June 23, 2014 20:18
A few semantic functions for Javascript...
/**
* Returns TRUE if all arguments passed in can be evaluated as true.
* Otherwise, returns FALSE.
*/
function all() {
var i, l = arguments.length;
for (i = 0; i < l; i++) {
if (!arguments[i]) {
return false;
}
@letsgetrandy
letsgetrandy / fmtWholeNum.js
Last active January 19, 2017 19:49
Format whole numbers with commas at every 3rd digit.
function fmtWholeNum (num) {
var n = parseFloat(num.toString().replace(/[^\d\.]+/, '')),
s = Math.floor(n).toString();
return s.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
}