This file contains hidden or 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
| /** | |
| * Returns the count and appropriate plural or singular form of a term. | |
| * | |
| * @example | |
| * pluralize(0, 'wallet'); // '0 wallets' | |
| * pluralize(1, 'wallet'); // '1 wallet' | |
| * pluralize(2, 'wallet'); // '2 wallets' | |
| * pluralize(1, ['person', 'people']); // '1 person' | |
| * pluralize(2, ['person', 'people']); // '2 people' | |
| * pluralize(1, ['person', 'people'], false); // 'person' |
This file contains hidden or 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 type { CSSProperties } from 'react'; | |
| type StyleVars = Record< | |
| `--${string}`, | |
| string | number | boolean | undefined | null | |
| >; | |
| /** | |
| * Type casting identity function. Maintains the autocompletion and type safety | |
| * of `CSSProperties`, while allowing the addition of CSS variables. |
This file contains hidden or 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 DRAW_DIRECTION = ["clockwise", "anti-clockwise"] as const; | |
| /** | |
| * Generate a complex path string, using familiar SVG shape (`<circle>`, | |
| * `<rect>`, etc.) syntax. | |
| * | |
| * @param data An array of shape objects. | |
| * @param evenodd Alternate drawing direction for each path. When multiple paths are combined into a single string of path data, this will influence whether the paths are additive or subtractive. | |
| * @returns A string of SVG path data representing all shapes. | |
| * |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) { |
NewerOlder