@contrail/data-grouping 1.0.27 → 1.0.29

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,6 @@
1
1
  import { DataGroup, DataGroupStructure, DataGroupingProperty } from '../interfaces';
2
2
  export declare class DataGroupGenerator {
3
- static getDistinctValues(data: any, rootIndex: any, slugIndex?: any, rootAltIndex?: any, slugAltIndex?: any, groupMultiSelectInSeparateFrame?: boolean): any[];
3
+ static getDistinctValues(data: any, rootIndex: any, slugIndex?: any, rootAltIndex?: any, slugAltIndex?: any, groupMultiSelectInSeparateFrame?: boolean, sortKey?: string, options?: any): any[];
4
4
  static buildChildDataGroups(data: any, parentGroup: DataGroup, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: any, currentDepth: any, groupMultiSelectInSeparateFrame?: boolean): void;
5
5
  static buildDataGroupStructure(data: Array<any>, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: number, groupMultiSelectInSeparateFrame?: boolean): DataGroupStructure;
6
6
  static createPartitionedGroupsFromData(parentGroup: DataGroup, data: Array<any>, leafNodeDataCount: number): Array<DataGroup>;
@@ -4,10 +4,10 @@ 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, rootIndex, slugIndex = null, rootAltIndex = null, slugAltIndex = null, groupMultiSelectInSeparateFrame = false) {
7
+ static getDistinctValues(data, rootIndex, slugIndex = null, rootAltIndex = null, slugAltIndex = null, groupMultiSelectInSeparateFrame = false, sortKey = 'asc', options = null) {
8
8
  const map = new Map();
9
9
  const sortingArray = [];
10
- data.forEach(obj => {
10
+ data.forEach((obj) => {
11
11
  if (!obj) {
12
12
  return;
13
13
  }
@@ -27,7 +27,7 @@ class DataGroupGenerator {
27
27
  key = value.id || value.name || value;
28
28
  }
29
29
  if (Array.isArray(value) && !groupMultiSelectInSeparateFrame) {
30
- value.forEach(arrayValue => {
30
+ value.forEach((arrayValue) => {
31
31
  map[arrayValue] = arrayValue;
32
32
  if (!sortingArray.includes(arrayValue)) {
33
33
  sortingArray.push(arrayValue);
@@ -41,22 +41,46 @@ class DataGroupGenerator {
41
41
  }
42
42
  }
43
43
  });
44
- const distinctValues = [...Object.values(map)].sort((v1, v2) => {
44
+ const distinctValues = [...Object.values(map)];
45
+ distinctValues.sort((v1, v2) => {
45
46
  let val1 = v1 && typeof v1 === 'object' ? v1.name : v1;
46
47
  let val2 = v2 && typeof v2 === 'object' ? v2.name : v2;
47
- return sortingArray.indexOf(val1) - sortingArray.indexOf(val2);
48
+ console.log('OPTIONS', options);
49
+ if (sortKey === 'asc') {
50
+ if (options) {
51
+ return options.indexOf(val1) - options.indexOf(val2);
52
+ }
53
+ else {
54
+ return val1 > val2 ? 1 : -1;
55
+ }
56
+ }
57
+ else if (sortKey === 'desc') {
58
+ if (options) {
59
+ return options.indexOf(val2) - options.indexOf(val1);
60
+ }
61
+ else {
62
+ return val1 < val2 ? 1 : -1;
63
+ }
64
+ }
65
+ else {
66
+ return sortingArray.indexOf(val1) - sortingArray.indexOf(val2);
67
+ }
48
68
  });
49
69
  return distinctValues;
50
70
  }
51
71
  static buildChildDataGroups(data, parentGroup, groupingProperties, leafNodeDataCount, currentDepth, groupMultiSelectInSeparateFrame = false) {
72
+ var _a;
52
73
  const groupingProperty = groupingProperties[currentDepth];
53
74
  const rootIndex = groupingProperty.typeRootSlug;
54
75
  const slugIndex = groupingProperty.propertyDefinition.slug;
55
76
  const rootAltIndex = util_1.StringUtil.convertToCamelCase(groupingProperty.typeRootSlug);
56
77
  const slugAltIndex = groupingProperty.propertyDefinition.slug;
57
- let distinctValues = this.getDistinctValues(data, rootIndex, slugIndex, rootAltIndex, slugAltIndex, groupMultiSelectInSeparateFrame);
78
+ const sort = groupingProperty.sort || 'asc';
79
+ const options = ((_a = groupingProperty.propertyDefinition) === null || _a === void 0 ? void 0 : _a.options) || null;
80
+ console.log('GROUPING PROPERTIES', groupingProperty);
81
+ let distinctValues = this.getDistinctValues(data, rootIndex, slugIndex, rootAltIndex, slugAltIndex, groupMultiSelectInSeparateFrame, sort, options);
58
82
  for (let val of distinctValues) {
59
- const groupData = data.filter(obj => {
83
+ const groupData = data.filter((obj) => {
60
84
  const objVal = util_1.ObjectUtil.getBySlugs(obj, rootIndex, slugIndex) || util_1.ObjectUtil.getBySlugs(obj, rootAltIndex, slugAltIndex);
61
85
  if (Array.isArray(val) && Array.isArray(objVal)) {
62
86
  return val.sort().join() === objVal.sort().join();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/data-grouping",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
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",