Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / index.js
Last active October 9, 2022 09:59
node.js: in-browser github oauth from cli
var githubOAuth = require('github-oauth')({
githubClient: process.env['GITHUB_CLIENT'],
githubSecret: process.env['GITHUB_SECRET'],
baseURL: 'http://localhost:9001',
loginURI: '/login',
callbackURI: '/callback',
scope: 'user',
});
require('http').createServer(function(req, res) {
@cowboy
cowboy / why.js
Last active June 4, 2023 05:30
JavaScript: why do we write async code the way we do?
// This is an UBER simplified example, but I hope you get the idea...
function thisIsHowWeWriteSyncCode(arg) {
var foo = doSomething(arg);
var bar = doSomethingElse(foo);
var baz = doSomethingWith("test", bar, 123);
return doLastThing(baz);
}
function soThisSeemsSensibleForAsyncCode(arg) {
@cowboy
cowboy / quine-examples.js
Last active April 26, 2025 08:09
A few JavaScript quines
// 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 / currying-use-case.js
Created January 29, 2015 16:17
JavaScript: I've finally found a use case for currying in JavaScript!
// You can have multiple optional arguments, but only if the arguments
// can be distinguished from one another (in this example, by type)
function optionalUnambiguous(myString, myNumber) {
if (typeof myString === 'number') {
myNumber = myString;
myString = null;
}
if (myString == null) { myString = 'default'; }
if (myNumber == null) { myNumber = 0; }
@cowboy
cowboy / bind-vs-variable.js
Created January 28, 2015 21:10
JavaScript: Why use an explicit variable when you can just bind and use this instead?
// Why use an explicit variable...
function lame() {
var args = arguments;
return function(fn) {
console.group.apply(console, args);
fn();
console.groupEnd();
};
}
@cowboy
cowboy / 5757.md
Last active August 29, 2015 14:12
ed rare route
@cowboy
cowboy / unicorn1.txt
Last active June 4, 2023 05:59
OMG ASCII ART UNICORNS YAY!!!
/
.7
\ , //
|\.--._/|//
/\ ) ) ).'/
/( \ // /
/( J`((_/ \
/ ) | _\ /
/|) \ eJ L
@cowboy
cowboy / ahk-active-window.ahk
Last active August 29, 2015 14:11
AutoHotKey: Poll system for known active windows
; ===================================================
; Poll system for known active windows
; "Cowboy" Ben Alman - 2014
; https://gist.github.com/cowboy/77642d25ef49559d574f
; ===================================================
#Persistent
#SingleInstance Force
; Exe windows to poll for.
@cowboy
cowboy / 1-file.txt
Last active May 6, 2024 12:29
Iterate over all lines in a file, handing extra trailing newlines
foo bar
baz
qux
last line (there may or may not be a trailing newline after this line)
@cowboy
cowboy / AutoHotkey.ahk
Last active May 16, 2017 02:37
AutoHotkey: Press quickly to send <key1>, hold for <delay> ms to send <key2>. Also, matching Logitech G700s profile.
; Press quickly to send <key1>, hold for <delay> ms to send <key2>
; Examples:
; Joy8::dualPress("[", "]")
; Joy10::dualPress("N", "{RShift Down}{N}{RShift Up}")
; MButton::dualPress("F12", "F11", 0.5)
dualPress(key1, key2, delay = 0.2) {
IfNotInString, key1, {
key1 = {%key1%}
IfNotInString, key2, {