Skip to content

Instantly share code, notes, and snippets.

View asolove's full-sized avatar
🤔
Thinking about the essence of UI programming

Adam Solove asolove

🤔
Thinking about the essence of UI programming
View GitHub Profile
@asolove
asolove / better.js
Created August 27, 2017 20:38
Exhaustiveness checking in flow
const checkType = (element: 'a' | 'b' | 'c'): string => {
switch (element) {
case 'a':
return 'first';
case 'b':
return 'second';
default:
(element: empty) // This cast only succeeds if you've already handled every possible case. Otherwise, you will get a type error
throw new Error()
}
@asolove
asolove / scan-barcode.js
Created August 14, 2017 20:32
Scan Barcodes for Project Cure
var searchInput = document.getElementById("SearchBox");
var scanInput = document.createElement("input");
scanInput.type = "file";
scanInput.accept = "image/*";
scanInput.addEventListener("change", onFileChange);
searchInput.parentElement.insertBefore(scanInput, searchInput);
function onFileChange() {
readImageData(scanInput.files[0], function(error, imageData) {
@asolove
asolove / 0-innerDiffNode.js
Created March 26, 2017 19:21
preact-innerDiffNode
/** Apply child and attribute changes between a VNode and a DOM Node to the DOM.
* @param {Element} dom Element whose children should be compared & mutated
* @param {Array} vchildren Array of VNodes to compare to `dom.childNodes`
* @param {Object} context Implicitly descendant context object (from most recent `getChildContext()`)
* @param {Boolean} mountAll
* @param {Boolean} absorb If `true`, consumes externally created elements similar to hydration
*/
function innerDiffNode(dom, vchildren, context, mountAll, absorb) {
let originalChildren = dom.childNodes,
children = [],
export function getNodeProps(vnode) {
let props = clone(vnode.attributes);
props.children = vnode.children;
let defaultProps = vnode.nodeName.defaultProps;
if (defaultProps) {
for (let i in defaultProps) {
if (props[i]===undefined) {
props[i] = defaultProps[i];
}
@asolove
asolove / 1-h-overview.js
Last active March 11, 2017 02:52
preact/src/h.js
import { VNode } from './vnode';
import options from './options';
// a single empty array shared among all child-less nodes to prevent having to allocate one array each
const EMPTY_CHILDREN = [];
export function h(nodeName, attributes) {
let children = // deleted code to calculate children
let p = new VNode(nodeName, attributes || undefined, children || EMPTY_CHILDREN);
@asolove
asolove / 1-before.js
Created March 11, 2017 02:26
JSX compilation before/after
/** @jsx h **/
import { render, h } from 'preact';
render(<p size="5"><span>foo</span><i>bar</i></p>, document.body);
@asolove
asolove / proposal.md
Last active August 18, 2016 13:59
↪️ Proposal: U+bb8dd "HORIZONTAL FLIP COMBINING CHARACTER

Purpose

The new combining character "HORIZONTAL FLIP COMBINING CHARACTER" combines with any emoji and flips it left-to-right.

Motivation

Many emoji have a standard directionality, but depending on context this may or may not be the direction that best conveys the author's intent.

Consider the common formula:

@asolove
asolove / problem.js
Last active July 29, 2016 16:47
Trouble exporting generic curried functions
// I have a library of helper functions that I want to export curried versions of
// A minimal chunk of it looks like this:
// stole this type signature from a PR discussion: thanks!
export function curry2<A,B,C>(f: (x: A, y: B) => C): (x: A) => (y: B) => C {
return (a) => (b) => f(a, b)
}
function _push<A>(item: A, items: Array<A>): Array<A> {
@asolove
asolove / after.js
Created July 28, 2016 16:07
Refactor to component
import {Component} from 'react'
export default class Thingy extends Component {
render() {
const {a, b} = this.props;
return <div>{a} <span>{b}</span></div>
}
}
@asolove
asolove / steps.sh
Created April 21, 2016 18:17
Web pack bundle size
npm install -g webpack-bundle-size-analyzer
edit gulp/webpack.config.production.js # remove all the lines but the app bundle you're interested in
webpack --config gulp/webpack.config.production.js --json | webpack-bundle-size-analyzer > analysis
# This generates a large text file of all nested dependencies, by file size.
# I found several large dependencies in cart that I was no longer using, totalling 300kb.