Skip to content

Instantly share code, notes, and snippets.

View tomByrer's full-sized avatar
🎦
researching video players

Tom Byrer tomByrer

🎦
researching video players
View GitHub Profile
if (typeof window!=='undefined' && navigator.serviceWorker && navigator.serviceWorker.controller) {
let reloadOnNext = false;
let pushState = history.pushState;
history.pushState = function(state, title, url) {
pushState.call(this, state, title, url);
if (reloadOnNext===true) location.reload(true);
};
navigator.serviceWorker.controller.addEventListener('statechange', e => {
@hharnisc
hharnisc / index.js
Last active November 28, 2018 06:27
Webpack Dev Middleware (w/ Hot Module Replacement) served by `Micro`
const fs = require('fs');
const webpack = require('webpack');
const { send } = require('micro');
const config = require('./webpack.config');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const compiler = webpack(config);
const middleware = (next) => {
const mw = webpackMiddleware(compiler, {
@zeusdeux
zeusdeux / destructuring.js
Last active June 16, 2021 09:44
Ways I use destructuring in JS
// basic destructuring
const { key1 } = { key1: 10 } // key1 = 10
const [x, y, z] = [1, 2, 3] // x = 1, y = 2, z = 3
const [head, ...tail] = [1, 2, 3] // head = 1, tail = [2, 3]
const { a: { b } } = { a: { b: 20 } } // b = 20
// storing value in a variable with a different name
const { key1: varForKey1 } = { key1: 20 } // varForKey1 = 20
const { 'quoted-key': x } = { 'quoted-key': 10 } // x = 10
@Rich-Harris
Rich-Harris / prepack-svelte.md
Last active May 19, 2022 11:02
Is Prepack like Svelte?

Note: I'm not involved in Prepack in any way — please correct me if I say anything incorrect below!

A few people have asked me if Prepack and Svelte are similar projects with similar goals. The answer is 'no, they're not', but let's take a moment to explore why.

What is Prepack?

Prepack describes itself as a 'partial evaluator for JavaScript'. What that means is that it will run your code in a specialised interpreter that, rather than having some effect on the world (like printing a message to the console), will track the effects that would have happened and express them more directly.

So for example if you give it this code...

@una
una / text.md
Last active April 12, 2022 03:29
Cleanup Branches
  1. Go to the remote repo and delete outdated branches

  2. Then either:

    • Locally cleanup only merged branches: git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
  • Locally remove fetched, outdated branches: git for-each-ref --format='%(refname:short) %(upstream)' refs/heads/ | awk '$2 !~/^refs\/remotes/' | xargs git branch -D
@nepsilon
nepsilon / how-to-append-line-numbers-in-front-of-each-line-in-a-file.md
Last active February 8, 2017 09:10
How to append line numbers in front of each line in a file? — First published in fullweb.io issue #86

How to append line numbers in front of each line in a file?

Just use cat. No kidding, cat has the -n option to number the output lines, starting at 1:

cat -n file.txt

This works well both on Linux and Mac. Use the shell redirection to keep the output in a new file (don’t override the original file with the redirection though).

var str = 'class ಠ_ಠ extends Array {constructor(j = "a", ...c) {const q = (({u: e}) => {return { [`s${c}`]: Symbol(j) };})({});super(j, q, ...c);}}' +
'new Promise((f) => {const a = function* (){return "\u{20BB7}".match(/./u)[0].length === 2 || true;};for (let vre of a()) {' +
'const [uw, as, he, re] = [new Set(), new WeakSet(), new Map(), new WeakMap()];break;}f(new Proxy({}, {get: (han, h) => h in han ? han[h] ' +
': "42".repeat(0o10)}));}).then(bi => new ಠ_ಠ(bi.rd));';
try {
eval(str);
} catch(e) {
alert('Your browser does not support ES6!')
}

In an effort to improve the quality of search results on sites like https://npmjs.com, there has been more and more discussion about factoring in the ranking/scoring of dependencies to influence search results. The general idea is that a library should be ranked not only on its own merits, but dependencies should weigh into the score as well.

I’m not sure what my opinion is on this yet. I was initially in favor of this, and still might be, but this document is a summary of some things that crossed my mind about the topic.

Should dependencies weigh into the search score for a library?

  1. Some very popular libraries on npm are very poorly written, but solve problems that are sufficiently complex that few alternatives exist. From a scoring standpoint, this alone wouldn't matter because the law of averages would even it out. However...
  2. It's possible for developers to write high quality, stable code on top of lesser quality libraries.
  3. The better quality the code, and more knowledgable the maintainer
@ljharb
ljharb / array_iteration_thoughts.md
Last active April 15, 2025 03:33
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

@nybblr
nybblr / 1-easy.js
Last active July 13, 2022 03:40
3 examples of using Async Generators and Async Iteration in JavaScript!
// Create a Promise that resolves after ms time
var timer = function(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
// Repeatedly generate a number starting
// from 0 after a random amount of time
var source = async function*() {