Skip to content

Instantly share code, notes, and snippets.

View christianscott's full-sized avatar

Christian Scott christianscott

View GitHub Profile
@christianscott
christianscott / template.js
Created February 12, 2018 02:01
JS Function calls to html
function createElement(tag, props, ...children) {
return (
`<${tag}${renderProps(props)}>${children.join ? children.join('') : children}</${tag}>`
)
}
function renderProps(props) {
const propsString = Object.keys(props).map(key => `${key}="${props[key]}"`).join(' ')
return propsString ? ' ' + propsString : ''
}
@christianscott
christianscott / answer.jsx
Last active February 15, 2018 09:46
Answer for stackoverflow question
import React from 'react'
import { renderToString } from 'react-dom/server'
import Menu from './Menu'
const SELECTOR = '#test-component' // or whatever
class Component extends React.Component {
state = { html: { __html: "" } }
componentDidMount() {
@christianscott
christianscott / flip.js
Created March 8, 2018 01:00
Flip the order of arguments for a function in Javascript
function flip(fn) {
return function () {
return fn.apply(null, Array.prototype.reverse.call(arguments))
}
}
@christianscott
christianscott / curry.js
Created March 8, 2018 01:02
Curry a function in Javascript to allow partial application
function curry(fn, ...initialArgs) {
return function (...nextArgs) {
const args = initialArgs.concat(nextArgs)
if (args.length >= fn.length) {
return fn(...args)
}
return curry(fn, ...args)
}
}
import React from 'react';
import { render } from 'react-dom';
class Child extends React.Component {
componentDidMount() {
if (this.props.n === 0) {
console.log(`child mounted at ${Date.now()}`)
}
}
@christianscott
christianscott / stats.js
Last active April 30, 2018 00:37
Covariance and Correlation in Javascript
function sum(xs) {
return xs.reduce((total, x) => total + x, 0)
}
function mean(xs) {
return sum(xs) / xs.length
}
function zip(xs, ys) {
return range(xs.length)
@christianscott
christianscott / pipes.js
Created March 24, 2018 08:47
HOF to add `pipe` method to any function
const assert = require("assert")
function pipes(func) {
return function () {
const value = func.apply(null, arguments)
return {
value,
pipe(func2) {
return func2(value)
}
@christianscott
christianscott / index.js
Last active July 20, 2021 11:09
Preventing event bubbling in JS
var el = document.createElement("div");
document.body.innerHTML = "";
document.body.appendChild(el);
document.body.addEventListener("foobar", announce);
el.addEventListener("foobar", preventBubble(announce));
var event = new CustomEvent("foobar", { bubbles: true, detail: "Hello" });
el.dispatchEvent(event); // "ANNOUNCING: Hello"
@christianscott
christianscott / QuoteWrappedInput.js
Created April 12, 2018 00:13
Component that wraps the value of an input in quotes
import React from "react";
import { wrapInQuotemarks, stripQuotationMarks, noop } from "./utils";
let previousQuote = null;
export default class QuotationWrappedInput extends React.Component {
static defaultProps = {
InputComponent: ({ handleRef, ...props }) => <textarea ref={handleRef} {...props} />,
handleRef: noop,
maxLength: 320,
placeholder: "Type here",
@christianscott
christianscott / hof.js
Created April 30, 2018 00:30
Higher order functions for iterating over arrays
function callWith() {
const args = arguments
return function (fn) {
return fn.apply(null, args)
}
}
const add = d => x => x + d
const updaters = [add(1), add(2), add(3)]
console.log(updaters.map(callWith(10))) // [11, 12, 13]