Skip to content

Instantly share code, notes, and snippets.

View richcsmith's full-sized avatar

Rich Smith richcsmith

View GitHub Profile
@richcsmith
richcsmith / useMediaQuery.ts
Last active June 14, 2022 17:17
React/TypeScript Media Query Hook
import { useCallback, useEffect } from 'react';
export type MediaQuery = MediaQueryListEvent['media'];
export type MediaQueryCallback = (event: MediaQueryListEvent) => void;
export function useMediaQuery(mediaQuery: MediaQuery, callback: MediaQueryCallback) {
const handleMediaQuery = useCallback((event: MediaQueryListEvent) => {
if (event.media === mediaQuery) {
callback(event);
}
@richcsmith
richcsmith / featureFlags.js
Last active April 23, 2018 20:43
Quick & dirty feature flag implementation
const isLocal = process.env.ENV === 'dev';
const isTest = process.env.ENV === 'test';
const isProd = process.env.ENV === 'prod';
const featureFlags = {
NewCampaignEdit: () => !isProd,
};
const isFeatureEnabled = (name = '') =>
{}.hasOwnProperty(featureFlags, name) && featureFlags[name]();
@richcsmith
richcsmith / csv.js
Last active March 27, 2018 18:35
CSV Helper Functions
const generateId = () => Math.floor(Math.random() * Date.now())
/*
* This function accepts an array of objects as its only argument. It will use the keys
* from the first object (not recursively) to create the header row of the .csv file.
*
* Returns: a formatted string, which can be fed directly into `downloadFile()`
*/
const convertToCSV = (data = []) => {
let csvContent = 'data:text/csv;charset=utf-8,'
@richcsmith
richcsmith / encryption_hash.rb
Created March 28, 2015 20:01
encyprtion hash
{"a"=>"c", "b"=>"d", "c"=>"e", "d"=>"f", "e"=>"g",
"f"=>"h", "g"=>"i", "h"=>"j", "i"=>"k", "j"=>"l",
"k"=>"m", "l"=>"n", "m"=>"o", "n"=>"p", "o"=>"q",
"p"=>"r", "q"=>"s", "r"=>"t", "s"=>"u", "t"=>"v",
"u"=>"w", "v"=>"x", "w"=>"y", "x"=>"z", "y"=>"a", "z"=>"b"}
@richcsmith
richcsmith / AMCSS Example
Last active August 29, 2015 14:06
Example of Markup using AMCSS
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Example of Markup using AMCSS</title>
</head>
<body>
<!-- Example Template -->
<div namespace-Module-Component="region section/layout style" id="specific-name">
<p>content</p>
=begin
Instructions
============
1. Copy the code here: http://bit.ly/SLB5Ah
2. Fix it.
3. Make up your own city names using an array called 'cities'.
4. Print the length of the characters of each person's name.
5. Ask the user for their name and where they are from.
6. Your final output should look like the following:
@richcsmith
richcsmith / ex_teddit_api_news.rb
Created March 29, 2014 19:51
Teddit API w/Mashable, Reddit and Digg
# We're going to add a remote data source to pull in stories from Mashable and Digg.
# http://mashable.com/stories.json
# http://digg.com/api/news/popular.json
# These stories will also be upvoted based on our rules. No more user input!
# Pull the json, parse it and then make a new story hash out of each story(Title, Category, Upvotes)
# Add each story to an array and display your "Front page"
require 'rest-client'
require 'json'
# Step 1: Working with Arrays and Hashes
# Copy your solution from ex_teddit_conditional.rb in 02_Variables_Conditionals
# Create an empty stories array. This will be used to hold all stories we capture.
# Users enter the story as they do now, but it gets put in a hash instead.
# Update any reference to the story (upvotes, category and title)
@richcsmith
richcsmith / encryption.rb
Created March 1, 2014 22:13
encryption.rb
#create an encryption algorithm (steps to mask a word)
encryption = {"a"=>"c", "b"=>"d", "c"=>"e", "d"=>"f", "e"=>"g",
"f"=>"h", "g"=>"i", "h"=>"j", "i"=>"k", "j"=>"l",
"k"=>"m", "l"=>"n", "m"=>"o", "n"=>"p", "o"=>"q",
"p"=>"r", "q"=>"s", "r"=>"t", "s"=>"u", "t"=>"v",
"u"=>"w", "v"=>"x", "w"=>"y", "x"=>"z", "y"=>"a", "z"=>"b"}
decryption = encryption.invert
#ask the user for a word