@contrail/data-grouping 1.0.24 → 1.0.26

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,2 +1,2 @@
1
- Library for use with @contrail/documents that allows for the quick/automation generation of
2
- documents based on templates and layout options.
1
+ Library for use with @contrail/documents that allows for the quick/automation generation of
2
+ documents based on templates and layout options.
@@ -1,7 +1,7 @@
1
- import { DataGroup, DataGroupStructure, DataGroupingProperty } from "../interfaces";
2
- export declare class DataGroupGenerator {
3
- static getDistinctValues(data: any, index: any, altIndex?: any, groupMultiSelectInSeparateFrame?: boolean): any[];
4
- static buildChildDataGroups(data: any, parentGroup: DataGroup, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: any, currentDepth: any, groupMultiSelectInSeparateFrame?: boolean): void;
5
- static buildDataGroupStructure(data: Array<any>, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: number, groupMultiSelectInSeparateFrame?: boolean): DataGroupStructure;
6
- static createPartitionedGroupsFromData(parentGroup: DataGroup, data: Array<any>, leafNodeDataCount: number): Array<DataGroup>;
7
- }
1
+ import { DataGroup, DataGroupStructure, DataGroupingProperty } from '../interfaces';
2
+ export declare class DataGroupGenerator {
3
+ static getDistinctValues(data: any, index: any, altIndex?: any, groupMultiSelectInSeparateFrame?: boolean): any[];
4
+ static buildChildDataGroups(data: any, parentGroup: DataGroup, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: any, currentDepth: any, groupMultiSelectInSeparateFrame?: boolean): void;
5
+ static buildDataGroupStructure(data: Array<any>, groupingProperties: Array<DataGroupingProperty>, leafNodeDataCount: number, groupMultiSelectInSeparateFrame?: boolean): DataGroupStructure;
6
+ static createPartitionedGroupsFromData(parentGroup: DataGroup, data: Array<any>, leafNodeDataCount: number): Array<DataGroup>;
7
+ }
@@ -1,130 +1,130 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DataGroupGenerator = void 0;
4
- const util_1 = require("@contrail/util");
5
- const types_1 = require("@contrail/types");
6
- class DataGroupGenerator {
7
- static getDistinctValues(data, index, altIndex = null, groupMultiSelectInSeparateFrame = false) {
8
- const map = new Map();
9
- const sortingArray = [];
10
- data.forEach(obj => {
11
- if (!obj) {
12
- return;
13
- }
14
- let key = util_1.ObjectUtil.getByPath(obj, index);
15
- let altKey = null;
16
- if (altIndex) {
17
- altKey = util_1.ObjectUtil.getByPath(obj, altIndex);
18
- if (!key) {
19
- key = altKey;
20
- }
21
- }
22
- let value = key;
23
- if (!value) {
24
- return;
25
- }
26
- if (key && value && typeof key === 'object') {
27
- key = value.id || value.name || value;
28
- }
29
- if (Array.isArray(value) && !groupMultiSelectInSeparateFrame) {
30
- value.forEach(arrayValue => {
31
- map[arrayValue] = arrayValue;
32
- if (!sortingArray.includes(arrayValue)) {
33
- sortingArray.push(arrayValue);
34
- }
35
- });
36
- }
37
- else {
38
- map[key] = value;
39
- if (!sortingArray.includes(value)) {
40
- sortingArray.push(value);
41
- }
42
- }
43
- });
44
- const distinctValues = [...(Object.values(map))].sort((v1, v2) => {
45
- let val1 = (v1 && typeof v1 === 'object') ? v1.name : v1;
46
- let val2 = (v2 && typeof v2 === 'object') ? v2.name : v2;
47
- return sortingArray.indexOf(val1) - sortingArray.indexOf(val2);
48
- });
49
- return distinctValues;
50
- }
51
- static buildChildDataGroups(data, parentGroup, groupingProperties, leafNodeDataCount, currentDepth, groupMultiSelectInSeparateFrame = false) {
52
- const groupingProperty = groupingProperties[currentDepth];
53
- const index = groupingProperty.typeRootSlug + "." + groupingProperty.propertyDefinition.slug;
54
- const altIndex = util_1.StringUtil.convertToCamelCase(groupingProperty.typeRootSlug) + "." + groupingProperty.propertyDefinition.slug;
55
- let distinctValues = this.getDistinctValues(data, index, altIndex, groupMultiSelectInSeparateFrame);
56
- for (let val of distinctValues) {
57
- const groupData = data.filter(obj => {
58
- const objVal = util_1.ObjectUtil.getByPath(obj, index) || util_1.ObjectUtil.getByPath(obj, altIndex);
59
- if (Array.isArray(val) && Array.isArray(objVal)) {
60
- return val.sort().join() === objVal.sort().join();
61
- }
62
- else if (!Array.isArray(val) && Array.isArray(objVal)) {
63
- return objVal.includes(val);
64
- }
65
- else if (val.id && (objVal === null || objVal === void 0 ? void 0 : objVal.id)) {
66
- return val.id === objVal.id;
67
- }
68
- else {
69
- return (objVal === val);
70
- }
71
- });
72
- const group = {
73
- data: [],
74
- subGroups: [],
75
- propertyValues: {},
76
- name: ''
77
- };
78
- if (groupingProperty.isSecondaryGroup) {
79
- group.isSecondaryGroup = true;
80
- }
81
- if (groupData.length > 0) {
82
- const details = groupData[0];
83
- group.propertyValues = Object.assign({}, details);
84
- }
85
- group.propertyValues[groupingProperty.propertyDefinition.slug] = val;
86
- const label = new types_1.PropertyValueFormatter().formatValueForProperty(val, groupingProperty.propertyDefinition);
87
- group.name = label;
88
- if (currentDepth === groupingProperties.length - 1) {
89
- group.subGroups = this.createPartitionedGroupsFromData(group, groupData, leafNodeDataCount);
90
- }
91
- else if (currentDepth < groupingProperties.length - 1) {
92
- this.buildChildDataGroups(groupData, group, groupingProperties, leafNodeDataCount, currentDepth + 1, groupMultiSelectInSeparateFrame);
93
- }
94
- parentGroup.subGroups.push(group);
95
- }
96
- }
97
- static buildDataGroupStructure(data, groupingProperties, leafNodeDataCount, groupMultiSelectInSeparateFrame = false) {
98
- const structure = {
99
- rootGroup: {
100
- subGroups: [],
101
- name: "root",
102
- propertyValues: {},
103
- data: [],
104
- },
105
- groupingProperties,
106
- depth: groupingProperties ? groupingProperties.length : 0,
107
- };
108
- this.buildChildDataGroups(data, structure.rootGroup, groupingProperties, leafNodeDataCount, 0, groupMultiSelectInSeparateFrame);
109
- return structure;
110
- }
111
- static createPartitionedGroupsFromData(parentGroup, data, leafNodeDataCount) {
112
- const dataLength = data.length;
113
- const frameCount = Math.ceil(dataLength / leafNodeDataCount);
114
- const groups = [];
115
- for (let i = 1; i <= frameCount; i++) {
116
- const startIndex = (i - 1) * leafNodeDataCount;
117
- const endIndex = startIndex + leafNodeDataCount;
118
- let groupData = data.slice(startIndex, endIndex);
119
- const group = {
120
- data: groupData,
121
- subGroups: [],
122
- name: parentGroup.name,
123
- propertyValues: parentGroup.propertyValues,
124
- };
125
- groups.push(group);
126
- }
127
- return groups;
128
- }
129
- }
130
- exports.DataGroupGenerator = DataGroupGenerator;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataGroupGenerator = void 0;
4
+ const util_1 = require("@contrail/util");
5
+ const types_1 = require("@contrail/types");
6
+ class DataGroupGenerator {
7
+ static getDistinctValues(data, index, altIndex = null, groupMultiSelectInSeparateFrame = false) {
8
+ const map = new Map();
9
+ const sortingArray = [];
10
+ data.forEach(obj => {
11
+ if (!obj) {
12
+ return;
13
+ }
14
+ let key = util_1.ObjectUtil.getByPath(obj, index);
15
+ let altKey = null;
16
+ if (altIndex) {
17
+ altKey = util_1.ObjectUtil.getByPath(obj, altIndex);
18
+ if (!key) {
19
+ key = altKey;
20
+ }
21
+ }
22
+ let value = key;
23
+ if (!value) {
24
+ return;
25
+ }
26
+ if (key && value && typeof key === 'object') {
27
+ key = value.id || value.name || value;
28
+ }
29
+ if (Array.isArray(value) && !groupMultiSelectInSeparateFrame) {
30
+ value.forEach(arrayValue => {
31
+ map[arrayValue] = arrayValue;
32
+ if (!sortingArray.includes(arrayValue)) {
33
+ sortingArray.push(arrayValue);
34
+ }
35
+ });
36
+ }
37
+ else {
38
+ map[key] = value;
39
+ if (!sortingArray.includes(value)) {
40
+ sortingArray.push(value);
41
+ }
42
+ }
43
+ });
44
+ const distinctValues = [...Object.values(map)].sort((v1, v2) => {
45
+ let val1 = v1 && typeof v1 === 'object' ? v1.name : v1;
46
+ let val2 = v2 && typeof v2 === 'object' ? v2.name : v2;
47
+ return sortingArray.indexOf(val1) - sortingArray.indexOf(val2);
48
+ });
49
+ return distinctValues;
50
+ }
51
+ static buildChildDataGroups(data, parentGroup, groupingProperties, leafNodeDataCount, currentDepth, groupMultiSelectInSeparateFrame = false) {
52
+ const groupingProperty = groupingProperties[currentDepth];
53
+ const index = groupingProperty.typeRootSlug + '.' + groupingProperty.propertyDefinition.slug;
54
+ const altIndex = util_1.StringUtil.convertToCamelCase(groupingProperty.typeRootSlug) + '.' + groupingProperty.propertyDefinition.slug;
55
+ let distinctValues = this.getDistinctValues(data, index, altIndex, groupMultiSelectInSeparateFrame);
56
+ for (let val of distinctValues) {
57
+ const groupData = data.filter(obj => {
58
+ const objVal = util_1.ObjectUtil.getByPath(obj, index) || util_1.ObjectUtil.getByPath(obj, altIndex);
59
+ if (Array.isArray(val) && Array.isArray(objVal)) {
60
+ return val.sort().join() === objVal.sort().join();
61
+ }
62
+ else if (!Array.isArray(val) && Array.isArray(objVal)) {
63
+ return objVal.includes(val);
64
+ }
65
+ else if (val.id && (objVal === null || objVal === void 0 ? void 0 : objVal.id)) {
66
+ return val.id === objVal.id;
67
+ }
68
+ else {
69
+ return objVal === val;
70
+ }
71
+ });
72
+ const group = {
73
+ data: [],
74
+ subGroups: [],
75
+ propertyValues: {},
76
+ name: '',
77
+ };
78
+ if (groupingProperty.isSecondaryGroup) {
79
+ group.isSecondaryGroup = true;
80
+ }
81
+ if (groupData.length > 0) {
82
+ const details = groupData[0];
83
+ group.propertyValues = Object.assign({}, details);
84
+ }
85
+ group.propertyValues[groupingProperty.propertyDefinition.slug] = val;
86
+ const label = new types_1.PropertyValueFormatter().formatValueForProperty(val, groupingProperty.propertyDefinition);
87
+ group.name = label;
88
+ if (currentDepth === groupingProperties.length - 1) {
89
+ group.subGroups = this.createPartitionedGroupsFromData(group, groupData, leafNodeDataCount);
90
+ }
91
+ else if (currentDepth < groupingProperties.length - 1) {
92
+ this.buildChildDataGroups(groupData, group, groupingProperties, leafNodeDataCount, currentDepth + 1, groupMultiSelectInSeparateFrame);
93
+ }
94
+ parentGroup.subGroups.push(group);
95
+ }
96
+ }
97
+ static buildDataGroupStructure(data, groupingProperties, leafNodeDataCount, groupMultiSelectInSeparateFrame = false) {
98
+ const structure = {
99
+ rootGroup: {
100
+ subGroups: [],
101
+ name: 'root',
102
+ propertyValues: {},
103
+ data: [],
104
+ },
105
+ groupingProperties,
106
+ depth: groupingProperties ? groupingProperties.length : 0,
107
+ };
108
+ this.buildChildDataGroups(data, structure.rootGroup, groupingProperties, leafNodeDataCount, 0, groupMultiSelectInSeparateFrame);
109
+ return structure;
110
+ }
111
+ static createPartitionedGroupsFromData(parentGroup, data, leafNodeDataCount) {
112
+ const dataLength = data.length;
113
+ const frameCount = Math.ceil(dataLength / leafNodeDataCount);
114
+ const groups = [];
115
+ for (let i = 1; i <= frameCount; i++) {
116
+ const startIndex = (i - 1) * leafNodeDataCount;
117
+ const endIndex = startIndex + leafNodeDataCount;
118
+ let groupData = data.slice(startIndex, endIndex);
119
+ const group = {
120
+ data: groupData,
121
+ subGroups: [],
122
+ name: parentGroup.name,
123
+ propertyValues: parentGroup.propertyValues,
124
+ };
125
+ groups.push(group);
126
+ }
127
+ return groups;
128
+ }
129
+ }
130
+ exports.DataGroupGenerator = DataGroupGenerator;
@@ -1 +1 @@
1
- export * from './data-group-generator';
1
+ export * from './data-group-generator';
@@ -1,17 +1,17 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./data-group-generator"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./data-group-generator"), exports);
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './interfaces';
2
- export * from './data-group-generator';
1
+ export * from './interfaces';
2
+ export * from './data-group-generator';
package/lib/index.js CHANGED
@@ -1,19 +1,18 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./interfaces"), exports);
18
- ;
19
- __exportStar(require("./data-group-generator"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./interfaces"), exports);
18
+ __exportStar(require("./data-group-generator"), exports);
@@ -1,31 +1,31 @@
1
- import { TypeProperty } from "@contrail/types";
2
- export interface DataGroup {
3
- subGroups: Array<DataGroup>;
4
- name: string;
5
- propertyValues: {
6
- [key: string]: any;
7
- };
8
- aggregateValues?: {
9
- [key: string]: any;
10
- };
11
- data: Array<any>;
12
- isSecondaryGroup?: boolean;
13
- }
14
- export interface DataGroupStructure {
15
- rootGroup: DataGroup;
16
- depth: number;
17
- groupingProperties: Array<DataGroupingProperty>;
18
- aggregationProperties?: Array<DataGroupingProperty>;
19
- sourceDataDefinition?: {
20
- sourceDataReference: string;
21
- filterDefinition?: any;
22
- sortDefinition?: any;
23
- };
24
- }
25
- export interface DataGroupingProperty {
26
- propertyDefinition: TypeProperty;
27
- values?: Array<string>;
28
- typeRootSlug: string;
29
- sort: string;
30
- isSecondaryGroup?: boolean;
31
- }
1
+ import { TypeProperty } from '@contrail/types';
2
+ export interface DataGroup {
3
+ subGroups: Array<DataGroup>;
4
+ name: string;
5
+ propertyValues: {
6
+ [key: string]: any;
7
+ };
8
+ aggregateValues?: {
9
+ [key: string]: any;
10
+ };
11
+ data: Array<any>;
12
+ isSecondaryGroup?: boolean;
13
+ }
14
+ export interface DataGroupStructure {
15
+ rootGroup: DataGroup;
16
+ depth: number;
17
+ groupingProperties: Array<DataGroupingProperty>;
18
+ aggregationProperties?: Array<DataGroupingProperty>;
19
+ sourceDataDefinition?: {
20
+ sourceDataReference: string;
21
+ filterDefinition?: any;
22
+ sortDefinition?: any;
23
+ };
24
+ }
25
+ export interface DataGroupingProperty {
26
+ propertyDefinition: TypeProperty;
27
+ values?: Array<string>;
28
+ typeRootSlug: string;
29
+ sort: string;
30
+ isSecondaryGroup?: boolean;
31
+ }
package/lib/interfaces.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,44 +1,44 @@
1
- {
2
- "name": "@contrail/data-grouping",
3
- "version": "1.0.24",
4
- "description": "Utilities and interfaces for grouping data into hierarchial data structures based on properties.",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "scripts": {
8
- "build": "tsc",
9
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
- "lint": "tslint -p tsconfig.json",
11
- "test": "jest"
12
- },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
- "devDependencies": {
17
- "@contrail/util": "^1.0.34",
18
- "@types/jest": "^29.5.2",
19
- "jest": "^29.5.0",
20
- "prettier": "^1.19.1",
21
- "ts-jest": "^29.1.1",
22
- "tslint": "^5.11.0",
23
- "tslint-config-prettier": "^1.18.0",
24
- "typescript": "^4.0.0"
25
- },
26
- "jest": {
27
- "moduleFileExtensions": [
28
- "js",
29
- "json",
30
- "ts"
31
- ],
32
- "rootDir": "src",
33
- "testRegex": ".spec.ts$",
34
- "transform": {
35
- "^.+\\.(t|j)s$": "ts-jest"
36
- },
37
- "coverageDirectory": "../coverage",
38
- "testEnvironment": "node"
39
- },
40
- "dependencies": {
41
- "@contrail/documents": "^1.0.38",
42
- "@contrail/types": "^3.0.27"
43
- }
44
- }
1
+ {
2
+ "name": "@contrail/data-grouping",
3
+ "version": "1.0.26",
4
+ "description": "Utilities and interfaces for grouping data into hierarchial data structures based on properties.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
+ "lint": "tslint -p tsconfig.json",
11
+ "test": "jest"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@contrail/util": "^1.0.63",
18
+ "@types/jest": "^29.5.2",
19
+ "jest": "^29.5.0",
20
+ "prettier": "^1.19.1",
21
+ "ts-jest": "^29.1.1",
22
+ "tslint": "^5.11.0",
23
+ "tslint-config-prettier": "^1.18.0",
24
+ "typescript": "^4.0.0"
25
+ },
26
+ "jest": {
27
+ "moduleFileExtensions": [
28
+ "js",
29
+ "json",
30
+ "ts"
31
+ ],
32
+ "rootDir": "src",
33
+ "testRegex": ".spec.ts$",
34
+ "transform": {
35
+ "^.+\\.(t|j)s$": "ts-jest"
36
+ },
37
+ "coverageDirectory": "../coverage",
38
+ "testEnvironment": "node"
39
+ },
40
+ "dependencies": {
41
+ "@contrail/documents": "^1.0.38",
42
+ "@contrail/types": "^3.0.27"
43
+ }
44
+ }