| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var inspect = require('object-inspect');
|
|---|
| 4 | var forEach = require('for-each');
|
|---|
| 5 | var v = require('es-value-fixtures');
|
|---|
| 6 |
|
|---|
| 7 | module.exports = function (groupBy, t) {
|
|---|
| 8 | t.test('callback function', function (st) {
|
|---|
| 9 | forEach(v.nonFunctions, function (nonFunction) {
|
|---|
| 10 | st['throws'](
|
|---|
| 11 | function () { groupBy([], nonFunction); },
|
|---|
| 12 | TypeError,
|
|---|
| 13 | inspect(nonFunction) + ' is not a function'
|
|---|
| 14 | );
|
|---|
| 15 | });
|
|---|
| 16 |
|
|---|
| 17 | st.end();
|
|---|
| 18 | });
|
|---|
| 19 |
|
|---|
| 20 | t.test('grouping', function (st) {
|
|---|
| 21 | st.deepEqual(
|
|---|
| 22 | groupBy([], function () { return 'a'; }),
|
|---|
| 23 | { __proto__: null },
|
|---|
| 24 | 'an empty array produces an empty object'
|
|---|
| 25 | );
|
|---|
| 26 |
|
|---|
| 27 | var arr = [0, -0, 1, 2, 3, 4, 5, NaN, Infinity, -Infinity];
|
|---|
| 28 | var parity = function (x) {
|
|---|
| 29 | if (x !== x) {
|
|---|
| 30 | return void undefined;
|
|---|
| 31 | }
|
|---|
| 32 | if (!isFinite(x)) {
|
|---|
| 33 | return '∞';
|
|---|
| 34 | }
|
|---|
| 35 | return x % 2 === 0 ? 'even' : 'odd';
|
|---|
| 36 | };
|
|---|
| 37 | var grouped = {
|
|---|
| 38 | __proto__: null,
|
|---|
| 39 | even: [0, -0, 2, 4],
|
|---|
| 40 | odd: [1, 3, 5],
|
|---|
| 41 | undefined: [NaN],
|
|---|
| 42 | '∞': [Infinity, -Infinity]
|
|---|
| 43 | };
|
|---|
| 44 | st.deepEqual(
|
|---|
| 45 | groupBy(arr, parity),
|
|---|
| 46 | grouped,
|
|---|
| 47 | inspect(arr) + ' group by parity groups to ' + inspect(grouped)
|
|---|
| 48 | );
|
|---|
| 49 |
|
|---|
| 50 | st.deepEqual(
|
|---|
| 51 | groupBy(arr, function (x, i) {
|
|---|
| 52 | st.equal(this, undefined, 'receiver is as expected'); // eslint-disable-line no-invalid-this
|
|---|
| 53 | st.equal(x, arr[i], 'second argument ' + i + ' is ' + inspect(arr[i]));
|
|---|
| 54 | return 42;
|
|---|
| 55 | }),
|
|---|
| 56 | { __proto__: null, 42: arr },
|
|---|
| 57 | 'thisArg and callback arguments are as expected'
|
|---|
| 58 | );
|
|---|
| 59 |
|
|---|
| 60 | st.end();
|
|---|
| 61 | });
|
|---|
| 62 | };
|
|---|