Skip to content

Instantly share code, notes, and snippets.

View csandman's full-sized avatar

Chris Sandvik csandman

View GitHub Profile
@csandman
csandman / path.ts
Created July 20, 2023 04:06
The `Path` TypeScript utility function used in react-hook-form to generate a type for every possible object dot path
/* eslint-disable @typescript-eslint/no-explicit-any */
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
/**
* Checks whether T1 can be exactly (mutually) assigned to T2
* @typeParam T1 - type to check
* @typeParam T2 - type to check against
* ```
* IsEqual<string, string> = true
* IsEqual<'foo', 'foo'> = true
@csandman
csandman / itunes.js
Last active February 22, 2022 20:24
Grab the response from the iTunes API for a book search and get the hi-res cover url in the process
import { URLSearchParams } from "url";
/**
* Check whether a URL string is in a proper format
*
* @param {string} str A URL string to check for the correct structure
* @returns
*/
const isUrl = (str) => {
if (typeof str !== "string") {
@csandman
csandman / query-string.js
Created April 16, 2021 19:54
Utility functions for parsing a query parameter string and stringify-ing a query parameter object
/**
* Stringify an object containing query parameters to be used in a request
*
* @param {object} paramObj An object containing query paramaters
* @returns {string} A string in the format of a query param
*/
export const stringifyQuery = (paramObj) =>
new URLSearchParams(paramObj).toString();
/**
@csandman
csandman / README.md
Last active October 11, 2023 10:23
Chakra UI React Select

Chakra React Select

UPDATE: I finally made an NPM package for this component! It is made with TypeScript, and now has a fully customizable styling system. It is also far ahead of this wrapper at this point. Check it out here if you'd like: https://github.com/csandman/chakra-react-select

In order to use this component, you can implement it and use it like you would normally use react-select. It should accept all of the props that the original takes, however customizing the theme or the components could break this implementation. There are also a few extra things you can do with this wrapper that pull from the chakra library.

  • You can pass the size prop with either sm, md, or lg. These will reflect the sizes available on the Chakra <Input /> component (with the exception of xs because it's too small to work).
  • In your options objects, you can add the key isFixed to emulate the exa
// milliseconds in 1 second
export const MS_IN_S = 1000;
// seconds in 1 minute
export const S_IN_M = 60;
// minutes in 1 hour
export const M_IN_H = 60;
// hours in 1 day
export const H_IN_D = 24;
// days in 1 year
export const D_IN_Y = 365;
@csandman
csandman / debounce-swr.js
Last active November 17, 2023 09:12
Debounce useSwr
// originally from: https://github.com/vercel/swr/issues/110#issuecomment-552637429
import useSWR from 'swr';
import useDebounce from 'hooks/use-debounce';
const App = () => {
const [search, setSearch] = useState('');
const debouncedSearch = useDebounce(search, 1000);
const { data: res, isValidating } = useSWR(
() => debouncedSearch ? `/api/suggestion?value=${debouncedSearch}` : null,
Api.fetcher,
@csandman
csandman / compare-emails.sql
Last active March 4, 2021 22:32
Check if 2 emails in match in tsql
-- Written for TSQL
CREATE FUNCTION dbo.CompareEmails(
@Email1 AS NVARCHAR(MAX) = '',
@Email2 AS NVARCHAR(MAX) = ''
)
RETURNS BIT
AS
BEGIN
-- Basic check to see if each string is an email
IF LEN(@Email1) = 0
@csandman
csandman / remove-cached-git-files.sh
Created February 12, 2021 20:09
A git command to remove all files that have been added to the .gitignore from github
git rm -r --cached . && git add . && git commit -am "Remove ignored files" && git push
@csandman
csandman / use-scroll.js
Last active October 7, 2020 18:37
A react hook to add a scroll event listener with optional callbacks
// inspired by:
// https://gist.github.com/joshuacerbito/ea318a6a7ca4336e9fadb9ae5bbb87f4
import { useEffect, useState } from 'react';
const isValidFunction = (func) => {
return func && typeof func === 'function';
};
export default function useScroll({ onScroll, onScrollUp, onScrollDown }) {
const [scroll, setScroll] = useState(
@csandman
csandman / fetch-with-timeout.js
Created June 17, 2020 20:11
An implementation of the fetch api with a timeout option
function fetchWithTimeout(url, options, timeout, onTimeout) {
const timer = new Promise((resolve) => {
setTimeout(resolve, timeout, {
timeout: true,
});
});
return Promise.race([fetch(url, options), timer])
.then((response) => {
if (response.timeout) {
onTimeout();