Skip to content

Instantly share code, notes, and snippets.

View joemaffei's full-sized avatar

Joe Maffei joemaffei

View GitHub Profile
@joemaffei
joemaffei / formatDollarAmount.js
Created September 6, 2018 17:56
Converts a number into dollars the "hard" way, because toLocaleString is unsupported by react-native on Android
const formatDollarAmount = (num) => (
'$' + `${Math.floor(num)}`
.split('')
.reverse()
.reduce((result, char, index) => {
index > 0 && !(index % 3) && result.push(',');
return [...result, char]
}, [])
.reverse()
.join('') + `${(num % 1).toFixed(2)}`.substr(1)
@joemaffei
joemaffei / slack-custom.css
Created October 8, 2018 17:19
Custom Slack client styling
/* Move footer to the top so it doesn't get obscured behind Windows 10's notifications */
#client_body:not(.onboarding):not(.feature_global_nav_layout)::before {
display: none;
}
#col_messages {
flex-direction: column-reverse !important;
}
@joemaffei
joemaffei / omit.js
Last active January 24, 2019 22:26
Naive implementation of lodash.omit
export function omitSingle(object = {}, key) {
if (key === null || key === undefined || !(key in object)) return object;
const { [key]: deleted, ...otherKeys } = object;
return otherKeys;
}
export function omitMultiple(object = {}, keys) {
if (!Array.isArray(keys)) return object;
return keys.reduce((result, key) => {
if (key in result) {
@joemaffei
joemaffei / fibonacci.js
Created February 19, 2019 05:27
Calculate nth Fibonacci number
function fibonacci(n) {
return Math.round((Math.pow(1.618033988749895, n) - Math.pow(-0.618033988749895, n)) / 2.23606797749979);
}
@joemaffei
joemaffei / useActiveValidation.jsx
Last active May 22, 2019 16:41
React hook for active validation (e.g. on change of a controlled input)
import React, { useEffect, useState } from 'react';
function useActiveValidation(value, validator) {
const [isValid, setIsValid] = useState(true);
useEffect(() => {
setIsValid(validator(value));
}, [value]);
return isValid;
@joemaffei
joemaffei / usePassiveValidation.jsx
Last active May 22, 2019 16:43
React hook for passive validation (e.g. on form submit)
import React, { useEffect, useState } from 'react';
function usePassiveValidation(value, validator) {
const [valid, setValid] = useState(true);
const [checkValue, setCheckValue] = useState(null);
const [didMount, setDidMount] = useState(false);
function validate() {
setCheckValue(value);
@joemaffei
joemaffei / repo-into-folder.txt
Created August 29, 2019 19:33
Merge repository as folder of local repository
cd {localRepo}
git subtree add --prefix {localFolder} {otherRepoUrl} {otherRepoBranch}
@joemaffei
joemaffei / refactoring-class-method.js
Last active September 3, 2019 22:23
Refactoring React: class method
// Original
tagClick = (tag) => {
let newSpecialties = this.state.specialties.slice();
let specialties = (filter(newSpecialties, (option) => {
return !isEqual(option, tag);
}));
this.setState({ specialties });
}
// 1. Variables are not being reassigned, so they should be consts
@joemaffei
joemaffei / refactoring-reducer-helper.js
Last active September 20, 2019 16:12
Refactoring React: reducer helper function
// Original
function clearTempStatuses(state) {
if (state == null) return [];
return state.map((quote) => {
const options = quote.options.map((option) => {
const { tempStatus: optionTempStatus, ...optionWithoutTempStatus } = option;
if (optionTempStatus) {
return { ...optionWithoutTempStatus, status: optionTempStatus };
}
return optionWithoutTempStatus;
@joemaffei
joemaffei / machine.js
Last active November 16, 2019 18:35
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)