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

Commit f100041

Browse files
yanks1987Kent C. Dodds
authored andcommitted
feat(reduce): reduce functionality (#78)
* feat(reduce): reduce functionality Using Reduce to tally votes * fix(review): addressed review comments replaced double quotes with single
1 parent 7b7d258 commit f100041

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
import reduce from './reduce-to-tally'
13
import flatten from './flatten'
24
import getQueryStringParam from './get-query-string-param'
35
import snakeToCamel from './snake-to-camel'
@@ -24,6 +26,7 @@ import dec2bin from './dec2bin'
2426
import searchAndReplace from './search-and-replace'
2527

2628
export {
29+
reduce,
2730
flatten,
2831
snakeToCamel,
2932
getQueryStringParam,

src/reduce-to-tally.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default reducer
2+
3+
/**
4+
* Original Source: https://egghead.io/courses/reduce-data-with-javascript
5+
*
6+
* This method will reduce duplicate values in an array,
7+
* into an object with count for each individual value.
8+
* @param {Object} tallyMap - The object from which the values are taken
9+
* @param {number} vote - The vote defines a item in array
10+
* @return {object} - A comma-separated Object for each individual array values with count
11+
*/
12+
function reducer(tallyMap, vote) {
13+
if (tallyMap.hasOwnProperty(vote)) {
14+
tallyMap[vote] = tallyMap[vote] + 1
15+
} else {
16+
tallyMap[vote] = 1
17+
}
18+
return tallyMap
19+
}

test/reduce-to-tally.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
import reducer from '../src/reduce-to-tally'
3+
4+
test('tallies an array into object with count on each', t => {
5+
const initialvalue = {}
6+
const original = ['angular', 'angular', 'rails', 'angular', 'Python', 'angular', 'Python', 'angular', 'Vanilla']
7+
const expected = {Python: 2, Vanilla: 1, angular: 5, rails: 1}
8+
const actual = original.reduce(reducer, initialvalue)
9+
t.deepEqual(actual, expected)
10+
})

0 commit comments

Comments
 (0)