-
-
Save TimCodes/e9c318ba699cbc35fa935a0140f6f6f0 to your computer and use it in GitHub Desktop.
simple reducers example
This file contains hidden or 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 name="description" content="[simple react counter]"> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <link href="http://extjs.cachefly.net/ext-3.1.0/resources/css/ext-all.css" rel="stylesheet" type="text/css" /> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/ext-core/3.1.0/ext-core.min.js"></script> | |
| <script src="https://fb.me/react-15.1.0.js"></script> | |
| <script src="https://fb.me/react-dom-15.1.0.js"></script> | |
| <script src = "https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"></script> | |
| <script src = "https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script> | |
| <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) { | |
| //list.concat([0]) | |
| return [].concat(_toConsumableArray(list), [0]); | |
| }; | |
| var removeCounter = function removeCounter(list, idx) { | |
| return list.slice(0, idx).concat(list.slice(idx + 1)); | |
| }; | |
| var incrementCounter = function incrementCounter(list, index) { | |
| return [].concat(_toConsumableArray(list.slice(0, index)), [list[index] + 1], _toConsumableArray(list.slice(index + 1))); | |
| }; | |
| var testAddCounter = function testAddCounter() { | |
| var listBefore = []; | |
| var listAfter = [0]; | |
| expect(addCounter(listBefore)).to.eql(listAfter); | |
| }; | |
| var testRemoveCounter = function testRemoveCounter() { | |
| var listBefore = [0, 10, 20]; | |
| var listAfter = [0, 20]; | |
| expect(removeCounter(listBefore, 1)).to.eql(listAfter); | |
| }; | |
| var testIncrementCounter = function testIncrementCounter() { | |
| var listBefore = [0, 10, 20]; | |
| var listAfter = [0, 11, 20]; | |
| expect(incrementCounter(listBefore, 1)).to.eql(listAfter); | |
| }; | |
| testAddCounter(); | |
| testRemoveCounter(); | |
| testIncrementCounter(); | |
| testAddCounter(); | |
| console.log("ALL TEST PASS"); | |
| </script> | |
| <script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta name="description" content="[simple react counter]"> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <link href="//extjs.cachefly.net/ext-3.1.0/resources/css/ext-all.css" rel="stylesheet" type="text/css" /> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/ext-core/3.1.0/ext-core.min.js"><\/script> | |
| <script src="https://fb.me/react-15.1.0.js"><\/script> | |
| <script src="https://fb.me/react-dom-15.1.0.js"><\/script> | |
| <script src = "https://cdnjs.cloudflare.com/ajax/libs/redux/3.6.0/redux.min.js"><\/script> | |
| <script src = "https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"><\/script> | |
| <div id="root"> | |
| </div> | |
| </body> | |
| </html></script> | |
| <script id="jsbin-source-javascript" type="text/javascript">const addCounter = (list) =>{ | |
| //list.concat([0]) | |
| return [...list, 0] | |
| } | |
| const removeCounter = (list, idx) =>{ | |
| return list | |
| .slice(0, idx) | |
| .concat(list.slice(idx + 1)) | |
| } | |
| const incrementCounter = (list, index) => { | |
| return [ | |
| ...list.slice(0, index), | |
| list[index] + 1, | |
| ...list.slice(index + 1) | |
| ]; | |
| }; | |
| const testAddCounter = () => { | |
| const listBefore = []; | |
| const listAfter = [0]; | |
| expect( | |
| addCounter(listBefore) | |
| ).to.eql(listAfter) | |
| } | |
| const testRemoveCounter = () => { | |
| let listBefore = [0,10,20]; | |
| let listAfter = [0,20] | |
| expect( | |
| removeCounter(listBefore,1) | |
| ).to.eql(listAfter) | |
| } | |
| const testIncrementCounter = () => { | |
| const listBefore = [0, 10, 20]; | |
| const listAfter = [0, 11, 20]; | |
| expect( | |
| incrementCounter(listBefore, 1) | |
| ).to.eql(listAfter); | |
| }; | |
| testAddCounter(); | |
| testRemoveCounter() | |
| testIncrementCounter(); | |
| testAddCounter() | |
| console.log("ALL TEST PASS")</script></body> | |
| </html> |
This file contains hidden or 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) { | |
| //list.concat([0]) | |
| return [].concat(_toConsumableArray(list), [0]); | |
| }; | |
| var removeCounter = function removeCounter(list, idx) { | |
| return list.slice(0, idx).concat(list.slice(idx + 1)); | |
| }; | |
| var incrementCounter = function incrementCounter(list, index) { | |
| return [].concat(_toConsumableArray(list.slice(0, index)), [list[index] + 1], _toConsumableArray(list.slice(index + 1))); | |
| }; | |
| var testAddCounter = function testAddCounter() { | |
| var listBefore = []; | |
| var listAfter = [0]; | |
| expect(addCounter(listBefore)).to.eql(listAfter); | |
| }; | |
| var testRemoveCounter = function testRemoveCounter() { | |
| var listBefore = [0, 10, 20]; | |
| var listAfter = [0, 20]; | |
| expect(removeCounter(listBefore, 1)).to.eql(listAfter); | |
| }; | |
| var testIncrementCounter = function testIncrementCounter() { | |
| var listBefore = [0, 10, 20]; | |
| var listAfter = [0, 11, 20]; | |
| expect(incrementCounter(listBefore, 1)).to.eql(listAfter); | |
| }; | |
| testAddCounter(); | |
| testRemoveCounter(); | |
| testIncrementCounter(); | |
| testAddCounter(); | |
| console.log("ALL TEST PASS"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment