Skip to content

Instantly share code, notes, and snippets.

View ljharb's full-sized avatar
🔜
working on that thing you asked about

Jordan Harband ljharb

🔜
working on that thing you asked about
View GitHub Profile
Function.prototype.within = function (millis) {
var that = this;
return function () {
var args = Array.prototype.slice.call(arguments); // if sinful present, slice(arguments)
return window.setTimeout(function () { that.apply(null, args); }, millis); );
};
};
var EventEmitter = require("events").EventEmitter;
var emitter = new EventEmitter();
var foo = function () {
console.log("foo");
};
emitter.once("foo", foo);
var bar = function () {
@ljharb
ljharb / Queue.js
Last active December 12, 2015 05:58 — forked from WebReflection/Queue.js
/*global setTimeout */
var Queue = function Queue(q) {
"use strict";
// (C) WebReflection - Mit Style License
var callback,
next = function next() {
callback = q.shift();
if (callback) { callback(q); }
return !!callback;
};
@ljharb
ljharb / gist:6126127
Created July 31, 2013 21:00
Konami code implementation
(function ($) {
var keys = [], code = '38,38,40,40,37,39,37,39,66,65';
var konami = function (e) {
keys.push(e.which);
if (keys.length >= 11 && keys.slice(-11).toString() !== code) {
keys = [];
/* do your konami thing here */
} else if (keys.length > 11) {
keys.length = 11;
}
@ljharb
ljharb / gist:6321587
Last active December 21, 2015 14:39 — forked from getify/gist:6315505
// because of declaration hoisting, wherever we put `filter()`, it
// will be declared for a longer lifetime than is necessary.
//
// inline function expression (or arrow function) inside the loop
// isn't the answer, because then you recreate the function over
// and over again.
function doSomething() {
var items = [], i, ret = 0;
@ljharb
ljharb / gist:8174372
Created December 29, 2013 20:20
Handling the results of multiple async ops with JS.
var promises = [
async1(),
async2(),
asyncN()
];
/* jQuery: warning, won't swallow exceptions */
var deferred = $.Deferred();
$.when.apply($, promises)
.done(function () { deferred.resolve(promises); })

Keybase proof

I hereby claim:

  • I am ljharb on github.
  • I am ljharb (https://keybase.io/ljharb) on keybase.
  • I have a public key whose fingerprint is F208 CF4D F9AF 4C98 82F5 C737 DD6C DDD3 CBBB C186

To claim this, I am signing this object:

@ljharb
ljharb / grid.js
Last active August 29, 2015 14:15 — forked from getify/grid.js
function formatValue(x) {
var ret;
if (typeof x == "string") {
return "'" + x.replace(/\n/g,"\\n") + "'";
}
if (typeof x == "number" && x === 0 && (1/x === -Infinity)) {
return "-0";
}
if (Array.isArray(x)) {
@ljharb
ljharb / test.js
Created February 4, 2016 06:44
node 0.6-0.12 has a broken `Object.getOwnPropertyNames`
> Object.defineProperty(Object.prototype, 'a', { set: function (v) { throw new Error(v); } });
{}
> Object.getOwnPropertyNames({})
[]
> Object.getOwnPropertyNames({a: 1})
Error: true
at Object.defineProperty.set (repl:1:74)
at Function.getOwnPropertyNames (native)
at repl:1:9
at REPLServer.self.eval (repl.js:110:21)
@ljharb
ljharb / array_iteration_thoughts.md
Last active October 24, 2024 07:30
Array iteration methods summarized

Array Iteration

https://gist.github.com/ljharb/58faf1cfcb4e6808f74aae4ef7944cff

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it mu