Skip to content

Instantly share code, notes, and snippets.

🔜
working on that thing you asked about

Jordan Harband ljharb

🔜
working on that thing you asked about
Block or report user

Report or block ljharb

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
@ljharb
ljharb / array_iteration_thoughts.md
Last active Jul 22, 2019
Array iteration methods summarized
View array_iteration_thoughts.md

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 much simpler to think about both the old list and the new one, what they contain, and

@ljharb
ljharb / test.js
Created Feb 4, 2016
node 0.6-0.12 has a broken `Object.getOwnPropertyNames`
View test.js
> 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)
View 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)) {
View keybase.md

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 / gist:8174372
Created Dec 29, 2013
Handling the results of multiple async ops with JS.
View gist:8174372
var promises = [
async1(),
async2(),
asyncN()
];
/* jQuery: warning, won't swallow exceptions */
var deferred = $.Deferred();
$.when.apply($, promises)
.done(function () { deferred.resolve(promises); })
View gist:6321587
// 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:6126127
Created Jul 31, 2013
Konami code implementation
View gist:6126127
(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;
}
View 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;
};
View Listeners.js
var EventEmitter = require("events").EventEmitter;
var emitter = new EventEmitter();
var foo = function () {
console.log("foo");
};
emitter.once("foo", foo);
var bar = function () {
View Function.prototype.within.js
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); );
};
};
You can’t perform that action at this time.