Created
March 22, 2016 18:30
-
-
Save anonymous/9b361a12e94f48cb9f5e to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xoboga
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://fb.me/react-0.14.7.min.js"></script> | |
<script src="https://fb.me/react-dom-0.14.7.min.js"></script> | |
<script src="https://wzrd.in/standalone/expect@latest"></script> | |
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.min.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id="root"></div> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var todo = function todo(state, action) { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return _extends({}, state, { | |
completed: !state.completed | |
}); | |
default: | |
return state; | |
} | |
}; | |
var visibilityFilter = function visibilityFilter(state, action) { | |
if (state === undefined) state = 'SHOW_ALL'; | |
switch (action.type) { | |
case 'SET_VISIBILITY_FILTER': | |
return action.filter; | |
default: | |
return state; | |
} | |
}; | |
var todos = function todos(state, action) { | |
if (state === undefined) state = []; | |
var ret = undefined; | |
switch (action.type) { | |
case 'ADD_TODO': | |
ret = [].concat(_toConsumableArray(state), [todo(undefined, action)]); | |
return ret; | |
case 'TOGGLE_TODO': | |
ret = state.map(function (t) { | |
return todo(t, action); | |
}); | |
return ret; | |
default: | |
return state; | |
} | |
}; | |
var combineReducers = function combineReducers(reducers) { | |
return function (state, action) { | |
if (state === undefined) state = {}; | |
return Object.keys(reducers).reduce(function (nextState, key) { | |
nextState[key] = reducers[key](state[key], action); | |
return nextState; | |
}, {}); | |
}; | |
}; | |
var todoApp = combineReducers({ | |
todos: todos, | |
visibilityFilter: visibilityFilter | |
}); | |
var _Redux = Redux; | |
var createStore = _Redux.createStore; | |
var store = createStore(todoApp); | |
console.log('Initial state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO...'); | |
console.log(store.dispatch({ | |
type: 'ADD_TODO', | |
id: 0, | |
text: 'Learn Redux' | |
})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO...'); | |
console.log(store.dispatch({ | |
type: 'ADD_TODO', | |
id: 1, | |
text: 'Go shopping' | |
})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching TOGGLE_TODO...'); | |
console.log(store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: 0 })); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching SET_VISIBILITY_FILTER...'); | |
console.log(store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: 'SHOW_COMPLETED' })); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const todo = (state, action) => { | |
switch(action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return { | |
...state, | |
completed: !state.completed | |
}; | |
default: | |
return state; | |
} | |
}; | |
const visibilityFilter = (state = 'SHOW_ALL', action) => { | |
switch(action.type) { | |
case 'SET_VISIBILITY_FILTER': | |
return action.filter; | |
default: | |
return state; | |
} | |
}; | |
const todos = (state = [], action) => { | |
let ret; | |
switch(action.type) { | |
case 'ADD_TODO': | |
ret = [ | |
...state, | |
todo(undefined, action) | |
]; | |
return ret; | |
case 'TOGGLE_TODO': | |
ret = state.map(t => todo(t, action)); | |
return ret; | |
default: | |
return state; | |
} | |
}; | |
const combineReducers = (reducers) => { | |
return (state = {}, action) => { | |
return Object.keys(reducers).reduce( | |
(nextState, key) => { | |
nextState[key] = reducers[key]( | |
state[key], | |
action | |
); | |
return nextState | |
}, | |
{} | |
); | |
}; | |
}; | |
const todoApp = combineReducers({ | |
todos: todos, | |
visibilityFilter: visibilityFilter | |
}); | |
const { createStore } = Redux; | |
const store = createStore(todoApp); | |
console.log('Initial state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO...'); | |
console.log(store.dispatch({ | |
type: 'ADD_TODO', | |
id: 0, | |
text: 'Learn Redux' | |
})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO...'); | |
console.log(store.dispatch({ | |
type: 'ADD_TODO', | |
id: 1, | |
text: 'Go shopping' | |
})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching TOGGLE_TODO...'); | |
console.log(store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: 0})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching SET_VISIBILITY_FILTER...'); | |
console.log(store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: 'SHOW_COMPLETED'})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var todo = function todo(state, action) { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
}; | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { | |
return state; | |
} | |
return _extends({}, state, { | |
completed: !state.completed | |
}); | |
default: | |
return state; | |
} | |
}; | |
var visibilityFilter = function visibilityFilter(state, action) { | |
if (state === undefined) state = 'SHOW_ALL'; | |
switch (action.type) { | |
case 'SET_VISIBILITY_FILTER': | |
return action.filter; | |
default: | |
return state; | |
} | |
}; | |
var todos = function todos(state, action) { | |
if (state === undefined) state = []; | |
var ret = undefined; | |
switch (action.type) { | |
case 'ADD_TODO': | |
ret = [].concat(_toConsumableArray(state), [todo(undefined, action)]); | |
return ret; | |
case 'TOGGLE_TODO': | |
ret = state.map(function (t) { | |
return todo(t, action); | |
}); | |
return ret; | |
default: | |
return state; | |
} | |
}; | |
var combineReducers = function combineReducers(reducers) { | |
return function (state, action) { | |
if (state === undefined) state = {}; | |
return Object.keys(reducers).reduce(function (nextState, key) { | |
nextState[key] = reducers[key](state[key], action); | |
return nextState; | |
}, {}); | |
}; | |
}; | |
var todoApp = combineReducers({ | |
todos: todos, | |
visibilityFilter: visibilityFilter | |
}); | |
var _Redux = Redux; | |
var createStore = _Redux.createStore; | |
var store = createStore(todoApp); | |
console.log('Initial state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO...'); | |
console.log(store.dispatch({ | |
type: 'ADD_TODO', | |
id: 0, | |
text: 'Learn Redux' | |
})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching ADD_TODO...'); | |
console.log(store.dispatch({ | |
type: 'ADD_TODO', | |
id: 1, | |
text: 'Go shopping' | |
})); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching TOGGLE_TODO...'); | |
console.log(store.dispatch({ | |
type: 'TOGGLE_TODO', | |
id: 0 })); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); | |
console.log('Dispatching SET_VISIBILITY_FILTER...'); | |
console.log(store.dispatch({ | |
type: 'SET_VISIBILITY_FILTER', | |
filter: 'SHOW_COMPLETED' })); | |
console.log('Current state:'); | |
console.log(store.getState()); | |
console.log('--------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment