Skip to content

Instantly share code, notes, and snippets.

View alex-cory's full-sized avatar
🔥
🤔

Alex Cory alex-cory

🔥
🤔
View GitHub Profile
@alex-cory
alex-cory / toCase.js
Created November 20, 2018 18:42
converts strings to a different case
/**
* this_is_snake_case
* thisIsCamelCase
* this-is-cabob-case
*/
const camelToSnakeCase = w => w.replace(/([A-Z])/g, l => '_' + l.toLowerCase())
const toCamel = s => s.replace(/(\-[a-z])/g, l => l.toUpperCase().replace('-',''))
const cabobToCamelCase = s => s.replace(/([A-Z])/g, l => "-"+l.toLowerCase())
@alex-cory
alex-cory / hooks.js
Last active January 10, 2019 22:42
Reusable React Hooks
import { useState, useEffect } from 'react'
/**
* Examples of Reusable Custom Hooks
* - useLodash https://github.com/chantastic/use-lodash
* - useEvents https://github.com/sandiiarov/use-events
* - list of hooks https://github.com/streamich/react-use
* - awesome hooks https://github.com/rehooks/awesome-react-hooks
*/
/**
* Only take the specified fields out of the object
*/
const pick = (o, ...fields) =>
fields.reduce((acc, x) => {
if ((o || {}).hasOwnProperty(x)) {
acc[x] = o[x];
}
return acc;
}, {});
const sleep = (ms = 0) => new Promise(r => setTimeout(r, ms))
const classToObject = theClass => {
const originalClass = theClass || {}
const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(originalClass))
return keys.reduce((classAsObj, key) => {
classAsObj[key] = originalClass[key]
return classAsObj
}, {})
}
@alex-cory
alex-cory / css2obj.js
Last active December 5, 2019 00:56
Converts a css string to a React js object using tagged template literal syntax
const css2obj = (strings, ...vals) => {
const css = strings.reduce((acc, str, i) => acc + str + (vals[i] || ''), '')
const r = /(?<=^|;)\s*([^:]+)\s*:\s*([^;]+)\s*/g, o = {}
css.replace(r, (m,p,v) => o[p.replace(/-(.)/g, (_,p) => p.toUpperCase())] = v)
return o
}
const actual = css2obj`
position: fixed;
left: 50%;
@alex-cory
alex-cory / mac-setup.sh
Last active January 4, 2023 14:53
My mac setup
#!/usr/bin/env bash
#
# BUGS:
# 1. not adding settings to .zshrc from below (i.e. default theme, packages, custom functions, etc.)
# 2. not auto-hiding doc
# 3. not auto-hiding menu bar
# 4. not adding .vimrc
# 5. not automatically copying iTerm profile, Option + Space not setup to toggle iTerm, doesn't default to opening iTerm at startup
#
# Bootstrap script for setting up a new OSX machine
@alex-cory
alex-cory / load-initial-todos.js
Last active April 19, 2020 01:54
for blog post
const { data: todos, loading } = useFetch('https://jsonplaceholder.typicode.com/todos', {
data: [], // <- we set this so our initial `data` is an array
}, []) // <- this empty array signifies, run onMount
@alex-cory
alex-cory / skeleton.js
Created September 6, 2020 17:36
Simplest skeleton/placeholder loading component ever.
import styled, { css, keyframes } from 'styled-components'
export default Skeleton export const skeletonKeyframes = keyframes`
0% {
background-position: -200px 0;
}
100% {
background-position: calc(200px + 100%) 0;
}
`
import { PusherProvider } from '@harelpls/use-pusher'
// _app.js
const config = {
clientKey: 'your-pusher-client-key',
cluster: 'us2',
// required for private/presence channels
authEndpoint: '/api/pusher/auth'
}
const App = ({ Component, pageProps }) => (