| Conditions | 1 |
| Paths | 1 |
| Total Lines | 100 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 3 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var utilities = require('../src/utilities.js'); |
||
| 3 | describe('utilities.toObject', function() { |
||
| 4 | describe('failing tests', function() { |
||
| 5 | it('should fail because the arr parameter is undefined', function() { |
||
| 6 | expect(utilities.toObject).toThrowError(TypeError); |
||
| 7 | }); |
||
| 8 | |||
| 9 | it('should fail because the arr parameter is not an Array', function() { |
||
| 10 | expect(utilities.toObject.bind(null, {})).toThrowError(TypeError); |
||
| 11 | }); |
||
| 12 | |||
| 13 | it('should fail because the mapBy not of type String, Array or Function and not falsy', function() { |
||
| 14 | expect(utilities.toObject.bind(null, [], {})).toThrowError(TypeError); |
||
| 15 | }); |
||
| 16 | }); |
||
| 17 | |||
| 18 | describe('array of primitives toObject', function() { |
||
| 19 | // test example from README.md |
||
| 20 | var states = ['Sachsen', 'Sachsen-Anhalt', 'Berlin', 'Hamburg']; |
||
| 21 | var statesObject = utilities.toObject(states); |
||
| 22 | // end test example from README.md |
||
| 23 | |||
| 24 | it('should be able to access object by index', function() { |
||
| 25 | expect(statesObject[0]).toEqual('Sachsen'); |
||
| 26 | }); |
||
| 27 | |||
| 28 | it('should have 4 keys', function() { |
||
| 29 | expect(Object.keys(statesObject).length).toEqual(4); |
||
| 30 | }); |
||
| 31 | }); |
||
| 32 | |||
| 33 | var news = [ |
||
| 34 | { |
||
| 35 | id: 12001, |
||
| 36 | headline: 'Tiger goes limp', |
||
| 37 | subHeadline: 'Pulls out after 9 holes' |
||
| 38 | }, |
||
| 39 | { |
||
| 40 | id: 666, |
||
| 41 | headline: 'Croc has beef with cow', |
||
| 42 | subHeadline: '' |
||
| 43 | }, |
||
| 44 | { |
||
| 45 | id: 1337, |
||
| 46 | headline: 'Germans wurst at penalties', |
||
| 47 | subHeadline: 'New stats prove England are better from the spot' |
||
| 48 | } |
||
| 49 | ]; |
||
| 50 | |||
| 51 | describe('array of objects toObject - string mapBy', function() { |
||
| 52 | var newsObject = utilities.toObject(news, 'id'); |
||
| 53 | |||
| 54 | it('should be able to access object by ID', function() { |
||
| 55 | expect(newsObject[1337].headline).toEqual('Germans wurst at penalties'); |
||
| 56 | }); |
||
| 57 | |||
| 58 | it('should have 3 keys', function() { |
||
| 59 | expect(Object.keys(newsObject).length).toEqual(3); |
||
| 60 | }); |
||
| 61 | }); |
||
| 62 | |||
| 63 | describe('array of objects toObject - array mapBy', function() { |
||
| 64 | var newsObject = utilities.toObject(news, ['id', 'id']); |
||
| 65 | |||
| 66 | it('should be able to access object by ID_ID', function() { |
||
| 67 | expect(newsObject['1337_1337'].headline).toEqual( |
||
| 68 | 'Germans wurst at penalties' |
||
| 69 | ); |
||
| 70 | }); |
||
| 71 | |||
| 72 | it('should have 3 keys', function() { |
||
| 73 | expect(Object.keys(newsObject).length).toEqual(3); |
||
| 74 | }); |
||
| 75 | }); |
||
| 76 | |||
| 77 | describe('array of objects toObject - function mapBy', function() { |
||
| 78 | var newsObject = utilities.toObject(news, function(val, i) { |
||
| 79 | return val.id + '_' + i; |
||
| 80 | }); |
||
| 81 | |||
| 82 | it('should be able to access object by ID_index', function() { |
||
| 83 | expect(newsObject['1337_2'].headline).toEqual( |
||
| 84 | 'Germans wurst at penalties' |
||
| 85 | ); |
||
| 86 | }); |
||
| 87 | |||
| 88 | it('should have 3 keys', function() { |
||
| 89 | expect(Object.keys(newsObject).length).toEqual(3); |
||
| 90 | }); |
||
| 91 | }); |
||
| 92 | |||
| 93 | describe('array of objects toObject - function provides neither string or number as new index', function() { |
||
| 94 | var newsObject = utilities.toObject(news, function(val, i) { |
||
| 95 | return [i]; |
||
| 96 | }); |
||
| 97 | |||
| 98 | it('should have 0 keys', function() { |
||
| 99 | expect(Object.keys(newsObject).length).toEqual(0); |
||
| 100 | }); |
||
| 101 | }); |
||
| 102 | }); |
||
| 103 |