Skip to content

Instantly share code, notes, and snippets.

Created June 13, 2016 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bc54d1b8456d882564773511fdb0a408 to your computer and use it in GitHub Desktop.
Save anonymous/bc54d1b8456d882564773511fdb0a408 to your computer and use it in GitHub Desktop.
Avoiding mutations // source http://jsbin.com/fagede
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Avoiding mutations</title>
<script src="http://wzrd.in/standalone/expect@latest"></script>
<script src="http://wzrd.in/standalone/deep-freeze@latest"></script>
</head>
<body>
<script id="jsbin-javascript">
const addCounter = (list) => {
//list.push(0);
//return list;
return [...list, 0];
};
const removeCounter = (list, index) => {
return [
...list.slice(0, index),
...list.slice(index + 1)
];
};
const incrementCounter = (list, index) => {
return [
...list.slice(0, index),
list[index] + 1,
...list.slice(index + 1)
]
};
const testAddCounter = () => {
const listBefore = [];
const listAfter = [0];
deepFreeze(listBefore);
expect(
addCounter(listBefore)
).toEqual(listAfter);
};
const testRemoveCounter = () => {
const listBefore = [10, 20, 30];
const listAfter = [10, 20];
deepFreeze(listBefore);
expect(
removeCounter(listBefore, 2)
).toEqual(listAfter);
};
const testIncrementCounter = () => {
const listBefore = [10, 20, 30];
const listAfter = [10, 21, 30];
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">const addCounter = (list) => {
//list.push(0);
//return list;
return [...list, 0];
};
const removeCounter = (list, index) => {
return [
...list.slice(0, index),
...list.slice(index + 1)
];
};
const incrementCounter = (list, index) => {
return [
...list.slice(0, index),
list[index] + 1,
...list.slice(index + 1)
]
};
const testAddCounter = () => {
const listBefore = [];
const listAfter = [0];
deepFreeze(listBefore);
expect(
addCounter(listBefore)
).toEqual(listAfter);
};
const testRemoveCounter = () => {
const listBefore = [10, 20, 30];
const listAfter = [10, 20];
deepFreeze(listBefore);
expect(
removeCounter(listBefore, 2)
).toEqual(listAfter);
};
const testIncrementCounter = () => {
const listBefore = [10, 20, 30];
const listAfter = [10, 21, 30];
deepFreeze(listBefore);
expect(
incrementCounter(listBefore, 1)
).toEqual(listAfter);
};
testAddCounter();
testRemoveCounter();
testIncrementCounter();
console.log("All tests passed");</script></body>
</html>
const addCounter = (list) => {
//list.push(0);
//return list;
return [...list, 0];
};
const removeCounter = (list, index) => {
return [
...list.slice(0, index),
...list.slice(index + 1)
];
};
const incrementCounter = (list, index) => {
return [
...list.slice(0, index),
list[index] + 1,
...list.slice(index + 1)
]
};
const testAddCounter = () => {
const listBefore = [];
const listAfter = [0];
deepFreeze(listBefore);
expect(
addCounter(listBefore)
).toEqual(listAfter);
};
const testRemoveCounter = () => {
const listBefore = [10, 20, 30];
const listAfter = [10, 20];
deepFreeze(listBefore);
expect(
removeCounter(listBefore, 2)
).toEqual(listAfter);
};
const testIncrementCounter = () => {
const listBefore = [10, 20, 30];
const listAfter = [10, 21, 30];
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