Skip to content

Instantly share code, notes, and snippets.

View git-clone-monorepo.sh
#!/usr/bin/env bash
BRANCH=${BRANCH:main}
git clone --branch $BRANCH --depth 1 --single-branch --no-tags $1
View example.js
import { useAsync } from './use-async.js';
import { useAsyncCallback } from './use-async-callback.js';
export function Cat() {
const { data, error, loading } = useAsync(
async (signal) => {
const cats = await fetch(
'https://api.thecatapi.com/v1/images/search?size=full',
{ signal }
);
View root-relative-require.md

Root-Relative Require with -

Some folks really don't like typing ../../.., etc. There are many ways around this, but one of the simplest is to use npm's file: feature.

Assume a project that looks like this:

my-project/
├── src/
│   ├── some/
View lint-staged.sh
#!/usr/bin/env bash
JS_FILES="$(git diff --name-only --staged --diff-filter=ACM | grep '.js$')"
if [ -n "$JS_FILES" ]; then
set -e
echo 'eslint' && ./node_modules/.bin/eslint --fix $JS_FILES
echo 'prettier' && ./node_modules/.bin/prettier --write $JS_FILES
echo 'git add' && git add $JS_FILES
fi
View cascade.js
function createCascade() {
const middleware = [];
async function run(ctx, next = () => {}) {
// Reverse list to .pop() instead of .shift() for performance
const handlers = [...middleware, next].reverse();
async function runNext() {
if (handlers.length) {
const handler = handlers.pop();
View .editorconfig
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true
View test.js
const tests = new Set();
const t = {
total: 0,
passed: 0,
failed: 0,
comment(message) {
console.log(`# ${message}`);
},
@shannonmoeller
shannonmoeller / container.js
Last active Sep 8, 2018
Dependency-injection container example using vanilla JS.
View container.js
let uid = 0;
class Foo {
constructor({ bar }, id) {
this.uid = uid++;
this.id = id;
this.bar = bar;
}
}
@shannonmoeller
shannonmoeller / store.md
Last active May 8, 2020
JavaScript stores for devs who like stores.
View store.md

Arbitrary Values

export function createStore(state) {
  const listeners = new Set();

  return {
    get() {
      return state;
    },