Skip to content

Instantly share code, notes, and snippets.

View johnpolacek's full-sized avatar

John Polacek johnpolacek

View GitHub Profile
@johnpolacek
johnpolacek / createPublicS3Bucket.js
Created September 15, 2023 13:27
Create a Public S3 Bucket via AWS SDK for Node
View createPublicS3Bucket.js
// Create the bucket
const bucketName = await getBucketName()
const createBucketCommand = new CreateBucketCommand({
Bucket: bucketName,
ObjectOwnership: "ObjectWriter",
})
await s3.send(createBucketCommand)
// Delete the block public access
const deletePublicAccessBlockCommand = new DeletePublicAccessBlockCommand({
@johnpolacek
johnpolacek / scratchpad.js
Created September 8, 2023 19:50
scratchpad
View scratchpad.js
function getNumMatchesAtCoordinate(matrix, x, y) {
const match = matrix[x][y]; // 1st is row, 2nd is col
let numMatches = 0; // at least 1 match
const maxLength = Math.min(matrix.length - x, matrix[0].length - y);
for (let i = 0; i < maxLength; i++) {
// increment row
let isMatch = true;
for (let j = 0; j <= i; j++) {
// increment col
// checks outermost row and column in the growing square
View api-utils.ts
import { NextApiRequest, NextApiResponse } from "next";
export async function handleApiRequest(
req: NextApiRequest,
res: NextApiResponse,
expectedReqMethod: string,
handleRequest: (body: any) => Promise<any>
): Promise<void> {
if (req.method === expectedReqMethod) {
try {
View transferToS3.ts
import AWS from "aws-sdk"
AWS.config.update({
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
})
const s3 = new AWS.S3()
const BUCKET = "your-bucket-name"
@johnpolacek
johnpolacek / FadeIn.tsx
Created March 7, 2023 12:11
Tailwind Component for Animate Fade-In and Up
View FadeIn.tsx
import React, { useEffect, useState } from "react"
const FadeIn = ({
children,
}: {
children: JSX.Element | JSX.Element[] | string
}) => {
const [animate, setAnimate] = useState("opacity-0 translate-y-10")
useEffect(() => {
@johnpolacek
johnpolacek / nextjs-api-stream-hooks.ts
Last active March 1, 2023 12:29
React Hooks for handline data from Next.js API routes that return readable streams from the OpenAI API
View nextjs-api-stream-hooks.ts
// use this hook to fetch data from a stream
// get the cumulative response as it is read
export const useStreamingDataFromPrompt = async (
prompt: string,
onData: (data: string) => void,
onDone?: () => void
) => {
const response = await fetch("/api/generate", {
method: "POST",
headers: {
@johnpolacek
johnpolacek / Fetch Example
Created February 19, 2022 21:43
Fetch Example
View Fetch Example
fetch('https://example.com').then(response => {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
})
.then(data => {
res.status(200).json({ data })
})
@johnpolacek
johnpolacek / CodeBlock.js
Created November 11, 2021 21:32
React Component for rendering blocks of code with Prism
View CodeBlock.js
import Highlight, { defaultProps } from "prism-react-renderer"
import github from "prism-react-renderer/themes/github"
const CodeBlock = ({ children, className }) => {
const language = className ? className.replace(/language-/, "") : "javascript"
return (
<Highlight
{...defaultProps}
code={children}
language={language}
@johnpolacek
johnpolacek / stockBuySell.js
Last active August 31, 2020 16:52
Given an array of numbers that represent stock prices (where each number is the price for a certain day), find 2 days when you should buy and sell your stock for the highest profit.
View stockBuySell.js
const stockBuySell = (days) => {
const best = days.reduce((bestResult, price, i) => {
const buyDays = days.slice(i+1)
if (buyDays.length) {
const highest = Math.max.apply(null, days.slice(i+1))
const roi = highest - price
if (roi > bestResult.roi) {
bestResult = {buyDay: i, sellDay: days.indexOf(highest), roi}
}
}
@johnpolacek
johnpolacek / prettier.package.json
Created August 15, 2020 02:25
NPM prettier format and format-watch script
View prettier.package.json
{
"scripts": {
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"format-watch": "npm run format && onchange \"**/*.{js,jsx,json,md}\" -- prettier --write {{changed}}",
},
"devDependencies": {
"onchange": "^7.0.2",
"prettier": "^2.0.5"
}
}