UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

93 lines (92 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var groupBy_1 = require("./groupBy"); var pipe_1 = require("./pipe"); var array = [ { a: 1, b: 1 }, { a: 1, b: 2 }, { a: 2, b: 1 }, { a: 1, b: 3 }, ]; var expected = { 1: [ { a: 1, b: 1 }, { a: 1, b: 2 }, { a: 1, b: 3 }, ], 2: [{ a: 2, b: 1 }], }; var employees = [ { name: 'John', skills: ['Printing', 'Painting', 'Writing'], }, { name: 'Britney', skills: ['Printing', 'Managing', 'Acting'], }, ]; var expectedEmployees = { Printing: [ { name: 'John', skills: ['Printing', 'Painting', 'Writing'], }, { name: 'Britney', skills: ['Printing', 'Managing', 'Acting'], }, ], Painting: [ { name: 'John', skills: ['Printing', 'Painting', 'Writing'], }, ], Managing: [ { name: 'Britney', skills: ['Printing', 'Managing', 'Acting'], }, ], Acting: [ { name: 'Britney', skills: ['Printing', 'Managing', 'Acting'], }, ], Writing: [ { name: 'John', skills: ['Printing', 'Painting', 'Writing'], }, ], }; describe('data first', function () { test('groupBy', function () { expect(groupBy_1.groupBy(array, function (x) { return x.a; })).toEqual(expected); }); test('groupBy multi keys', function () { expect(groupBy_1.groupBy(employees, function (x) { return x.skills; })).toEqual(expectedEmployees); }); test('groupBy.indexed', function () { expect(groupBy_1.groupBy.indexed(array, function (x) { return x.a; })).toEqual(expected); }); test('groupBy.indexed multi keys', function () { expect(groupBy_1.groupBy.indexed(employees, function (x) { return x.skills; })).toEqual(expectedEmployees); }); }); describe('data last', function () { test('groupBy', function () { expect(pipe_1.pipe(array, groupBy_1.groupBy(function (x) { return x.a; }))).toEqual(expected); }); test('groupBy multi keys', function () { expect(pipe_1.pipe(employees, groupBy_1.groupBy(function (x) { return x.skills; }))).toEqual(expectedEmployees); }); test('groupBy.indexed', function () { expect(pipe_1.pipe(array, groupBy_1.groupBy.indexed(function (x) { return x.a; }))).toEqual(expected); }); test('groupBy.indexed multi keys', function () { expect(pipe_1.pipe(employees, groupBy_1.groupBy.indexed(function (x) { return x.skills; }))).toEqual(expectedEmployees); }); });