@contrail/data-grouping
Advanced tools
Comparing version 1.0.11 to 1.0.12
import { DataGroup, DataGroupStructure, DataGroupingProperty } from "../interfaces"; | ||
export declare class DataGroupGenerator { | ||
static getDistinctValues(data: any, index: any): any[]; | ||
static buildChildDataGroups(data: any, parentGroup: DataGroup, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: any, currentDepth: any): void; | ||
@@ -4,0 +5,0 @@ static buildDataGroupStructure(data: Array<any>, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: number, currentDepth?: number): DataGroupStructure; |
@@ -7,10 +7,26 @@ "use strict"; | ||
class DataGroupGenerator { | ||
static getDistinctValues(data, index) { | ||
const map = new Map(); | ||
data.forEach(obj => { | ||
if (!obj) { | ||
return; | ||
} | ||
let key = obj[index]; | ||
let value = key; | ||
if (typeof key === 'object') { | ||
key = value.id || value.name || value; | ||
} | ||
map[key] = value; | ||
}); | ||
const distinctValues = [...(Object.values(map))].sort((v1, v2) => { | ||
let val1 = (typeof v1 === 'object') ? v1.name : v1; | ||
let val2 = (typeof v2 === 'object') ? v2.name : v2; | ||
return val1 > val2 ? 1 : -1; | ||
}); | ||
return distinctValues; | ||
} | ||
static buildChildDataGroups(data, parentGroup, groupingProperties, leafNodeDataCount, currentDepth) { | ||
const groupingProperty = groupingProperties[currentDepth]; | ||
const index = groupingProperty.scope + "." + groupingProperty.property.slug; | ||
const distinctValues = [...new Set(data.filter(obj => util_1.ObjectUtil.getByPath(obj, index)) | ||
.map(obj => util_1.ObjectUtil.getByPath(obj, index))) | ||
].sort((v1, v2) => { | ||
return v1 > v2 ? 1 : -1; | ||
}); | ||
const distinctValues = this.getDistinctValues(data, index); | ||
for (let val of distinctValues) { | ||
@@ -17,0 +33,0 @@ const groupData = data.filter(obj => util_1.ObjectUtil.getByPath(obj, index) === val); |
{ | ||
"name": "@contrail/data-grouping", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "Utilities and interfaces for grouping data into hierarchial data structures based on properties.", | ||
@@ -17,2 +17,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@contrail/util": "^1.0.26", | ||
"@types/jest": "^23.3.14", | ||
@@ -19,0 +20,0 @@ "jest": "^23.6.0", |
7935
152
8