Skip to content

Instantly share code, notes, and snippets.

View AndersDJohnson's full-sized avatar

Anders D. Johnson AndersDJohnson

View GitHub Profile
@AndersDJohnson
AndersDJohnson / eattouchend.js
Last active June 5, 2019 13:33
eat touchend on links
// Wrangling with "ghost clicks" in Sencha Touch... is this hungry enough to eat them?
// Based on http://stackoverflow.com/questions/6457220/sencha-touch-button-handler-called-twice-for-single-click-why
// Put this somewhere global and early. (top of app.js?)
document.addEventListener('touchend', function(e) {
var node = e.target;
while (node) {
if (
(node.localName && node.localName.toLowerCase && node.localName.toLowerCase() === 'a') ||
@AndersDJohnson
AndersDJohnson / abs_dirname.sh
Last active June 5, 2019 13:33
bash script BASEDIR
# from: https://github.com/zimbatm/direnv/blob/3bb35d375331fca89614f0015b2a6cd21688ab79/bin/direnv
# usage: abs_dirname $filename
# finds the original $filename path and prints it's absolute folder path
abs_dirname() {
prev_path="$1"
# Resolve the symlink(s) recursively
while true; do
abs_path=`readlink "$prev_path"`
if [ -z "$abs_path" ]; then
abs_path="$prev_path"
@AndersDJohnson
AndersDJohnson / Object.filter.js
Last active June 5, 2019 13:33
Object functional js
// Like underscore.js "filter", but returns an object with the keys and values
// instead of just an array of values.
Object.prototype.filter = function(obj, predicate) {
var result = {}, key;
for (key in obj) {
if (obj.hasOwnProperty(key) && predicate(obj[key], key, obj)) {
result[key] = obj[key];
}
((plugin) ->
# Module systems magic dance.
if (typeof require == "function" && typeof exports == "object" && typeof module == "object")
# NodeJS
module.exports = plugin
else if (typeof define == "function" && define.amd)
# AMD
define -> plugin
else
# Other environment (usually <script> tag): plug in to global chai instance directly.
@AndersDJohnson
AndersDJohnson / another contents
Last active June 5, 2019 13:34
test description
another filename
@AndersDJohnson
AndersDJohnson / setIntervalSynchronous.js
Last active December 6, 2024 14:35
A synchronous version of setInterval (functional form). Waits for the interval function to finish before starting the timeout to the next call.
var setIntervalSynchronous = function (func, delay) {
var intervalFunction, timeoutId, clear;
// Call to clear the interval.
clear = function () {
clearTimeout(timeoutId);
};
intervalFunction = function () {
func();
timeoutId = setTimeout(intervalFunction, delay);
}
@AndersDJohnson
AndersDJohnson / JSONCircular.coffee
Last active June 5, 2019 13:34
JSON stringify circular replacer
JSONCircular = (circularValue = '[Circular]') ->
cache = [];
return (key, value) ->
if (typeof value is 'object' && value isnt null)
if (cache.indexOf(value) isnt -1)
return circularValue;
cache.push(value);
return value;
# use:
@AndersDJohnson
AndersDJohnson / express-rewrite.01.coffee
Last active June 5, 2019 13:34
express rewrite URLs
app.use (req, res, next) ->
if req.url.indexOf('/fake') isnt -1
req.url = req.url.replace('/fake', '/ghost')
next()
/**
* Handlebars.js partial parent context access I wrote.
* See https://github.com/wycats/handlebars.js/issues/182#issuecomment-4445747
*/
Handlebars.registerHelper('$', function ( child, options ) {
if ( typeof child !== 'object' ) {
return '';
}
child['$_'] = this;
return options.fn( child );
@AndersDJohnson
AndersDJohnson / customErrors.js
Last active June 5, 2019 13:34
Using Custom Errors in Node.js
/**
* Using Custom Errors in Node.js
* http://dustinsenos.com/articles/customErrorsInNode
*/
// Grab the util module that's bundled with Node
var util = require('util')
// Create a new Abstract Error constructor
var AbstractError = function (msg, constr) {