This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const partialApply = (fn, ...fixedArgs) => { | |
return (...remainingArgs) => fn(...fixedArgs.concat(...remainingArgs)); | |
}; | |
const add = (a, b) => a + b; | |
const add10 = partialApply(add, 10); | |
console.log(add10(5)) // 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { ToastContext } from './Provider'; | |
export const ToastConsumer = ({ children }) => ( | |
<ToastContext.Consumer> | |
{context => children(context)} | |
</ToastContext.Consumer> | |
); | |
// Higher-Order Component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { ToastContext } from './Provider'; | |
export const ToastConsumer = ({ children }) => ( | |
<ToastContext.Consumer> | |
{context => children(context)} | |
</ToastContext.Consumer> | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Renderer from './Renderer'; | |
export const ToastContext = React.createContext(); | |
export class ToastProvider extends React.Component { | |
state = { toasts: [] } | |
add = (content) => { | |
this.setState(...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Store { | |
listeners = [] | |
store = [] | |
add = (data) => { | |
const id = uniqueId() | |
const item = { id, data } | |
this.store.push(item) | |
this.publish() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// probably unnecessary but... | |
const merge = (...args) => args.reduce((obj, val) => ({ ...obj, ...val }), {}); | |
const combined = merge( | |
{ background: 'red', padding: 20 }, | |
{ color: 'blue' }, | |
{ color: 'white' }, | |
); | |
console.log(combined); // { background: 'red', padding: 20, color: 'white' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function not(predicate) { | |
return function negate(...args) { | |
return !predicate(...args); | |
} | |
} | |
// Usage | |
// ============================== | |
function longEnough(str) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function shuffles an array of items. | |
* @param arr {Array} array of items | |
* @param clone {Boolean} immutable or not | |
* @returns {Array} the array of items in random order. | |
*/ | |
function shuffle(arr, clone) { | |
var array = clone ? arr.slice(0) : arr; | |
var m = array.length, t, i; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CATEGORIES = [ | |
{ id: 'animals', parent: null }, | |
{ id: 'mammals', parent: 'animals' }, | |
{ id: 'cats', parent: 'mammals' }, | |
{ id: 'dogs', parent: 'mammals' }, | |
{ id: 'labrador', parent: 'dogs' }, | |
{ id: 'cocker spaniel', parent: 'dogs' }, | |
{ id: 'rag doll', parent: 'cats' }, | |
{ id: 'burmese', parent: 'cats' }, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "babel-polyfill"; | |
const asyncQuerySelector = async (node, query) => { | |
try { | |
return await (query ? node.querySelector(query) : node); | |
} catch (error) { | |
console.error(`Cannot find ${query ? `${query} in`: ''} ${node}.`, error); | |
return null; | |
} | |
}; |
NewerOlder