Skip to content

Instantly share code, notes, and snippets.

@asolove
asolove / better.js
Created Aug 27, 2017
Exhaustiveness checking in flow
View better.js
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 Aug 14, 2017
Scan Barcodes for Project Cure
View scan-barcode.js
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 Mar 26, 2017
preact-innerDiffNode
View 0-innerDiffNode.js
/** 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 = [],
View preact-getNodeProps.js
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 Mar 11, 2017
preact/src/h.js
View 1-h-overview.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 Mar 11, 2017
JSX compilation before/after
View 1-before.js
/** @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 Aug 18, 2016
↪️ Proposal: U+bb8dd "HORIZONTAL FLIP COMBINING CHARACTER
View proposal.md

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 Jul 29, 2016
Trouble exporting generic curried functions
View problem.js
// 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 Jul 28, 2016
Refactor to component
View after.js
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 Apr 21, 2016
Web pack bundle size
View steps.sh
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.