Skip to content

Instantly share code, notes, and snippets.

View granmoe's full-sized avatar
🔔
Maximizing the frame rate, resolution, and screen size of my mind 😑

Matt Granmoe granmoe

🔔
Maximizing the frame rate, resolution, and screen size of my mind 😑
View GitHub Profile
@granmoe
granmoe / React Join Children
Last active December 7, 2022 14:50
Ever wanted to join react children like you join an array?
This file is only here to provide the title of the gist
@granmoe
granmoe / __responsive styled components
Last active January 6, 2021 02:22
Programmatically create responsive styled components with react and styled-components
// this file is just here to change the name of the gist
import React from 'react'
import styled from 'styled-components'
const makeResponsiveComponent = (rulesets, tagName = 'div') =>
styled(tagName)`${buildStyles(rulesets)}`
const buildStyles = rulesets =>
rulesets.reduce(
(cssString, { constraint, width, rules }) =>
@granmoe
granmoe / form-validation-and-utils.jsx
Last active September 23, 2020 03:59
Simple react form validation plus a few tiny utilities, via a higher order component
import React from 'react'
export default options => {
return WrappedComponent => class FormValidation extends React.Component {
constructor () {
super()
this.validate = options.validate
this.state = options.fields.reduce((result, field) => {
result.fields.push(field)
/**
* Given an array of Person objects, returns the root PersonTreeNode (the CEO).
* @param {Person[]} employees - An array of Person objects representing all the employees of the company.
* @returns {PersonTreeNode} The CEO of the organization.
*/
function generateTree(employees) {
let ceo = null
const visitedById = new Map()
const visitedByManagerId = new Map()
@granmoe
granmoe / int-to-roman-numeral.js
Created February 6, 2019 02:11
Convert an integer of 9,999 or less to a roman numeral with JS (just a terrible JS interview question that got stuck in my head)
const convertToRomanNumerals = int => {
const romanNumerals = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
const createRomanNumeralRange = (I, V, X) => [
'',
I,
`${I}${I}`,
`${I}${I}${I}`,
`${I}${V}`,
V,
`${V}${I}`,
@granmoe
granmoe / example.js
Last active November 10, 2018 21:21
A weird way of testing react hooks. It's interesting that it's possible, but you should do this instead: https://gist.github.com/granmoe/1316210dd63fc09bb012acfbcd0e28a8
// I like how this approach lets me test my hook as if it's a pure function,
// but something about this feels weird 🤔 🤫
test('use-validation', () => {
let counter = 0
render(
<Test>
{fields => {
const fooFuncs = {
onChange: fields.foo.onChange,
@granmoe
granmoe / hook-test.js
Created November 10, 2018 21:12
A way to test React hooks
import React from 'react'
import { render, fireEvent } from 'react-testing-library'
import useValidation from '..'
describe('use-validation', () => {
const Test = ({ mockFunc, handleSubmit }) => {
const { fields } = useValidation({
fields: {
foo: '',
bar: '',
@granmoe
granmoe / gitconfig
Last active May 16, 2018 18:19
temp-vscode-settings
[alias]
s = status
d = diff
c = checkout
co = commit
ps = push
l = log
p = pull
b = branch
[user]
@granmoe
granmoe / form-validation-and-utils-with-cached-functions.jsx
Last active May 9, 2017 02:04
Simple form validation through a Higher Order Component. Same as the other ones I made, except this caches functions so shouldComponentUpdate of the child doesn't always return true.
import React from 'react'
export default options => {
return WrappedComponent => class FormValidation extends React.Component {
constructor () {
super()
this.validate = options.validate
this.cachedFunctions = { onChange: {}, onBlur: {} }
this.state = options.fields.reduce((result, field) => {
@granmoe
granmoe / make-reducer.js
Last active May 9, 2017 01:50
A simple pattern for having a reducer as a map of functions instead of a switch statement. One benefit is being able to grab any of the reducer's functions independently for testing.
export default function (reducerMap = {}, initialState) =>
(state = initialState, { type = '', ...payload }) =>
(reducerMap[type] || (() => state))(state, payload) // if type isn't found in the reducer, just use a noop