Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Michael Fix mfix22

View GitHub Profile
@mfix22
mfix22 / index.js
Last active Feb 2, 2019
Add Plotly Carbon Theme Snipper
View index.js
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',
@mfix22
mfix22 / index.js
Last active Apr 2, 2019
Replace string component
View index.js
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>
}
View batch.js
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)
View range.js
function range(s, e) {
let start
let end
if (e == null) {
end = s
start = 0
} else {
start = s
end = e
}
@mfix22
mfix22 / index.js
Last active Oct 19, 2017
Y-Combinator in modern JavaScript
View index.js
// 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
View gist:06fbf0907a27af6e1166a3da07119b6d
___ ___ ___ ___ ___
/\__\ /\ \ /\ \ _____ /\ \ /\ \
/:/ / /::\ \ /::\ \ /::\ \ /::\ \ \:\ \
/:/ / /:/\:\ \ /:/\:\__\ /:/\:\ \ /:/\:\ \ \:\ \
/:/ / ___ /:/ /::\ \ /:/ /:/ / /:/ /::\__\ /:/ \:\ \ _____\:\ \
/:/__/ /\__\ /:/_/:/\:\__\ /:/_/:/__/___ /:/_/:/\:|__| /:/__/ \:\__\ /::::::::\__\
\:\ \ /:/ / \:\/:/ \/__/ \:\/:::::/ / \:\/:/ /:/ / \:\ \ /:/ / \:\~~\~~\/__/
\:\ /:/ / \::/__/ \::/~~/~~~~ \::/_/:/ / \:\ /:/ / \:\ \
\:\/:/ / \:\ \ \:\~~\ \:\/:/ / \:\/:/ / \:\ \
\::/ / \:\__\ \:\__\ \::/ / \::/ / \:\__\
@mfix22
mfix22 / catch-errors.js
Created Sep 25, 2017
Protect a function from all errors. Returns a Promise
View catch-errors.js
const catchErrors = (fn, handleErrors) => {
try {
return Promise.resolve(fn()).catch(handleErrors)
} catch (e) {
handleErrors(e);
}
}
@mfix22
mfix22 / global.js
Last active Mar 28, 2017
Examples of testing with `gest`
View global.js
// 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 / Functional_Functions.md
Last active Oct 26, 2018
Fun functional programming examples, without error handling
View Functional_Functions.md

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 / Recursive_file_search_by_pattern.md
Last active Mar 15, 2017
Recursive file search by pattern
View Recursive_file_search_by_pattern.md

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 =>
You can’t perform that action at this time.