Skip to content

Instantly share code, notes, and snippets.

View joshwcomeau's full-sized avatar

Joshua Comeau joshwcomeau

View GitHub Profile
@joshwcomeau
joshwcomeau / enter-exit-animation.js
Last active May 8, 2016 14:17
letter-animation
class LetterDemo extends Component {
constructor(props) { ... }
componentWillMount() { ... }
componentWillUnmount() { ... }
renderLetters() { ... }
render() {
import React, { Component } from 'react';
import random from 'lodash/random';
import sampleSize from 'lodash/sampleSize';
import FlipMove from 'react-flip-move';
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
function getSubsetOfAlphabet() { ... }
class LetterDemo extends Component {
import React, { Component } from 'react';
import random from 'lodash/random';
import sampleSize from 'lodash/sampleSize';
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
function getSubsetOfAlphabet() {
const numToPick = random(1, 26);
return sampleSize(alphabet, numToPick).sort();
}
@joshwcomeau
joshwcomeau / cta-async-await.js
Last active May 12, 2016 13:24
convert-to-async
app.post('/process-upload', upload.single('image'), async (req, res) => {
// These helper methods aren't as necessary anymore, but they still
// provide semantic value that makes the route easier to follow.
const resizeAndConvert = buffer => (
imConvertPromise({
srcData: buffer,
width: 32,
height: 16,
format: 'PNG'
})
@joshwcomeau
joshwcomeau / cta-wrap-imagemagick.js
Last active May 1, 2016 14:58
convert-to-async
export const imConvertPromise = wrapWithPromise.bind(null, imageMagick.convert);
export const wrapWithPromise = wrappedFunction => (...args) => (
new Promise((resolve, reject) => {
wrappedFunction(...args, (err, result) => {
return err ? reject(err) : resolve(result);
});
})
);
export const readFilePromise = wrapWithPromise(fs.readFile);
@joshwcomeau
joshwcomeau / cta-fs-promises.js
Last active May 1, 2016 14:37
convert-to-async
export function readFilePromise(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, buffer) => {
return err ? reject(err) : resolve(buffer);
});
});
}
export function writeFilePromise(filePath, data) {
return new Promise((resolve, reject) => {
@joshwcomeau
joshwcomeau / cta-callbacks.js
Last active May 1, 2016 16:30
convert-to-async
app.post('/process-upload', upload.single('image'), (req, res) => {
// Start by reading the uploaded file
fs.readFile(req.file.path, (err, originalFileBuffer) => {
if (err) throw err;
// Resize the image to 32x16, and convert to .png for consistency
imageMagick.convert({
srcData: originalFileBuffer,
width: 32,
height: 16,
@joshwcomeau
joshwcomeau / immutable-props-application.jsx
Created March 11, 2016 13:03
Immutable Props decorator - application
@immutableProps(['prop1', 'prop2'])
class Thing extends Component {
// Stuff
}
@joshwcomeau
joshwcomeau / immutable-props.jsx
Last active March 11, 2016 13:02
Immutable props decorator - function
const immutableProps = propsToCheck => ComposedComponent => {
return class immutablePropChecker extends Component {
shouldComponentUpdate(nextProps) {
return propsToCheck.some( p => this.props[p] !== nextProps[p] );
}
render() {
return <ComposedComponent {...this.props} />;
}
}