Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
import assert from 'node:assert/strict';
// Extended RegExp mode (flag /x) [1] via a template tag
//
// Quote: “While the x-mode flag can be used in a RegularExpressionLiteral,
// it does not permit the use of LineTerminator in RegularExpressonLiteral.
// For multi-line regular expressions you would need to use the RegExp
// constructor.”
//
// The plan is to include this functionality in re-template-tag [2]. Then
@rauschma
rauschma / README.md
Last active May 10, 2023 09:53
Better dynamic type checks

Better dynamic type checks

  • Update 2022-07-10: new approach based on Function.prototype.hasInstance()

Problems

In principle, the rule “typeof is for primitive values, instanceof is for objects” works. There are two exceptions:

  • Checking if a value is an object
  • Cross-realm instance checks
import {ReadableStream} from 'node:stream/web';
/**
* @param iterable an iterable (asynchronous or synchronous)
*
* @see https://streams.spec.whatwg.org/#example-rs-pull
*/
function iterableToReadableStream(iterable) {
return new ReadableStream({
async start() {
@cowboyd
cowboyd / 1-click-to-load.js
Last active April 11, 2023 10:54
Handle a stream of clicks by canceling the latest one
/**
* Implements an operation to fetch the data as described in https://twitter.com/BenLesh/status/1455597411385098242
* The "winner" is unambiguous. It is the request corresponding to the latest click, guaranteed.
* Other important things to note:
*
* 1. The HTTP request is cancelled every time along with its containing task
* 2. click listener is removed automatically at the end of the `clickToLoad` operation
* 3. The cancellation logic is explicit here to demonstrate the concept, but it can easily be abstracted into a separate
*. function. See the `click-to-load-HOF` example below
*/
@aszx87410
aszx87410 / README.md
Last active December 8, 2023 21:59
let vs var by investigating the bytecode generated by Node.js

It's the reply to the question raised by @getify on his twitter:

here's a variation on the question... will JS engines exhibit much performance difference between these two loops?

for (var i = 0; i < 100000000; i++) {
   // do some stuff, but not closure
}

for (let i = 0; i < 100000000; i++) {
@WebReflection
WebReflection / executable-standalone-module.md
Last active March 4, 2024 20:55
NodeJS Executable Standalone Module

Update

If you're OK in having a node-esm executable, please consider this solution.

#!/usr/bin/env sh
# the /usr/local/bin/node-esm executable
input_file=$1
shift
exec node --input-type=module - $@ &lt;$input_file
@MTuner
MTuner / fix-sublimetext-subpixel.txt
Last active March 31, 2023 21:44
Fixing font rendering/aliasing in Sublime Text in MacOS Mojave
Apple removed colored sub-pixel antialiasing in MacOS Mojave
(https://developer.apple.com/videos/play/wwdc2018/209/ starting from ~28min)
To make fonts look normal in Sublime Text, add to Preferences:
// For the editor
"font_options": [ "gray_antialias" ],
// For the sidebar / other elements
"theme_font_options": [ "gray_antialias" ],

In this tutorial we're going to build a set of parser combinators.

What is a parser combinator?

We'll answer the above question in 2 steps.

  1. What is a parser?
  2. and, what is a parser combinator?

So first question: What is parser?

@ggauravr
ggauravr / array_iteration_thoughts.md
Last active October 3, 2021 02:40 — forked from ljharb/array_iteration_thoughts.md
Array iteration methods summarized

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

@michaelficarra
michaelficarra / append-template-tag.js
Created May 30, 2016 14:29
chainable template tag for joining a bunch of strings over many lines
function append(separator) {
return typeof separator === "string" ? appender(separator, "") : appender("", "").apply(this, arguments);
}
function appender(separator, s) {
return function tag(literalParts, ...computedParts) {
s += literalParts[0];
for (let i = 1; i < literalParts.length; ++i) {
s += computedParts[i - 1] + literalParts[i];
}