Skip to content

Instantly share code, notes, and snippets.

View mfix22's full-sized avatar
🖥️
Working on the Dashboard & OSS @ Stripe

Mike Fix mfix22

🖥️
Working on the Dashboard & OSS @ Stripe
View GitHub Profile
@mfix22
mfix22 / Recursive_file_search_by_pattern.md
Last active March 15, 2017 23:51
Recursive file search by pattern

Recursive file searching can be helpful if you are building a tool like jest or gest. For example, jest searches for files that match /(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$/i, and gest looks for /.*.(query|graphql|gql)$/i. I wanted to test my functional programming abilities and try to tackle this recursive problem as functionally as possible.

Implementation below

Example Usage

const gest = Gest(schema, options)

findFilesByPattern(/.*.(query|graphql|gql)$/i)
 .then(values =>
@mfix22
mfix22 / Functional_Functions.md
Last active October 26, 2021 12:15
Fun functional programming examples, without error handling

A bunch of random, contrived functions with maybe some value. Mostly small proof-of-concepts.

Examples

destructure()

Operates just like const sanitize = ({ id, email, date }) => ({ id, email, date }) but with destructure you can store your included keys in an array, say, in your config.

const keysToInclude = ['id', 'email', 'date']
const sanitize = destructure(keysToInclude)
@mfix22
mfix22 / global.js
Last active March 28, 2017 20:39
Examples of testing with `gest`
// As a global function
// will create global `gest()` function
Gest(schema)
describe('GraphQL', () => {
// pass a test name and a query
gest('test query', `
{
test
@mfix22
mfix22 / catch-errors.js
Created September 25, 2017 17:07
Protect a function from all errors. Returns a Promise
const catchErrors = (fn, handleErrors) => {
try {
return Promise.resolve(fn()).catch(handleErrors)
} catch (e) {
handleErrors(e);
}
}
@mfix22
mfix22 / gist:06fbf0907a27af6e1166a3da07119b6d
Created October 9, 2017 18:53
Test out appending a Gist URL
___ ___ ___ ___ ___
/\__\ /\ \ /\ \ _____ /\ \ /\ \
/:/ / /::\ \ /::\ \ /::\ \ /::\ \ \:\ \
/:/ / /:/\:\ \ /:/\:\__\ /:/\:\ \ /:/\:\ \ \:\ \
/:/ / ___ /:/ /::\ \ /:/ /:/ / /:/ /::\__\ /:/ \:\ \ _____\:\ \
/:/__/ /\__\ /:/_/:/\:\__\ /:/_/:/__/___ /:/_/:/\:|__| /:/__/ \:\__\ /::::::::\__\
\:\ \ /:/ / \:\/:/ \/__/ \:\/:::::/ / \:\/:/ /:/ / \:\ \ /:/ / \:\~~\~~\/__/
\:\ /:/ / \::/__/ \::/~~/~~~~ \::/_/:/ / \:\ /:/ / \:\ \
\:\/:/ / \:\ \ \:\~~\ \:\/:/ / \:\/:/ / \:\ \
\::/ / \:\__\ \:\__\ \::/ / \::/ / \:\__\
@mfix22
mfix22 / index.js
Last active October 19, 2017 02:21
Y-Combinator in modern JavaScript
// Y-Combinator implemented in JavaScript
// Inspired (mostly copied) from http://kestas.kuliukas.com/YCombinatorExplained/
// Thank you!
const Y = g =>
(f => f(f))(f =>
g(x => f(f)(x)))
const factorial = given =>
n => n < 2
function range(s, e) {
let start
let end
if (e == null) {
end = s
start = 0
} else {
start = s
end = e
}
const noop = () => {}
function batch (reduce, options = {}) {
const timeout = options.timeout || 1000
const max = options.max || 10
let calls = []
let cancel = noop
let flush = noop
const reset = () => {
clearTimeout(cancel)
@mfix22
mfix22 / index.js
Last active April 2, 2019 20:30
Replace string component
export function replaceWithComponent(regex, mapping, s) {
const parts = s.split(regex);
for (let i = 1; i < parts.length; i += 2) {
parts[i] = mapping(parts[i]);
}
return <Fragment>{parts}</Fragment>
}
@mfix22
mfix22 / index.js
Last active February 2, 2019 07:16
Add Plotly Carbon Theme Snipper
localStorage.setItem(
'CARBON_THEMES',
JSON.stringify(
JSON.parse(localStorage.getItem('CARBON_THEMES') || '[]').concat({
id: 'theme:plotly',
name: 'Plotly',
highlights: {
background: '#2a3f5f',
text: 'rgba(223,228,229,1)',
variable: '#119DFF',