Created
February 22, 2017 21:38
-
-
Save sebabouche/9198fbbd4b45c33950dbfcd642494a93 to your computer and use it in GitHub Desktop.
JS Bin Object Assign and Spread to return a new javascript object // source http://jsbin.com/hujiqa
This file contains 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> | |
<meta name="description" content="Object Assign and Spread to return a new javascript object"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://wzrd.in/standalone/expect@latest"></script> | |
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script> | |
</head> | |
<body> | |
<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; }; | |
console.clear(); | |
console.log('Running...'); | |
var toggleTodo = function toggleTodo(todo) { | |
return _extends({}, 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 | |
}; | |
deepFreeze(todoBefore); | |
deepFreeze(todoAfter); | |
expect(toggleTodo(todoBefore)).toEqual(todoAfter); | |
expect(toggleTodo(todoAfter)).toEqual(todoBefore); | |
}; | |
testToggleTodo(); | |
console.log('Passed'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear() | |
console.log('Running...') | |
const toggleTodo = (todo) => { | |
return { | |
...todo, | |
completed: !todo.completed | |
} | |
} | |
const testToggleTodo = () => { | |
const todoBefore = { | |
id: 0, | |
text: 'Learn Redux', | |
completed: false | |
} | |
const todoAfter = { | |
id: 0, | |
text: 'Learn Redux', | |
completed: true | |
} | |
deepFreeze(todoBefore) | |
deepFreeze(todoAfter) | |
expect( | |
toggleTodo(todoBefore) | |
).toEqual(todoAfter) | |
expect( | |
toggleTodo(todoAfter) | |
).toEqual(todoBefore) | |
} | |
testToggleTodo() | |
console.log('Passed')</script></body> | |
</html> |
This file contains 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; }; | |
console.clear(); | |
console.log('Running...'); | |
var toggleTodo = function toggleTodo(todo) { | |
return _extends({}, 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 | |
}; | |
deepFreeze(todoBefore); | |
deepFreeze(todoAfter); | |
expect(toggleTodo(todoBefore)).toEqual(todoAfter); | |
expect(toggleTodo(todoAfter)).toEqual(todoBefore); | |
}; | |
testToggleTodo(); | |
console.log('Passed'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment