Skip to content

Instantly share code, notes, and snippets.

@th3mon
Last active October 29, 2017 00:00
Show Gist options
  • Save th3mon/4e210f3d06aa1683e86aff56d72bb7d4 to your computer and use it in GitHub Desktop.
Save th3mon/4e210f3d06aa1683e86aff56d72bb7d4 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/muqigog
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
<script id="jsbin-javascript">
console.clear()
const addCounter = list => [...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 = [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>
<script id="jsbin-source-javascript" type="text/javascript">console.clear()
const addCounter = list => [...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 = [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>
console.clear()
const addCounter = list => [...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 = [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.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment