From 185d7c58c23c69c4334f6333362dea008bb2334b Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Sat, 27 Apr 2019 18:41:26 +0100 Subject: [PATCH] Finish group-by util Move function to util, add tests. --- app/services/kredits.js | 27 +++----------------- app/utils/group-by.js | 27 ++++++++++++++++++++ tests/unit/utils/group-by-test.js | 41 +++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 app/utils/group-by.js create mode 100644 tests/unit/utils/group-by-test.js diff --git a/app/services/kredits.js b/app/services/kredits.js index 7c40cbc..7062625 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -2,12 +2,12 @@ import ethers from 'npm:ethers'; import Kredits from 'npm:kredits-contracts'; import RSVP from 'rsvp'; -import { A } from '@ember/array'; import Service from '@ember/service'; -import { computed, get } from '@ember/object'; +import { computed } from '@ember/object'; import { alias, notEmpty } from '@ember/object/computed'; -import { isEmpty, isPresent } from '@ember/utils'; +import { isEmpty } from '@ember/utils'; +import groupBy from 'kredits-web/utils/group-by'; import formatKredits from 'kredits-web/utils/format-kredits'; import config from 'kredits-web/config/environment'; @@ -15,27 +15,6 @@ import Contributor from 'kredits-web/models/contributor' import Proposal from 'kredits-web/models/proposal' import Contribution from 'kredits-web/models/contribution' -function groupBy (collection, property) { - let groups = A(); - let items = collection; - - if (items) { - items.forEach(function(item) { - let value = get(item, property); - let group = groups.findBy('value', value); - - if (isPresent(group)) { - get(group, 'items').push(item); - } else { - group = { property: property, value: value, items: [item] }; - groups.push(group); - } - }); - } - - return groups; -} - export default Service.extend({ currentBlock: null, diff --git a/app/utils/group-by.js b/app/utils/group-by.js new file mode 100644 index 0000000..3b08a95 --- /dev/null +++ b/app/utils/group-by.js @@ -0,0 +1,27 @@ +// +// Code from https://github.com/HeroicEric/ember-group-by (MIT licensed) +// +import { A } from '@ember/array'; +import { get } from '@ember/object'; +import { isPresent } from '@ember/utils'; + +export default function groupBy (collection, property) { + let groups = A(); + let items = collection; + + if (items) { + items.forEach(function(item) { + let value = get(item, property); + let group = groups.findBy('value', value); + + if (isPresent(group)) { + get(group, 'items').push(item); + } else { + group = { property: property, value: value, items: [item] }; + groups.push(group); + } + }); + } + + return groups; +} diff --git a/tests/unit/utils/group-by-test.js b/tests/unit/utils/group-by-test.js new file mode 100644 index 0000000..9ab5b0e --- /dev/null +++ b/tests/unit/utils/group-by-test.js @@ -0,0 +1,41 @@ +// +// Code adapted from https://github.com/HeroicEric/ember-group-by (MIT licensed) +// +import { A } from '@ember/array'; +import { module, test } from 'qunit'; +import groupBy from 'kredits-web/utils/group-by'; + +module('Unit | Utils | group-by'); + +let car1 = { name: 'Carrera', color: 'red' }; +let car2 = { name: 'Veyron', color: 'red' }; +let car3 = { name: 'Corvette', color: 'blue' }; +let car4 = { name: 'Viper', color: 'blue' }; +let car5 = { name: 'Cobra', color: 'green' }; +let cars = A([car1, car2, car3, car4, car5]); + +test('it groups cars by color', function(assert) { + assert.expect(1); + let redGroup = { property: 'color', value: 'red', items: [car1, car2] }; + let blueGroup = { property: 'color', value: 'blue', items: [car3, car4] }; + let greenGroup = { property: 'color', value: 'green', items: [car5] }; + + let result = groupBy(cars, 'color'); + let expected = [redGroup, blueGroup, greenGroup]; + + assert.deepEqual(result, expected); +}); + +test('it does not fail with empty array', function(assert) { + let cars = []; + + let result = groupBy(cars, 'color'); + assert.deepEqual(result, []); +}); + +test('it does not failkwith null', function(assert) { + let cars = null; + + let result = groupBy(cars, 'color'); + assert.deepEqual(result, []); +});