Skip to content

Instantly share code, notes, and snippets.

@adilbenmoussa
Created May 26, 2017 09:47
Show Gist options
  • Save adilbenmoussa/cf9ee752d8cdcc4372e63d724d5a0303 to your computer and use it in GitHub Desktop.
Save adilbenmoussa/cf9ee752d8cdcc4372e63d724d5a0303 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/bobitif
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="javascript-redux-avoiding-object-mutations-with-object-assign-and-spread">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://unpkg.com/expect/umd/expect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/freezer-js/0.12.1/freezer.js"></script>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<title>JS Bin</title>
</head>
<body>
<div id="root"></div>
<script id="jsbin-javascript">
// javascript-redux-avoiding-object-mutations-with-object-assign-and-spread
'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; };
var toggleTodo = function toggleTodo(todo) {
return _extends({}, todo, {
completed: !todo.completed
});
// return Object.assign({},todo,
// {completed: !todo.completed})
};
var testToggleTodo = function testToggleTodo() {
var todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
var todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};
Object.freeze(todoBefore);
expect(toggleTodo(todoBefore)).toEqual(todoAfter);
};
testToggleTodo();
console.log('Tests Ok!');
</script>
<script id="jsbin-source-javascript" type="text/javascript">// javascript-redux-avoiding-object-mutations-with-object-assign-and-spread
const toggleTodo = (todo) => {
return {
...todo,
completed: !todo.completed
};
// return Object.assign({},todo,
// {completed: !todo.completed})
};
const testToggleTodo = () => {
const todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
const todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};
Object.freeze(todoBefore);
expect(
toggleTodo(todoBefore)
).toEqual(todoAfter);
};
testToggleTodo();
console.log('Tests Ok!')
</script></body>
</html>
// javascript-redux-avoiding-object-mutations-with-object-assign-and-spread
'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; };
var toggleTodo = function toggleTodo(todo) {
return _extends({}, todo, {
completed: !todo.completed
});
// return Object.assign({},todo,
// {completed: !todo.completed})
};
var testToggleTodo = function testToggleTodo() {
var todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
var todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};
Object.freeze(todoBefore);
expect(toggleTodo(todoBefore)).toEqual(todoAfter);
};
testToggleTodo();
console.log('Tests Ok!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment