Skip to content

Instantly share code, notes, and snippets.

View bryanmylee's full-sized avatar
🔬
Learning new things!

Bryan Lee bryanmylee

🔬
Learning new things!
View GitHub Profile
@bryanmylee
bryanmylee / tryResult.ts
Last active August 16, 2023 06:19
Handle errors more declaratively without try catch
/* eslint-disable @typescript-eslint/no-explicit-any */
export type Result<T, E> = [T, undefined] | [undefined, E];
/**
* Handle errors more declaratively without try catch.
*
* ```
* try {
* const res = doSomething(30);
* } catch (err) {
@bryanmylee
bryanmylee / Unmasked.ts
Last active August 3, 2023 05:44
`Unmasked` utility for GraphQL fragments
type Decr = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
type Obj = Record<string, unknown>;
type Prettify<T> = {
[K in keyof T]: T[K];
} & NonNullable<unknown>;
type ValuesOf<T> = T[keyof T];
@bryanmylee
bryanmylee / context.ts
Created April 7, 2023 10:37
Svelte type-safe context
import {getContext, setContext} from 'svelte';
interface Context<TValue> {
get: () => TValue | null;
set: (currentValue: TValue) => void;
}
/**
* Allow strongly typed Svelte context getters and setters while using a unique
* Symbol instead of a string key to avoid any potential key conflicts.
@bryanmylee
bryanmylee / BottomSheet.tsx
Last active March 30, 2024 17:53
Tamagui Bottom Sheet
import {Anchor, Button, H1, Paragraph, Separator, Sheet, Theme, XStack, YStack} from '@slipbox/ui';
import {ChevronDown, ChevronUp} from '@tamagui/lucide-icons';
import {useState} from 'react';
function SheetDemo() {
const [open, setOpen] = useState(false);
const [position, setPosition] = useState(0);
return (
<>
<Button
@bryanmylee
bryanmylee / Podfile
Created July 26, 2022 13:41
React Native 0.69 with Firebase
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
platform :ios, '12.4'
install! 'cocoapods', :deterministic_uuids => false
production = ENV["PRODUCTION"] == "1"
target 'Wave' do
pod 'Firebase', :modular_headers => true
@bryanmylee
bryanmylee / batch-squoosh.sh
Created June 5, 2022 08:29
Batch compress images with Squoosh
find $1 -name "*.jpg" -exec bash -c $'file="{}"; npx @squoosh/cli --webp \'{"quality":75,"target_size":0,"target_PSNR":0,"method":4,"sns_strength":50,"filter_strength":60,"filter_sharpness":0,"filter_type":1,"partitions":0,"segments":4,"pass":1,"show_compressed":0,"preprocessing":0,"autofilter":0,"partition_limit":0,"alpha_compression":1,"alpha_filtering":1,"alpha_quality":100,"lossless":0,"exact":0,"image_hint":0,"emulate_jpeg_size":0,"thread_level":0,"low_memory":0,"near_lossless":100,"use_delta_palette":0,"use_sharp_yuv":0}\' -d ./output/ "$file"' \;
@bryanmylee
bryanmylee / ReturnTypes.ts
Created April 23, 2022 09:48
Given an array of functions, get the type for an array of the function return types
type ReturnTypes<T extends Array<(...a: any[]) => any>> = {
[P in keyof T]: T[P] extends (...a: any[]) => infer R ? R : never
}
@bryanmylee
bryanmylee / type_factory_method.py
Created March 15, 2022 13:00
Add type hints to a Python factory method
from typing import Type, TypeVar
T = TypeVar('T', bound='TrivialClass')
class TrivialClass:
# ...
@classmethod
def from_int(cls: Type[T], int_arg: int) -> T:
# ...
@bryanmylee
bryanmylee / .storybook_main.cjs
Last active November 7, 2022 17:20
Svelte (kit) + Storybook + Webpack 5 configuration to support all modern features (optional chaining and nullish coalescence in templates)
const path = require('path');
const postcss = require('postcss');
const preprocess = require('svelte-preprocess');
const replaceFileExtension = (filePath, newExtension) => {
const { name, root, dir } = path.parse(filePath);
return path.format({
name,
root,
dir,
@bryanmylee
bryanmylee / Generics.svelte
Created January 16, 2022 17:36
Generic type definitions for Svelte components
<script lang="ts">
type T = $$Generic
interface $$Slots {
default: { // slot name
item: T
index: number
}
}
export let items: T[] = []
</script>