Skip to content

Commit

Permalink
☔ 🔥 #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Wilhite committed Jan 8, 2020
1 parent 90e58dc commit 1decab6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export { Fragment } from './models/fragment';
export { FunctionDisplayModel } from './models/function-display.model';
export { MenuDisplayItemModel } from './models/menu-display-item.model';
export { Presentation } from './models/presentation';
export { ReducedGroup } from './models/reduced-group';
export { Segment } from './models/segment';
export { Selectable } from './models/selectable';
export { Sortable } from './models/sortable';
Expand Down
18 changes: 18 additions & 0 deletions src/models/reduced-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* defines a group generated
* from an Array-reduce operation
*
* @export
*/
export interface ReducedGroup {
/**
* the grouping key
*
*/
key: any;
/**
* the grouping values
*
*/
values: any[];
}
66 changes: 25 additions & 41 deletions src/utilities/array.utility.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ReducedGroup } from 'src/models/reduced-group';

/**
* static members for @type {Array}
*
Expand Down Expand Up @@ -27,53 +29,35 @@ export class ArrayUtility {
}

/**
* groups an object with @type {string} keys
* reduces the specified reducible in @type {ReducedGroup} groups
*
* @description https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_groupby
* @description https://stackoverflow.com/a/48981669/22944
* @description https://github.com/BryanWilhite/songhay-core/issues/20
*/
public static groupBy<T extends { [key: string]: any }>(
data: T[],
keyGetter: (datum: T) => string): { [key: string]: T[] } {
if (!data) { return {}; }

const initialValue = {} as T;
const grouped = data.reduce(
// tslint:disable-next-line:ban-types
public static groupBy(reducible: any[], keyGetter: Function): ReducedGroup[] {
const initialValue = {};
const groupByObjects = reducible.reduce(
(
accumulator: T,
current: T,
index: number,
dataReference: T[],
key = keyGetter(current)
) => ((accumulator[key] || (accumulator[key] = [])).push(current), accumulator),
initialValue
accumulator: any,
current: any,
i: number,
dataRef: any,
k: any = keyGetter(current)
) => ((accumulator[k] || (accumulator[k] = [])).push(current), accumulator), initialValue
);
return grouped;
}
const groupByModels: ReducedGroup[] = [];

/**
* groups an object with @type {string} keys
* into numeric-keyed groups
*
* @description https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_groupby
*/
public static groupByNumeric<T extends { [key: string]: any }>(
data: T[],
keyGetter: (datum: T) => number): { [key: number]: T[] } {
if (!data) { return {}; }
for (const p in groupByObjects) {
if (!groupByObjects.hasOwnProperty(p)) {
continue;
}
groupByModels.push({
key: p,
values: groupByObjects[p]
});
}

const initialValue = {} as T;
const grouped = data.reduce(
(
accumulator: T,
current: T,
index: number,
dataReference: T[],
key = keyGetter(current)
) => ((accumulator[key] || (accumulator[key] = [])).push(current), accumulator),
initialValue
);
return grouped;
return groupByModels;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/utilities/display-item.utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MenuDisplayItemModel } from '../models/menu-display-item.model';

import { ArrayUtility } from './array.utility';
import { MapObjectUtility } from './map-object.utility';
import { ReducedGroupUtility } from './reduced-group-utility';

/**
* the default fallback display item grouping pair
Expand Down Expand Up @@ -112,8 +113,10 @@ export class DisplayItemUtility {

DisplayItemUtility.setGrouping(items, groupId);

const grouped = ArrayUtility.groupBy(items, i => i.groupId as string);
return grouped;
const grouped = ArrayUtility.groupBy(items, (i: MenuDisplayItemModel) => i.groupId);
const reduced = ReducedGroupUtility.reduceToObject<MenuDisplayItemModel>(grouped);

return reduced;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/utilities/reduced-group-utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ReducedGroup } from 'src/models/reduced-group';

/**
* static routines for display-item models
*
* @export
*/
export class ReducedGroupUtility {
/**
* reduces @type {ReducedGroup[]} to @type {{ [key: any]: T[] }}
*
*/
public static reduceToObject<T>(groups: ReducedGroup[]): { [key: string]: T[] } {
const keyGetter = (datum: ReducedGroup) => datum.key;
const initialValue: { [key: string]: T[] } = {};
const reduction = groups.reduce(
(
accumulator: { [key: string]: T[] },
current: ReducedGroup,
i: number,
dataRef: any,
k: any = keyGetter(current)
) => ({...current, ...accumulator}),
initialValue
);

return reduction;
}
}

0 comments on commit 1decab6

Please sign in to comment.