Created
June 13, 2016 15:22
-
-
Save anonymous/bc54d1b8456d882564773511fdb0a408 to your computer and use it in GitHub Desktop.
Avoiding mutations // source http://jsbin.com/fagede
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 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> |
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
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