@contrail/data-grouping 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { DataGroup, DataGroupStructure, DataGroupingProperty } from "../interfaces";
2
2
  export declare class DataGroupGenerator {
3
- static buildChildDataGroups(data: any, parentGroup: DataGroup, groupingProperties: any, leafNodeDataCount: any, currentDepth: any): void;
3
+ static getDistinctValues(data: any, index: any): any[];
4
+ static buildChildDataGroups(data: any, parentGroup: DataGroup, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: any, currentDepth: any): void;
4
5
  static buildDataGroupStructure(data: Array<any>, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: number, currentDepth?: number): DataGroupStructure;
5
6
  static createPartitionedGroupsFromData(parentGroup: DataGroup, data: Array<any>, leafNodeDataCount: number): Array<DataGroup>;
6
7
  }
@@ -4,14 +4,30 @@ exports.DataGroupGenerator = void 0;
4
4
  const util_1 = require("@contrail/util");
5
5
  const types_1 = require("@contrail/types");
6
6
  class DataGroupGenerator {
7
+ static getDistinctValues(data, index) {
8
+ const map = new Map();
9
+ data.forEach(obj => {
10
+ if (!obj) {
11
+ return;
12
+ }
13
+ let key = obj[index];
14
+ let value = key;
15
+ if (typeof key === 'object') {
16
+ key = value.id || value.name || value;
17
+ }
18
+ map[key] = value;
19
+ });
20
+ const distinctValues = [...(Object.values(map))].sort((v1, v2) => {
21
+ let val1 = (typeof v1 === 'object') ? v1.name : v1;
22
+ let val2 = (typeof v2 === 'object') ? v2.name : v2;
23
+ return val1 > val2 ? 1 : -1;
24
+ });
25
+ return distinctValues;
26
+ }
7
27
  static buildChildDataGroups(data, parentGroup, groupingProperties, leafNodeDataCount, currentDepth) {
8
28
  const groupingProperty = groupingProperties[currentDepth];
9
29
  const index = groupingProperty.scope + "." + groupingProperty.property.slug;
10
- const distinctValues = [...new Set(data.filter(obj => util_1.ObjectUtil.getByPath(obj, index))
11
- .map(obj => util_1.ObjectUtil.getByPath(obj, index)))
12
- ].sort((v1, v2) => {
13
- return v1 > v2 ? 1 : -1;
14
- });
30
+ const distinctValues = this.getDistinctValues(data, index);
15
31
  for (let val of distinctValues) {
16
32
  const groupData = data.filter(obj => util_1.ObjectUtil.getByPath(obj, index) === val);
17
33
  const group = {
@@ -21,7 +37,7 @@ class DataGroupGenerator {
21
37
  name: ''
22
38
  };
23
39
  group.properties[groupingProperty.property.slug] = val;
24
- const label = new types_1.PropertyValueFormatter().formatValueForProperty(val, groupingProperty);
40
+ const label = new types_1.PropertyValueFormatter().formatValueForProperty(val, groupingProperty.property);
25
41
  group.name = label;
26
42
  if (currentDepth === groupingProperties.length - 1) {
27
43
  group.subGroups = this.createPartitionedGroupsFromData(group, groupData, leafNodeDataCount);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/data-grouping",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Utilities and interfaces for grouping data into hierarchial data structures based on properties.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -14,6 +14,7 @@
14
14
  "author": "",
15
15
  "license": "ISC",
16
16
  "devDependencies": {
17
+ "@contrail/util": "^1.0.26",
17
18
  "@types/jest": "^23.3.14",
18
19
  "jest": "^23.6.0",
19
20
  "prettier": "^1.19.1",