Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Rich-Harris / http-apis.md
Last active Oct 2, 2020
Next-gen Node HTTP APIs
View http-apis.md

I saw this poll on Twitter earlier and was surprised at the result, which at the time of writing overwhelmingly favours option 1:

Screen Shot 2020-09-10 at 10 19 22 AM

I've always been of the opinion that the (req, res, next) => {} API is the worst of all possible worlds, so one of two things is happening:

  • I'm an idiot with bad opinions (very possibly!)
  • People like familiarity
View lookup-vs-nested-foreach.js
// re https://stackoverflow.com/questions/62621666/how-can-i-reliably-merge-objects-from-these-2-arrays-and-loop-the-resulting-arra/62623714?noredirect=1#comment110766294_62623714
function with_nested_for_each([a, b]) {
const result = [];
a.forEach(p => {
b.forEach(q => {
if (p.code === q.code) {
result.push(Object.assign({}, p, q));
}
@Rich-Harris
Rich-Harris / README.md
Last active Jul 17, 2020
Svelte style properties naive implementation
View README.md

This is a copy of an early attempt to imagine an implementation of 'style properties' in Svelte, as described in this RFC. It is preserved here to satisfy curiosity, or in case we hate the new one.


Style properties — handily distinguishable from regular properties by the leading -- used by CSS custom properties — are passed down to components through a separate channel. The example at the top of this RFC might be converted to the following JavaScript:

const slider = new Slider({
  props: {
    value: ctx.value,
@Rich-Harris
Rich-Harris / README.md
Last active Apr 26, 2020
Testing array.splice vs array.pop vs set.delete
View README.md

You have an array. Its sort order doesn't matter. You want to remove an item from this array.

The obvious thing to do would be to use splice:

function remove(array, item) {
  const index = array.indexOf(item);
  array.splice(index, 1);
}
@Rich-Harris
Rich-Harris / README.md
Last active Sep 24, 2020
Svelte vs Knockout
View README.md
@Rich-Harris
Rich-Harris / index.js
Last active Jan 22, 2020
whatwg stream utils
View index.js
const fs = require('fs');
const { ReadableStream, TransformStream, WritableStream} = require('web-streams-polyfill/ponyfill/es2018');
function createReadStream(file, opts) {
return new ReadableStream({
start(controller) {
const stream = fs.createReadStream(file, opts);
stream.on('readable', () => {
const data = stream.read();
controller.enqueue(data);
@Rich-Harris
Rich-Harris / README.md
Last active Dec 20, 2019
Source bytes: React vs Ember Octane vs Svelte
View README.md

I'm procrastinating instead of working on a conference talk, so I decided to implement this component in Svelte to see which framework is most expressive:

Screen Shot 2019-03-30 at 16 42 16

Ember Octane

Here's the original, with the addition of an import for dataUriAsSrc (and updated per @chriskrycho's comment):

<img
@Rich-Harris
Rich-Harris / README.md
Last active Dec 20, 2019
Optimising CSS modules
View README.md

https://github.com/w3c/webcomponents/issues/759

Suppose you have an app like this, and your bundler turns .css files into strings:

// main.js
import './x-foo.js';
import './x-bar.js';

document.body.innerHTML = `<x-foo></x-foo><x-bar></x-bar>`;
@Rich-Harris
Rich-Harris / extensions.md
Last active Aug 24, 2020
What should be the canonical file extension for Svelte components?
View extensions.md

So far, most of us have been writing Svelte components into .html files, with a few exceptions (Parcel users, for example, have additional constraints to work with). We're currently discussing whether this should change.

Once you've read the pros and cons, please vote!

.html

Pros:

  • It signals that you can use your existing HTML (and CSS) knowledge
  • Valid HTML is valid Svelte
@Rich-Harris
Rich-Harris / streams.md
Last active Nov 20, 2019
Notes on Node streams
View streams.md

I have a real mental blockage around this stuff. Partly because the docs could probably be a little better, partly because it's changed over the years, probably partly because I'm dumb. Whatever. I'm going to try and document how to do stuff I occasionally need to do, so I can refer to it next time.

Return value of pipe

x.pipe(y) === y; // true
x.on(someEvent, callback) === x; // true
You can’t perform that action at this time.