Skip to content

Instantly share code, notes, and snippets.

Avatar

Piotr Monwid-Olechnowicz hasparus

View GitHub Profile
View MyComponents.spec.tsx
import { ReactRenderer, StoryObj } from '@storybook/react'
import { ComponentAnnotations } from '@storybook/types'
import { describeStories } from './tiniest-storybook-test-suites'
import * as stories from './MyComponent.stories'
describeStories(stories)
@hasparus
hasparus / generator.ts
Last active April 27, 2022 08:12
freebooter-generator/lib
View generator.ts
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// generator.js
// written and released to the public domain by drow <drow@bin.sh>
// http://creativecommons.org/publicdomain/zero/1.0/
// Adapted and rewritten to TypeScript by hasparus <hasparus@gmail.com>
// - used PRNG instead of Math.random
// - added context and `ctx -> text` functions
import { random } from "./random";
@hasparus
hasparus / #All Stories Actually Render the Component.md
Last active April 22, 2022 10:26
ensure all stories render the component they describe
View #All Stories Actually Render the Component.md

It's easy to overlook that your component stories stopped working after changes to the component source. If we're not reusing our stories in unit tests and thus ensure they still work on CI in every Pull Request, we need to remember to check them during code review.

When you're hotfixing a production bug you have more serious concerns than not breaking the docs. That's understandable. Every app I worked on that used Storybook eventually had some broken stories. Fixing them lands at the bottom of the backlog; they're just some component examples and those components work on production now! You're going to do it only if you actually need those stories.

View unexpected-access-proxy.ts
const UNEXPECTED_ACCESS = Symbol() as never
export function throwsOnUnexpectedAccess<T extends object>(target: T): T {
return new Proxy(target, {
get: (target, key) => {
const value = target[key as keyof typeof target]
if (value === UNEXPECTED_ACCESS) {
throw new Error(`Unexpected access to "${String(key)}"`)
}
return value
},
@hasparus
hasparus / windows-auto-darkmode.ps1
Last active December 1, 2021 21:38
A PowerShell script setting light mode during the day and dark mode after 18:30.
View windows-auto-darkmode.ps1
# Set your triggers to 06:00, log in, and 18:30.
# Set this to false if you like light Windows UI.
# Personally, I'm conflicted so I change it back and forth every few months.
$SYSTEM_ALWAYS_DARK = $TRUE
$lightTheme = @{ AppsUseLightTheme = 1; SystemUsesLightTheme = 1 }
$darkTheme = @{ AppsUseLightTheme = 0; SystemUsesLightTheme = 0 }
# We set light mode between 06:00 and 18:30.
@hasparus
hasparus / compare-jest-snapshots-between-files.ts
Created August 11, 2021 08:06
When you rename a test file the snapshot name changes and the old snapshot is declared obsolete. Thankfully, as Jest snapshots are JS files exporting a bunch of strings, we can compare them with a simple script instead of going through them by hand.
View compare-jest-snapshots-between-files.ts
function requireSnapshots(paths: string[]) {
const map = new Map<string, string>()
for (const path of paths) {
const module = require(path)
for (const name of Object.keys(module)) {
if (map.has(name)) {
throw new Error(`Conflict on snapshot "${name}" in ${paths}`)
}
map.set(name, module[name])
@hasparus
hasparus / Microsoft.Powershell_profile.ps1
Last active May 26, 2022 17:13
A guide on setting up on new laptops for future me, and a manifest of all the tools I installed.
View Microsoft.Powershell_profile.ps1
# Git Completions
Import-Module posh-git
# Prompt
Invoke-Expression (&starship init powershell)
# Aliases
Set-Alias g git
# Utils
View going-too-far-with-matchers-type.ts
// TypeScript Playground: https://tsplay.dev/mq8eYN
/// <reference types="@types/jest" />
import type { MatcherState } from 'expect';
const matchers = {
toHaveWordsCount(this: MatcherState, sentence: string, wordsCount: number) {
// implementation redacted
},
View examples-auth-db.tsx
// Imagine that @prisma/client is a normal library and it doesn't have different types in userspace and libraries
import {GetEvents, LogDefinition, LogLevel, PrismaClient, PrismaClientOptions} from "@prisma/client"
import {exec} from "child_process"
interface EnhancedPrismaClient<
// TypeScript doesn't have higher (i.e. * -> * -> *) order types, so
// we can't use anything like ConstructorParameters<typeof PrismaClient>,
// because ConstructorParameter will "strip" generics.
//
// The only way to preserve generic parameters is to copy them.
View collectGraphQLFragments.ts
// @ts-ignore
import GatsbyParser from "gatsby/dist/query/file-parser";
import path from "path";
import glob from "glob";
/**
* Collect all graphql fragments from a directory
* @see https://github.com/gatsbyjs/gatsby/issues/12155#issuecomment-618424527
*/
export const collectGraphQLFragments = async (