Skip to content

Instantly share code, notes, and snippets.

@frankfaustino
frankfaustino / reducedfruits.js
Created June 4, 2018 02:26
reducing an object into an object, array, or int
const fruits = {
apple: 'better',
banana: 'good',
peach: 'ok',
pear: 'good'
}
Object
.entries(fruits)
.reduce((goodFruit, [fruit, quality]) => {
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
const getCoffee = () => new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
});
async go = () => {
try {
@frankfaustino
frankfaustino / aync-await–error–handler.js
Last active May 5, 2018 04:24
Error handling async await in an Express server 🤖
const express = require('express')
const db = require('./data/helpers/tagDb')
const server = express()
server.use(express.json())
// Catches async/await errors/Promise rejections and passes it along to express middleware
const catchErrors = fn => (req, res, next) => fn(req, res, next).catch(next)
// Error Handler (displays full error details)
const handleErrors = (err, req, res, next) => {
@frankfaustino
frankfaustino / redux.js
Created March 21, 2018 19:41
Redux explained as a reduce function
const state = []
// Actions => tells the reducer which action to perform and provides the values
const action = [
{ type: 'ADD_TODO', text: 'do laundry', completed: false, id: 0 },
{ type: 'ADD_TODO', text: 'walk the kids', completed: false, id: 1 },
{ type: 'ADD_TODO', text: 'take the dogs to school', completed: false, id: 2 },
{ type: 'ADD_TODO', text: 'kiss the plant', completed: false, id: 3 },
{ type: 'ADD_TODO', text: 'water the wifey', completed: false, id: 4 },
{ type: 'REMOVE_TODO', id: 0 },
@frankfaustino
frankfaustino / PropsRestSpread.jsx
Created March 15, 2018 18:17
React: rest/spread operator and props
const MyComponent = ({ className, ...others }) => (
<div className={className}>
<MyOtherComponent {...others} />
</div>
)
@frankfaustino
frankfaustino / Functional.jsx
Last active March 15, 2018 18:17
React: Stateless functional components
// Good
const TableRowWrapper = ({ children }) => (
<tr>
{children}
</tr>
)
// Bad
class TableRowWrapper extends Component {
render() {
@frankfaustino
frankfaustino / Container.jsx
Created March 15, 2018 18:13
React: Separate stateful aspects from rendering
// Container Pattern: https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0
import Loading from './Loading'
import RenderUser from './RenderUser'
class User extends Component {
state = { loading: true }
render() {
const { loading, user } = this.state
return loading ? <Loading /> : <RenderUser user={user} />
@frankfaustino
frankfaustino / ContactForm.jsx
Last active March 28, 2018 16:19
React Router V4 Redirect after form submission
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
class ContactForm extends Component {
submitForm = (e) => {
e.preventDefault()
this.props.history.push('/thank-you')
}
render() {
@frankfaustino
frankfaustino / destructuring.js
Created January 27, 2018 21:21 — forked from mikaelbr/destructuring.js
Several demos and usages for ES6 destructuring. Runnable demos and slides about the same topic: http://git.mikaelb.net/presentations/bartjs/destructuring
// === Arrays
var [a, b] = [1, 2];
console.log(a, b);
//=> 1 2
// Use from functions, only select from pattern
var foo = () => [1, 2, 3];
@frankfaustino
frankfaustino / composing.software.md
Last active March 3, 2023 06:20
Eric Elliot: Composing Software Series

Eric Elliott: Composing Software