Skip to content

Instantly share code, notes, and snippets.

Created March 21, 2016 18:04
Show Gist options
  • Save anonymous/249fcc94667d8eced1f0 to your computer and use it in GitHub Desktop.
Save anonymous/249fcc94667d8eced1f0 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/harazu
<!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';
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 addCounter = function addCounter(list) {
// list.push(0); // bad - mutates original array
// return list;
// return list.concat([0]); // better - does not mutate
// best - ES2015
return [].concat(_toConsumableArray(list), [0]);
};
var removeCounter = function removeCounter(list, index) {
// list.splice(index, 1); // bad - mutates original array
// return list;
// return list // better - does not mutate
// .slice(0, index)
// .concat(list.slice(index + 1));
// best - ES2015
return [].concat(_toConsumableArray(list.slice(0, index)), _toConsumableArray(list.slice(index + 1)));
};
var incrementCounter = function incrementCounter() {};
var addCounterTest = function addCounterTest() {
var listBefore = [];
var listAfter = [0];
deepFreeze(listBefore);
expect(addCounter(listBefore)).toEqual(listAfter);
};
var removeCounterTest = function removeCounterTest() {
var listBefore = [0, 10, 20];
var listAfter = [0, 20];
deepFreeze(listBefore);
expect(removeCounter(listBefore, 1)).toEqual(listAfter);
};
var incrementCounterTest = function incrementCounterTest() {};
addCounterTest();
removeCounterTest();
console.log('All tests pass!');
</script>
<script id="jsbin-source-javascript" type="text/javascript">const addCounter = (list) => {
// list.push(0); // bad - mutates original array
// return list;
// return list.concat([0]); // better - does not mutate
// best - ES2015
return [...list, 0];
};
const removeCounter = (list, index) => {
// list.splice(index, 1); // bad - mutates original array
// return list;
// return list // better - does not mutate
// .slice(0, index)
// .concat(list.slice(index + 1));
// best - ES2015
return [
...list.slice(0, index),
...list.slice(index + 1)
];
};
const incrementCounter = () => {
};
const addCounterTest = () => {
const listBefore = [];
const listAfter = [0];
deepFreeze(listBefore);
expect(
addCounter(listBefore)
).toEqual(listAfter);
};
const removeCounterTest = () => {
const listBefore = [0, 10, 20];
const listAfter = [0, 20];
deepFreeze(listBefore);
expect(
removeCounter(listBefore, 1)
).toEqual(listAfter);
};
const incrementCounterTest = () => {
};
addCounterTest();
removeCounterTest();
console.log('All tests pass!');</script></body>
</html>
'use strict';
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 addCounter = function addCounter(list) {
// list.push(0); // bad - mutates original array
// return list;
// return list.concat([0]); // better - does not mutate
// best - ES2015
return [].concat(_toConsumableArray(list), [0]);
};
var removeCounter = function removeCounter(list, index) {
// list.splice(index, 1); // bad - mutates original array
// return list;
// return list // better - does not mutate
// .slice(0, index)
// .concat(list.slice(index + 1));
// best - ES2015
return [].concat(_toConsumableArray(list.slice(0, index)), _toConsumableArray(list.slice(index + 1)));
};
var incrementCounter = function incrementCounter() {};
var addCounterTest = function addCounterTest() {
var listBefore = [];
var listAfter = [0];
deepFreeze(listBefore);
expect(addCounter(listBefore)).toEqual(listAfter);
};
var removeCounterTest = function removeCounterTest() {
var listBefore = [0, 10, 20];
var listAfter = [0, 20];
deepFreeze(listBefore);
expect(removeCounter(listBefore, 1)).toEqual(listAfter);
};
var incrementCounterTest = function incrementCounterTest() {};
addCounterTest();
removeCounterTest();
console.log('All tests pass!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment