Skip to content

Instantly share code, notes, and snippets.

@datchley
datchley / asyn-sequence.js
Last active August 29, 2015 14:11
A rudimentary, asychronous ES5 generator using Promises
/**
* Return a random number between min/max where the mean
* result is approximately the mode across a uniform distribution.
* @param min {Number} - lower limit of range (inclusive)
* @param max {Number} - upper limit of range (inclusive)
* @return {Function} - a function that returns a random number between 'min' and 'max'
*/
var genRandomFromRangeFn = function(min, max) {
return function() {
var res = Math.round(Math.random() * (max - min)) + min;
@datchley
datchley / object-elevator.js
Last active August 29, 2015 14:11
Simple, single elevator implementation in javascript
function extend(/* objects */) {
var args = [].slice.call(arguments,0),
base = args.shift(),
len = args.length;
while (args.length) {
var o = args.pop();
for (var prop in o) {
// properties override those existing
if (typeof base[prop] === 'undefined' && typeof o[prop] !== 'function') {
base[prop] = o[prop];
@datchley
datchley / escape-utils.js
Last active August 29, 2015 14:07
Javascript string escape utilities
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
@datchley
datchley / moveto.html
Last active August 29, 2015 14:07
jQuery extension - $.moveTo() and a `DOMChanged` event implementation
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.moveable {
border: 1px solid red;
display: inline-block;
padding: 2px;
font-family: sans-serif;
}
@datchley
datchley / cprop.jquery.js
Last active August 29, 2015 14:07
Class-like properties on elements in jQuery
!function (factory) {
if (typeof exports == 'object') {
factory(require('jquery'));
} else if (typeof define == 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function($) {
'use strict';
@datchley
datchley / .jshint.rc
Created September 15, 2014 16:16
JSHint config file for RevOps Team developers
{
// JSHint Default Configuration File
// See http://jshint.com/docs/ for more details
"maxerr" : 50, // {int} Maximum error before stopping
// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
@datchley
datchley / url-parse.js
Last active August 29, 2015 14:03
Parse a URL string in it's various parts
function parseUrl(url) {
var _url = (url || window.location.href).match(/^(?:(https?:)\/\/)?([^\?\#\/\:]+)(?::(\d+))?([^\?\#]+)?(\?[^#]+)?(#[^\s]+)?$/),
params = _url[5] || false;
if (!_url.length) {
// couldn't parse url string
return false;
}
if (params && !/^$/.test(params)) {
@datchley
datchley / generic-xhr.js
Created June 3, 2014 19:58
Plain javascript XMLHttpRequest implementation.
function xhr(){
try {
return new XMLHttpRequest();
}catch(e){}
try {
return new ActiveXObject("Msxml3.XMLHTTP");
}catch(e){}
try {
return new ActiveXObject("Msxml2.XMLHTTP.6.0");
}catch(e){}
@datchley
datchley / promises.js
Last active August 29, 2015 14:02
Simple Promise/Deferred library (likely not Promise/A+ aligned).
var Promise = function() {
this.resolve_callbacks = [];
this.reject_callbacks = [];
this.state = null;
};
Promise.prototype = {
then: function(resolvefn, rejectfn) {
if (this.state == 'resolved') {
resolvefn();
@datchley
datchley / dom-utils.js
Last active March 19, 2019 05:59
DOM related utilities, implementations for wrap(), wrapAll(), geStyle() and getElementsByClassName() - try it http://jsbin.com/jozaje/1/
/** Faster rounding function to avoid heavy use of Math.round */
function round(n) {
return (n + 0.5) << 0;
}
/**
* Wrapper for getBoundingClientRect that returns a
* subset ('top','left') and includes a 'width' and 'height'.
* It also rounds the pixel measurements to the nearest integer value
*