ts-prime
Version:
A utility library for JavaScript and Typescript.
91 lines (90 loc) • 2.53 kB
JavaScript
import { groupBy } from './groupBy';
import { pipe } from './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(array, function (x) { return x.a; })).toEqual(expected);
});
test('groupBy multi keys', function () {
expect(groupBy(employees, function (x) { return x.skills; })).toEqual(expectedEmployees);
});
test('groupBy.indexed', function () {
expect(groupBy.indexed(array, function (x) { return x.a; })).toEqual(expected);
});
test('groupBy.indexed multi keys', function () {
expect(groupBy.indexed(employees, function (x) { return x.skills; })).toEqual(expectedEmployees);
});
});
describe('data last', function () {
test('groupBy', function () {
expect(pipe(array, groupBy(function (x) { return x.a; }))).toEqual(expected);
});
test('groupBy multi keys', function () {
expect(pipe(employees, groupBy(function (x) { return x.skills; }))).toEqual(expectedEmployees);
});
test('groupBy.indexed', function () {
expect(pipe(array, groupBy.indexed(function (x) { return x.a; }))).toEqual(expected);
});
test('groupBy.indexed multi keys', function () {
expect(pipe(employees, groupBy.indexed(function (x) { return x.skills; }))).toEqual(expectedEmployees);
});
});