Skip to content

Instantly share code, notes, and snippets.

Avatar

Jared Palmer jaredpalmer

View GitHub Profile
@jaredpalmer
jaredpalmer / replaceValues.ts
Last active Aug 7, 2020
Replace all values of a given key
View replaceValues.ts
/**
* Return a copy of an object, but with values of a key replaced with value of provided key-value object.
* Useful for duplicating highly nested object, but generating new values for a key (e.g. `id`)
*
*
* @param obj An object
* @param keyToReplace The key of the values needed to be swapped
* @param mapOfOldToNewValues A key-value object with a keys of old values mapped to new ones (used for swapping)
*
* @example
View Formik-Autosave.jsx
import React from 'react';
import PropTypes from 'prop-types'
import debounce from 'lodash.debounce' // or whatevs
import isEqual from 'lodash.isEqual'
class AutoSave extends React.Component {
static contextTypes = {
formik: PropTypes.object
}
View tweet.js
/*
* Code snippet for posting tweets to your own twitter account from node.js.
* You must first create an app through twitter, grab the apps key/secret,
* and generate your access token/secret (should be same page that you get the
* app key/secret).
* Uses oauth package found below:
* https://github.com/ciaranj/node-oauth
* npm install oauth
* For additional usage beyond status updates, refer to twitter api
* https://dev.twitter.com/docs/api/1.1
View SuspenseRedirect.tsx
import { globalHistory, navigate, RedirectProps, History } from '@reach/router';
import React from 'react';
type ReachHistory = History & {
_onTransitionComplete: () => void;
};
/**
* Why is `SuspenseRedirect` necessary when @reach/router has `Redirect`?
*
@jaredpalmer
jaredpalmer / SearchUtil.java
Created Jul 21, 2020
Search String parser
View SearchUtil.java
package org.formium.util;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SearchUtil {
public static Map<String,String> parseSearchQuery(String query) {
@jaredpalmer
jaredpalmer / ValidateUtil.java
Created Jul 21, 2020
US Phone Validation w/ext
View ValidateUtil.java
package org.formium.util;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import java.util.Optional;
public class ValidateUtil {
public static Optional<String> validatePhone(String phone) {
@jaredpalmer
jaredpalmer / feed.xml
Created Nov 30, 2014
feed.xml for Jekyll
View feed.xml
---
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ site.title | xml_escape }}</title>
<description>{{ site.description | xml_escape }}</description>
<link>{{ site.url }}{{ site.baseurl }}/</link>
<atom:link href="{{ "/feed.xml" | prepend: site.baseurl | prepend: site.url }}" rel="self" type="application/rss+xml"/>
@jaredpalmer
jaredpalmer / Video.js
Created Jul 8, 2020
Better <video> with intersection observer.
View Video.js
import { useRef, useCallback, useEffect } from 'react'
import { useInView } from 'react-intersection-observer'
import 'intersection-observer'
export default ({ src, caption, ratio }) => {
const [inViewRef, inView] = useInView({
threshold: 1,
})
const videoRef = useRef()
View forwardRefWithAs.tsx
import * as React from 'react';
/**
* React.Ref uses the readonly type `React.RefObject` instead of
* `React.MutableRefObject`, We pretty much always assume ref objects are
* mutable (at least when we create them), so this type is a workaround so some
* of the weird mechanics of using refs with TS.
*/
export type AssignableRef<ValueType> =
| {
@jaredpalmer
jaredpalmer / App.js
Created May 30, 2017
Next.js-like SSR without Next.js.
View App.js
import React from 'react';
import Route from 'react-router-dom/Route';
import Link from 'react-router-dom/Link';
import Switch from 'react-router-dom/Switch';
const App = ({ routes, initialData }) => {
return routes
? <div>
<Switch>
{routes.map((route, index) => {
You can’t perform that action at this time.