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 / 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 / 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%;
const classToObject = theClass => {
const originalClass = theClass || {}
const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(originalClass))
return keys.reduce((classAsObj, key) => {
classAsObj[key] = originalClass[key]
return classAsObj
}, {})
}
const sleep = (ms = 0) => new Promise(r => setTimeout(r, ms))
/**
* 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;
}, {});
@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
*/
@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 / capitalize.js
Created November 20, 2018 18:23
Capitalizes strings
/**
* Handles special characters too
*/
const capitalize = w => w.toLowerCase().replace(/(?:^|\s)\S/g, l => l.toUpperCase());
// recursive
const isPalindrome = str => {
if (str.length === 0 || str.length === 1) return true
if (str[0] === str[str.length - 1]) return isPalindrome(str.slice(1, str.length - 1))
return false
}
// efficiently in O(n) time
const isPalindrome2 = str => {
var half = Math.floor(str.length / 2)
@alex-cory
alex-cory / isLeapYear.js
Created December 4, 2017 03:15
Tells whether the specified year is a leap year
const isLeapYear = year => new Date(year,1,29).getDate() === 29