Skip to content

Instantly share code, notes, and snippets.

@Stuk
Stuk / Clock.tsx
Last active May 10, 2022 20:56
swc #3628 repro
import React from "react";
export function Clock({
hour,
minute,
second,
}: {
hour: number;
minute: number;
second: number;
@Stuk
Stuk / example.js
Created November 6, 2020 17:35
AbortController usage
// See https://dom.spec.whatwg.org/#abortcontroller-api-integration
function getData({signal}) {
// 1. Let p be a new promise.
// 4. Return p.
return new Promise((resolve, reject) => {
// 2. If options' signal member is present, then:
if (signal && signal.aborted) {
// 2. 1. If options' signal's aborted flag is set, then reject p with an "AbortError" DOMException and return p.
throw new DOMException("Aborted", "AbortError");
@Stuk
Stuk / class.js
Last active March 7, 2024 01:06
ES5 classes, compatible with ES6 classes
function A() {}
A.prototype.thing = function () {
return "A";
};
function B() {}
Object.setPrototypeOf(B, A)
B.prototype = Object.create(A.prototype)
B.prototype.constructor = B;
@Stuk
Stuk / tools.md
Last active July 14, 2021 06:42
Things that I use on my Mac that you may also find useful

Things that I use on my Mac that you may also find useful

This list is in a rough order of most obscure/useful, so that the things at the top are tools that you haven't heard of before.

Witch

I find the default application switcher in OSX useless. I often have many Sublime Text windows open, a number of GitX windows, and maybe a couple of Chrome windows. Having to Cmd + Tab to the app, and then Cmd + ` through the app windows takes too long. Witch works like Alt + Tab on Windows.

It takes a bit of persuading to make it replace the default Cmd + Tab shortcut, but it's worth it.

var webpackConfig = {
plugins: [
// Plugin to show any webpack warnings and prevent tests from running
function () {
this.plugin("done", function (stats) {
if (stats.compilation.warnings.length) {
// Log each of the warnings
stats.compilation.warnings.forEach(function (warning) {
console.log(warning.message || warning);
});
@Stuk
Stuk / heap-require.js
Created April 7, 2014 20:55
A little module that can reveal if memory usage jumps after requiring modules
// usage:
// require = require("./heap-require")(require);
// then use require as normal
module.exports = function (_require) {
var newRequire = function (id) {
var before = process.memoryUsage().heapUsed / 1024 / 1024;
var exports = _require(id);
var after = process.memoryUsage().heapUsed / 1024 / 1024;
@Stuk
Stuk / index.html
Created November 8, 2013 18:39
Using montage with deep urls
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>resolve</title>
<!-- Base URL ensures montage.js and style.css are loaded from the root of the server
Make sure this comes before any `script`, `style`, or `link` tags to ensure that
resources are loaded from the correct place. -->
<base href="/">
var defaultLocalizer = require("montage/core/localizer").defaultLocalizer;
defaultLocalizer.localize("hello_name", "Hello, {name}!")
.then(function (hi) {
console.log(hi({name: "World"})); // => "Hello, World!", or the translation at the "hello_name" key
});
defaultLocalizer.localize("cat", "Cat")
.then(function (cat) {
console.log(cat.toString()); // => "Cat", or the translation at the "cat" key
@Stuk
Stuk / index.js
Created October 7, 2013 01:14
Parsing POST requests in Joey
var joey = require("joey");
// Uses qs https://npmjs.org/package/qs
var qs = require("qs");
joey
.log()
.error()
.favicon()
.route(function ($) {
@Stuk
Stuk / exec.js
Created August 14, 2013 00:15
Wrap Node's `child_process.spawn` with a promise interface that rejects if the process has an error, or exits with a code other than zero.
var spawn = require("child_process").spawn;
var Q = require("q");
/**
* Wrap executing a command in a promise
* @param {string} command command to execute
* @param {Array<string>} args Arguments to the command.
* @param {string} cwd The working directory to run the command in.
* @return {Promise} A promise for the completion of the command.
*/