Skip to content

Instantly share code, notes, and snippets.

View eddyw's full-sized avatar
🏠
Working from home

Eddy Wilson eddyw

🏠
Working from home
View GitHub Profile
@eddyw
eddyw / machine.js
Last active April 21, 2021 17:25
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@eddyw
eddyw / index.html
Created July 6, 2018 14:10 — forked from jjgonecrypto/index.html
es6 proxy #jsbench #jsperf (https://jsbench.github.io/#531652a2edfa806a5014558bafe6eb0e) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>es6 proxy #jsbench #jsperf</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@eddyw
eddyw / index.js
Created February 5, 2018 06:55
Type-checking Redux State with propTypes (without React)
import TodoReducer from 'TodoReducer'
import { createStore } from 'redux'
const store = createStore(TodoReducer)
store.dispatch({
type: 'ADD_TODO',
payload: {
title: 'Example',
done: 1,
@eddyw
eddyw / TodoReducer.js
Last active February 5, 2018 07:13
Type-checking Redux State with propTypes (without React)
import withPropTypes from 'withPropTypes.js'
const ItemSchema = {
id: propTypes.number,
done: propTypes.bool.isRequired,
title: propTypes.string.isRequired,
}
const TodoReducerSchema = propTypes.arrayOf(
propTypes.shape(ItemSchema).isRequired
@eddyw
eddyw / withPropTypes.js
Created February 5, 2018 06:43
Type-checking Redux State with propTypes (without React)
const withPropTypes = (name, propTypesSchema) => reducer => {
if (process.env.NODE_ENV === 'development') {
return (state, action) => {
const result = reducer(state, action)
propTypes.checkPropTypes(
{ state: propTypesSchema },
{ state: result },
'property',
name,
@eddyw
eddyw / TodoReducer.js
Last active February 5, 2018 06:31
Type-checking Redux State with propTypes (without React)
let id = 0
const TodoReducer = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [...state, {
id: ++id,
title: action.payload.title,
done: action.payload.done,
}]
@eddyw
eddyw / example.js
Last active February 5, 2018 06:32
Type-checking Redux State with propTypes (without React)
import propTypes from 'prop-types'
const pTypes = {
name: propTypes.string.isRequired,
age: propTypes.number.isRequired,
}
const obj = {
name: 16,
age: 'John',
}
@eddyw
eddyw / map-component.js
Last active November 11, 2017 19:05
Map Component
const Item = ({ title, completed, check, style, className }) => (
<li className={className}>
<span>{check} ({String(completed)}) </span>
<span style={style}>{title}</span>
</li>
)
const map = item => ({
...item,
check: item.completed? 'o' : 'x',
style: item.completed? { color: 'gree' } : { color: 'red' },
@eddyw
eddyw / map-component.js
Created November 11, 2017 17:16
Map Component
const completedStyle = { color: 'green' }
const uncompletedStyle = { color: 'red' }
const Item = ({ title, completed }) => (
<li>
<span>{completed ? 'o' : 'x'} </span>
<span style={completed ? completedStyle : uncompletedStyle}>{title}</span>
</li>
)
const TodoList = () => (
<ul>
@eddyw
eddyw / map-component.js
Last active November 11, 2017 19:08
Map Component
class MapArray extends React.Component {
static propTypes = {
from: propTypes.array.isRequired,
children: propTypes.element.isRequired,
map: propTypes.func,
}
static defaultProps = {
map: e => e,
}
shouldComponentUpdate(nextProps) {