Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 486268f

Browse files
thall1961Kent C. Dodds
authored andcommitted
feat(reduceCount): added reduceCount function (#211)
* feat(reduceCount): added reduceCount function to count occurences of param in array(s) * feat(reduceCount): removed history folder
1 parent a6a9c5e commit 486268f

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import removeProperty from './removeProperty'
7777
import temperatureConverter from './temperatureConverter'
7878
import last from './last'
7979
import descendingOrder from './descending-order'
80+
import reduceCount from './reduceCount'
8081

8182
export {
8283
reverseArrayInPlace,
@@ -158,4 +159,5 @@ export {
158159
temperatureConverter,
159160
last,
160161
descendingOrder,
162+
reduceCount,
161163
}

src/reduceCount.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default reduceCount
2+
3+
/**
4+
* Original Source: https://egghead.io/courses/reduce-data-with-javascript
5+
*
6+
* The function will count how many times an item occurs in an array,
7+
* even in arrays within the array.
8+
* @param {array} array - The array
9+
* @param {any type} item - The variable whose occurences will be counted
10+
* @return {number} - The amount of times the item is found in the array(s)
11+
*/
12+
13+
function reduceCount(array, itemToCount) {
14+
// make sure the array passed in is actually an array
15+
if (!Array.isArray(array)) {
16+
return `${array} isn't an array.`
17+
}
18+
19+
// make sure we got something to work with in that array
20+
if (array.length === 0) {
21+
return `${array} is empty.`
22+
}
23+
24+
// reduce the array and count the occurences of item
25+
return array.reduce((a, b) => {
26+
if (Array.isArray(b)) {
27+
return a + reduceCount(b, itemToCount)
28+
}
29+
return a + (b === itemToCount ? 1 : 0)
30+
}, 0)
31+
}

test/reduceCount.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava'
2+
import {reduceCount} from '../src'
3+
4+
test('first param has to be an array', t => {
5+
const array = {}
6+
const expected = `${array} isn't an array.`
7+
const actual = reduceCount(array, 0)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('array must have at least one value', t => {
12+
const array = []
13+
const expected = `${array} is empty.`
14+
const actual = reduceCount(array, 0)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('array will count occurences of integer param', t => {
19+
const array = [1, 2, [1, 3], 4, 5, [1, 6, 7]]
20+
const expected = 3
21+
const actual = reduceCount(array, 1)
22+
t.deepEqual(actual, expected)
23+
})
24+
25+
test('array will count occurences of string param', t => {
26+
const array = ['six', 2, [1, 'six'], 4, 5, [1, 'six', 7], 'six', 'sicks']
27+
const expected = 4
28+
const actual = reduceCount(array, 'six')
29+
t.deepEqual(actual, expected)
30+
})
31+
32+
test('array will count occurences of float param', t => {
33+
const array = [1, 2, [1, 3], 4, 6.4, [1, 6.4, 7], 6.4, 6.5, 4.6]
34+
const expected = 3
35+
const actual = reduceCount(array, 6.4)
36+
t.deepEqual(actual, expected)
37+
})

0 commit comments

Comments
 (0)