Skip to content

Instantly share code, notes, and snippets.

Ben Alman cowboy

Block or report user

Report or block cowboy

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cowboy
cowboy / ba-smallwalker.js
Created May 5, 2011
Small Walker: A small and simple JavaScript DOM walker
View ba-smallwalker.js
/*!
* Small Walker - v0.1.1 - 5/5/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Walk the DOM, depth-first (HTML order). Inside the callback, `this` is the
@cowboy
cowboy / HEY-YOU.md
Last active Feb 15, 2020
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@cowboy
cowboy / mock-axios.js
Last active Feb 7, 2020
axios mocking via interceptors
View mock-axios.js
import axios from 'axios'
let mockingEnabled = false
const mocks = {}
export function addMock(url, data) {
mocks[url] = data
}
@cowboy
cowboy / sudo-keepalive-example.sh
Created Jul 15, 2012
Bash: Sudo keep-alive (good for long-running scripts that need sudo internally but shouldn't be run with sudo)
View sudo-keepalive-example.sh
#!/bin/bash
# Might as well ask for password up-front, right?
sudo -v
# Keep-alive: update existing sudo time stamp if set, otherwise do nothing.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Example: do stuff over the next 30+ mins that requires sudo here or there.
function wait() {
@cowboy
cowboy / bad-bad-bad.js
Created Oct 15, 2011
Shitty destructors for JavaScript!
View bad-bad-bad.js
function Thing(context, name) {
this.context = context;
this.name = name;
this.init();
this.startTheWorstGarbageCollectorEver();
}
Thing.prototype.startTheWorstGarbageCollectorEver = function() {
var id = setInterval(function() {
if (!this.context[this.name]) {
@cowboy
cowboy / abstraction-2020.js
Last active Feb 3, 2020
Abstraction.js 2020 Edition (ES6)
View abstraction-2020.js
/*
* Abstraction.js 2020 Edition
*
* Copyright (c) 2020 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
let $elseif, $else, $if = state => state ? (
$elseif = () => () => {},
@cowboy
cowboy / quine-examples.js
Last active Jan 19, 2020
A few JavaScript quines
View quine-examples.js
// Today, I noticed that someone favorited this tweet of mine from 2012:
// https://twitter.com/cowboy/status/232587756486938624
// !function $(){console.log('!'+$+'()')}() #quine
// So I fooled around for a little bit:
// Shortest JavaScript quine?
(function _(){return'('+_+')()'})()
// Have fun with this one!
@cowboy
cowboy / 1-file.txt
Last active Dec 6, 2019
Iterate over all lines in a file, handing extra trailing newlines
View 1-file.txt
foo bar
baz
qux
last line (there may or may not be a trailing newline after this line)
@cowboy
cowboy / bocoup-training-more-efficient-event-handlers.js
Created Feb 12, 2013
Bocoup training: More Efficient jQuery Event Handlers
View bocoup-training-more-efficient-event-handlers.js
// Straightforward + simple.
$("button").on("click", function(event) {
event.preventDefault();
var button = $(this);
var numberElem = button.find(".number");
var number = Number(numberElem.text()) - 1;
numberElem.text(number);
if (number === 0) {
button.prop("disabled", true);
button.off("click");
@cowboy
cowboy / ba-detach.js
Created Apr 23, 2011
JavaScript detach: detach a node from the DOM, optionally reattaching it when done.
View ba-detach.js
/*!
* JavaScript detach - v0.2 - 5/18/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
function detach(node, async, fn) {
You can’t perform that action at this time.