Skip to content

Instantly share code, notes, and snippets.

View sethdavis512's full-sized avatar
🤖

Seth Davis sethdavis512

🤖
View GitHub Profile
function getSafe(fn) {
try {
return fn();
} catch (e) {
return undefined;
}
}
function allTrue(conditions: boolean[]): boolean {
return conditions.every(Boolean);
async function encryptData(data, key) {
const encodedData = new TextEncoder().encode(data);
const iv = crypto.getRandomValues(new Uint8Array(12)); // Initialization vector
const encryptedData = await crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
encodedData
function getEnvVariable(key: string): string {
if (key === undefined) {
throw Error(`"${key}" is undefined`);
}
const value = process.env[key];
if (!value) {
throw Error(
`Environment variable "${key}" does not exist on process.env`
@sethdavis512
sethdavis512 / global.code-snippets
Created September 24, 2024 14:25
VS Code snippets
{
"Component": {
"prefix": "mkComponent",
"body": [
"import React, { type ReactNode } from 'react';",
"",
"interface ${TM_FILENAME_BASE/(.*)\\..+$/$1/}Props {",
" children: ReactNode;",
"}",
"",
import { ButtonHTMLAttributes, ReactNode } from "react";
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
children: ReactNode;
}
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
<span>
{new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
signDisplay: "never",
}).format(option.price - selectedOption.price)}
</span>
@sethdavis512
sethdavis512 / as-child.tsx
Last active May 24, 2024 17:43
asChild example
import { Slot, type AsChildProps } from "./slot.tsx"
type ButtonProps = AsChildProps<
React.ButtonHTMLAttributes<HTMLButtonElement>
> & {
style?: React.CSSProperties
className?: string
}
function Button({ asChild, ...props }: ButtonProps) {
@sethdavis512
sethdavis512 / Grid.tsx
Created April 14, 2024 01:13
Grid component (Tailwind)
import { type ReactNode } from 'react';
interface GridProps {
children: ReactNode;
as?: keyof JSX.IntrinsicElements;
className?: string;
}
export default function Grid({ as = 'div', children, className }: GridProps) {
const Component = as;
const getRandomBool = (chance: number = 50): boolean =>
Math.round(Math.random() * 100) >= chance;