Skip to content

Instantly share code, notes, and snippets.

@LucasCaixeta
Created January 30, 2018 16:02
Show Gist options
  • Save LucasCaixeta/edb1f1ba319a1e89ff40a0b270e4108f to your computer and use it in GitHub Desktop.
Save LucasCaixeta/edb1f1ba319a1e89ff40a0b270e4108f to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/qobefifiwe
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<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">
/*
* Open the console to see
* that all tests have passed.
*/
'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) {
return [].concat(_toConsumableArray(list), [0]);
};
var removeCounter = function removeCounter(list, index) {
return list.filter(function (el, i) {
return index !== i;
});
};
var incrementCounter = function incrementCounter(list, index) {
return list.map(function (item, localIndex) {
return localIndex === index ? item + 1 : item;
});
};
var testAddCounter = function testAddCounter() {
var listBefore = [];
var listAfter = [0];
deepFreeze(listBefore);
expect(addCounter(listBefore)).toEqual(listAfter);
};
var testRemoveCounter = function testRemoveCounter() {
var listBefore = [0, 10, 20];
var listAfter = [0, 20];
deepFreeze(listBefore);
expect(removeCounter(listBefore, 1)).toEqual(listAfter);
};
var testIncrementCounter = function testIncrementCounter() {
var listBefore = [0, 10, 20];
var listAfter = [0, 11, 20];
deepFreeze(listBefore);
expect(incrementCounter(listBefore, 1)).toEqual(listAfter);
};
testAddCounter();
testRemoveCounter();
testIncrementCounter();
console.log('All tests passed.');
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
* Open the console to see
* that all tests have passed.
*/
const addCounter = (list) => {
return [...list, 0];
};
const removeCounter = (list, index) => {
return list.filter((el, i)=> index !==i);
};
const incrementCounter = (list, index) => {
return list.map((item, localIndex) => localIndex === index ? item + 1 : item)
};
const testAddCounter = () => {
const listBefore = [];
const listAfter = [0];
deepFreeze(listBefore);
expect(
addCounter(listBefore)
).toEqual(listAfter);
};
const testRemoveCounter = () => {
const listBefore = [0, 10, 20];
const listAfter = [0, 20];
deepFreeze(listBefore);
expect(
removeCounter(listBefore, 1)
).toEqual(listAfter);
};
const testIncrementCounter = () => {
const listBefore = [0, 10, 20];
const listAfter = [0, 11, 20];
deepFreeze(listBefore);
expect(
incrementCounter(listBefore, 1)
).toEqual(listAfter);
};
testAddCounter();
testRemoveCounter();
testIncrementCounter();
console.log('All tests passed.');
</script></body>
</html>
/*
* Open the console to see
* that all tests have passed.
*/
'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) {
return [].concat(_toConsumableArray(list), [0]);
};
var removeCounter = function removeCounter(list, index) {
return list.filter(function (el, i) {
return index !== i;
});
};
var incrementCounter = function incrementCounter(list, index) {
return list.map(function (item, localIndex) {
return localIndex === index ? item + 1 : item;
});
};
var testAddCounter = function testAddCounter() {
var listBefore = [];
var listAfter = [0];
deepFreeze(listBefore);
expect(addCounter(listBefore)).toEqual(listAfter);
};
var testRemoveCounter = function testRemoveCounter() {
var listBefore = [0, 10, 20];
var listAfter = [0, 20];
deepFreeze(listBefore);
expect(removeCounter(listBefore, 1)).toEqual(listAfter);
};
var testIncrementCounter = function testIncrementCounter() {
var listBefore = [0, 10, 20];
var listAfter = [0, 11, 20];
deepFreeze(listBefore);
expect(incrementCounter(listBefore, 1)).toEqual(listAfter);
};
testAddCounter();
testRemoveCounter();
testIncrementCounter();
console.log('All tests passed.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment