Last active
March 18, 2017 06:05
-
-
Save yashikagarg13/8dba9b64afa8ee2ceba080bbc1deefd4 to your computer and use it in GitHub Desktop.
Updating list without mutating it using slice(), concat() and spread (...)
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"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<script src="https://npmcdn.com/expect/umd/expect.min.js"></script> | |
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script> | |
</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) { | |
var initialState = 0; | |
return [].concat(_toConsumableArray(list), [0]); | |
// return list.concat([initialState]) | |
}; | |
var removeCounter = function removeCounter(list, index) { | |
return [].concat(_toConsumableArray(list.slice(0, index)), _toConsumableArray(list.slice(index + 1))); | |
}; | |
var incrementCounter = function incrementCounter(list, index) { | |
var counterState = list[index] + 1; | |
return [].concat(_toConsumableArray(list.slice(0, index)), [counterState], _toConsumableArray(list.slice(index + 1))); | |
}; | |
var decrementCounter = function decrementCounter(list, index) { | |
var counterState = list[index] - 1; | |
return [].concat(_toConsumableArray(list.slice(0, index)), [counterState], _toConsumableArray(list.slice(index + 1))); | |
}; | |
var testAddCounter = function testAddCounter() { | |
var listBefore = []; | |
var listAfter = [0]; | |
deepFreeze(listBefore); | |
expect(addCounter(listBefore)).toEqual(listAfter); | |
}; | |
var testRemoveCounter = function testRemoveCounter() { | |
var listBefore = [0, 10, 1]; | |
var listAfter = [0, 1]; | |
deepFreeze(listBefore); | |
expect(removeCounter(listBefore, 1)).toEqual(listAfter); | |
}; | |
var testIncrementCounter = function testIncrementCounter() { | |
var listBefore = [0, 10, 1]; | |
var listAfter = [0, 11, 1]; | |
deepFreeze(listBefore); | |
expect(incrementCounter(listBefore, 1)).toEqual(listAfter); | |
}; | |
var testDecrementCounter = function testDecrementCounter() { | |
var listBefore = [0, 10, 1]; | |
var listAfter = [0, 9, 1]; | |
deepFreeze(listBefore); | |
expect(decrementCounter(listBefore, 1)).toEqual(listAfter); | |
}; | |
testAddCounter(); | |
testRemoveCounter(); | |
testIncrementCounter(); | |
testDecrementCounter(); | |
console.log("Test passed!!"); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const addCounter = (list) => { | |
let initialState = 0; | |
return [...list, 0]; | |
// return list.concat([initialState]) | |
} | |
const removeCounter = (list, index) => { | |
return [...list.slice(0, index), | |
...list.slice(index + 1)] | |
} | |
const incrementCounter = (list, index) => { | |
const counterState = list[index] + 1; | |
return [...list.slice(0, index), | |
counterState, | |
...list.slice(index + 1)] | |
} | |
const decrementCounter = (list, index) => { | |
const counterState = list[index] - 1; | |
return [...list.slice(0, index), | |
counterState, | |
...list.slice(index + 1)] | |
} | |
const testAddCounter = () => { | |
const listBefore = []; | |
const listAfter = [0]; | |
deepFreeze(listBefore); | |
expect(addCounter(listBefore)).toEqual(listAfter); | |
} | |
const testRemoveCounter = () => { | |
const listBefore = [0, 10, 1]; | |
const listAfter = [0, 1]; | |
deepFreeze(listBefore); | |
expect(removeCounter(listBefore, 1)).toEqual(listAfter); | |
} | |
const testIncrementCounter = () => { | |
const listBefore = [0, 10, 1]; | |
const listAfter = [0, 11, 1]; | |
deepFreeze(listBefore); | |
expect(incrementCounter(listBefore, 1)).toEqual(listAfter); | |
} | |
const testDecrementCounter = () => { | |
const listBefore = [0, 10, 1]; | |
const listAfter = [0, 9, 1]; | |
deepFreeze(listBefore); | |
expect(decrementCounter(listBefore, 1)).toEqual(listAfter); | |
} | |
testAddCounter(); | |
testRemoveCounter(); | |
testIncrementCounter(); | |
testDecrementCounter(); | |
console.log("Test passed!!"); | |
</script></body></script> | |
</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
"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) { | |
var initialState = 0; | |
return [].concat(_toConsumableArray(list), [0]); | |
// return list.concat([initialState]) | |
}; | |
var removeCounter = function removeCounter(list, index) { | |
return [].concat(_toConsumableArray(list.slice(0, index)), _toConsumableArray(list.slice(index + 1))); | |
}; | |
var incrementCounter = function incrementCounter(list, index) { | |
var counterState = list[index] + 1; | |
return [].concat(_toConsumableArray(list.slice(0, index)), [counterState], _toConsumableArray(list.slice(index + 1))); | |
}; | |
var decrementCounter = function decrementCounter(list, index) { | |
var counterState = list[index] - 1; | |
return [].concat(_toConsumableArray(list.slice(0, index)), [counterState], _toConsumableArray(list.slice(index + 1))); | |
}; | |
var testAddCounter = function testAddCounter() { | |
var listBefore = []; | |
var listAfter = [0]; | |
deepFreeze(listBefore); | |
expect(addCounter(listBefore)).toEqual(listAfter); | |
}; | |
var testRemoveCounter = function testRemoveCounter() { | |
var listBefore = [0, 10, 1]; | |
var listAfter = [0, 1]; | |
deepFreeze(listBefore); | |
expect(removeCounter(listBefore, 1)).toEqual(listAfter); | |
}; | |
var testIncrementCounter = function testIncrementCounter() { | |
var listBefore = [0, 10, 1]; | |
var listAfter = [0, 11, 1]; | |
deepFreeze(listBefore); | |
expect(incrementCounter(listBefore, 1)).toEqual(listAfter); | |
}; | |
var testDecrementCounter = function testDecrementCounter() { | |
var listBefore = [0, 10, 1]; | |
var listAfter = [0, 9, 1]; | |
deepFreeze(listBefore); | |
expect(decrementCounter(listBefore, 1)).toEqual(listAfter); | |
}; | |
testAddCounter(); | |
testRemoveCounter(); | |
testIncrementCounter(); | |
testDecrementCounter(); | |
console.log("Test passed!!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment