Skip to content

Instantly share code, notes, and snippets.

This middleware does a few interesting things:

  • Ensures a url shape in the zustand store, where we'll store URL information.
  • Assumes we will be storing our url state slice in the ?state search parameter after it has been stringified and base 64 encoded.
  • On creation, decodes stores state from the ?state search parameter into the url slice of our store.
  • After each state update, updates the ?state search parameter with the new url state slice.
  • Sets up an event listener that listens for popstate and re-decodes the state from the URL into our store.
@cef62
cef62 / fixPackageJsonExports.mjs
Created May 30, 2022 15:30
Refactor `package.json` exports to be compatible with Typescript 4.7+
// @ts-check
import path from 'path'
import { readFile, writeFile } from 'fs/promises'
import glob from 'tiny-glob'
/**
* @typedef {object} LegacyExportField
* @property {string} [types]
* @property {string} [import]
@cef62
cef62 / ag-replace.sh
Created July 6, 2017 08:37
Search and replace using the silver searcher and sed
#!/usr/bin/env zsh
# Currently the script will choke on tilde characters used in either search or
# replace terms
# inspired by
# https://gist.github.com/hlissner/db74d23fc00bed81ff62
# https://gist.github.com/kates/5915285
# ag
@cef62
cef62 / sed cheatsheet
Created January 5, 2018 15:31 — forked from un33k/sed cheatsheet
magic of sed -- find and replace "text" in a string or a file
FILE SPACING:
# double space a file
sed G
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
sed '/^$/d;G'
@cef62
cef62 / flow-type.js
Created June 12, 2017 06:32
Flow-type workaround for peer dependencies
#!/usr/bin/env node
const { installFlowTypeDefinitions } = require('./flow-typed')
const { echo, exit } = require('shelljs')
installFlowTypeDefinitions().then(() => exit(0)).catch(err => {
echo(err)
exit(1)
})
import createBabelFile from 'babel-file';
import createBabylonOptions from 'babel-options';
import {buildCodeFrameError} from 'babel-errors';
let code = `
console.log('hello world');
`;
let file = createBabelFile(code, {
filename: 'hello-world.js',
@cef62
cef62 / TweetPresenter.js
Created April 27, 2017 15:35 — forked from jevakallio/TweetPresenter.js
Tweet Presenter!
// paste this to chrome console on anybody's twitter page
// and it'll turn into an instant presentation :)
(function TweetPresent() {
const presenter = $(`<div id="tpd" style=" position: fixed; top: 0; left: 0; height: 100vh; width: 100vw; padding: 10vh 10vw; box-sizing: border-box; background-color: white; font-size: 5vw; text-align: center; z-index: 9999; display: flex; align-items: center; justify-content: center;"></div>`);
const tweets = $$(`div.tweet p.js-tweet-text`)
.map(el => el.innerHTML)
.map(t => ({t, s: Math.random()}))
.sort((a, b) => a.s > b.s ? -1 : 1)
.map(c => c.t);
@cef62
cef62 / .flowconfig
Created April 27, 2017 14:03 — forked from MoOx/.flowconfig
flow config webpack adjustements to avoid the "Required module not found" for png, css, svg etcc
# ...
[options]
# webpack loaders
module.name_mapper='.*\.css$' -> '<PROJECT_ROOT>/flow/stub/css-modules.js'
module.name_mapper='.*\.\(svg\|png\|jpg\|gif\)$' -> '<PROJECT_ROOT>/flow/stub/url-loader.js'
@cef62
cef62 / isValidISODate.js
Last active March 3, 2017 13:50
Validate ISO date strings with custom separators
export default function isValidISODate(dateString, separator, minYear = 1980, maxYear = 2030) {
const sep = separator ? `\\${separator}` : ''
const regex = new RegExp(`^(\\d{4})${sep}{0,1}(\\d{2})${sep}{0,1}(\\d{2})$`)
const groups = dateString.match(regex)
if (!groups) {
return false
}
@cef62
cef62 / iterm2.md
Created February 22, 2017 14:11
iterm2 cheatsheet

Tabs and Windows

Function Shortcut
Previous Tab + Left Arrow
Next Tab + Right Arrow
Go to Tab + Number
Go to Window + Option + Number
Go to Split Pane by Direction + Option + Arrow
Go to Split Pane by Order of Use + ] , + [