@qrvey/utils 1.16.1-5 → 1.16.1-7

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.
@@ -72,7 +72,7 @@ export interface VisualizationTableMenu {
72
72
  filtering: boolean;
73
73
  column_resizing: boolean;
74
74
  column_arrangement: boolean;
75
- grouping_column_update: boolean;
75
+ columnGrouping: boolean;
76
76
  column_update: boolean;
77
77
  change_aggregation: boolean;
78
78
  }
@@ -72,7 +72,7 @@ export interface VisualizationTableMenu {
72
72
  filtering: boolean;
73
73
  column_resizing: boolean;
74
74
  column_arrangement: boolean;
75
- grouping_column_update: boolean;
75
+ columnGrouping: boolean;
76
76
  column_update: boolean;
77
77
  change_aggregation: boolean;
78
78
  }
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAggFilters = void 0;
4
- const COLUMN_1 = require("../../columns/constants/COLUMN");
5
4
  const isEmpty_1 = require("../../general/mix/isEmpty");
6
- const objectCopy_1 = require("../../general/object/objectCopy");
7
5
  const get_1 = require("../../general/object/get");
8
- const FILTER_OPERATOR_1 = require("../constants/common/FILTER_OPERATOR");
6
+ const objectCopy_1 = require("../../general/object/objectCopy");
7
+ const traverseTree_1 = require("../../general/object/traverseTree");
8
+ const adapters_1 = require("../adapters");
9
9
  /**
10
10
  * @deprecated soon
11
11
  * @param logics
@@ -14,33 +14,26 @@ const FILTER_OPERATOR_1 = require("../constants/common/FILTER_OPERATOR");
14
14
  */
15
15
  function getAggFilters(logics = [], summaries = []) {
16
16
  const aggregateFilters = getAggregateFilters(logics);
17
- const aggFilters = aggregateFilters
18
- .map((aggFilter) => {
19
- const expressions = getExpressionsInLogic([aggFilter]);
20
- return (expressions &&
21
- expressions.length && {
22
- operator: FILTER_OPERATOR_1.FILTER_OPERATOR.OR,
23
- expressions: expressions
24
- .map((expression) => {
25
- const summaryIndex = summaries.findIndex((summary) => expression.questionid === summary.questionid &&
26
- expression.qrveyid === summary.qrveyid &&
27
- (0, get_1._get)(expression.uiExtras.column, "aggregate") ===
28
- summary.aggregate);
29
- if (summaryIndex > -1 ||
30
- expression.questionType === COLUMN_1.COLUMN.AGGREGATED_FORMULA) {
31
- return {
32
- enabled: expression.enabled,
33
- summaryIndex,
34
- validationType: expression.validationType,
35
- value: expression.value,
36
- };
37
- }
38
- })
39
- .filter(Boolean),
40
- });
41
- })
42
- .filter((aggFilter) => aggFilter && aggFilter.expressions && aggFilter.expressions.length);
43
- return aggFilters && concatAggFilters(aggFilters);
17
+ const logic = (0, objectCopy_1.objectCopy)((0, adapters_1.FDToLogic)((0, adapters_1.adaptFilterData)({ logic: aggregateFilters })));
18
+ if ((0, isEmpty_1.isEmpty)(logic) || (0, isEmpty_1.isEmpty)(logic[0].filters))
19
+ return;
20
+ (0, traverseTree_1.traverseTree)({
21
+ root: logic[0].filters,
22
+ getChildren: (node) => node.expressions,
23
+ isLeaf: (node) => !("operator" in node),
24
+ onLeaf: (expression) => {
25
+ const { enabled, validationType, value } = expression;
26
+ const summaryIndex = getIndexByExpression(expression, summaries);
27
+ Object.keys(expression).forEach((key) => {
28
+ delete expression[key];
29
+ });
30
+ expression.enabled = enabled;
31
+ expression.summaryIndex = summaryIndex;
32
+ expression.validationType = validationType;
33
+ expression.value = value;
34
+ },
35
+ });
36
+ return logic[0].filters[0];
44
37
  }
45
38
  exports.getAggFilters = getAggFilters;
46
39
  /**
@@ -69,23 +62,13 @@ function getAggregateFilters(logics = []) {
69
62
  return newLogics;
70
63
  }, []);
71
64
  }
72
- function concatAggFilters(aggFilters, operator = FILTER_OPERATOR_1.FILTER_OPERATOR.AND) {
73
- const expressions = aggFilters.filter(Boolean);
74
- if (!expressions.length)
75
- return undefined;
76
- else if (expressions.length === 1)
77
- return expressions[0];
78
- return {
79
- operator,
80
- expressions: aggFilters.filter(Boolean),
81
- };
82
- }
83
- function getExpressionsInLogic(logics = []) {
84
- return logics.reduce((expressions, logic) => {
85
- let _expressions = expressions;
86
- logic.filters.forEach((filter) => {
87
- _expressions = _expressions.concat(filter.expressions[0].expressions);
88
- });
89
- return _expressions;
90
- }, []);
65
+ /**
66
+ * Finds the index of a summary that matches the given filter expression based on question ID and Qrvey ID.
67
+ * @param expression The filter expression to match
68
+ * @param summaries The list of summaries to search
69
+ * @returns The index of the matching summary, or -1 if not found
70
+ */
71
+ function getIndexByExpression(expression, summaries = []) {
72
+ return summaries.findIndex((summary) => expression.questionid === summary.questionid &&
73
+ expression.qrveyid === summary.qrveyid);
91
74
  }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Traverses a tree structure and allows modification of nodes and leaves via callback functions.
3
+ * @param options The options for traversing and modifying the tree.
4
+ * @param options.root The root node of the tree.
5
+ * @param options.getChildren Function to get the children of a node.
6
+ * @param options.isLeaf Function to determine if a node is a leaf.
7
+ * @param options.onLeaf Callback function to be called on each leaf node.
8
+ * @param options.onNode Callback function to be called on each non-leaf node.
9
+ */
10
+ export declare function traverseTree<TNode, TLeaf extends TNode = TNode>(options: {
11
+ root: TNode | readonly TNode[];
12
+ getChildren: (node: TNode) => readonly TNode[] | undefined;
13
+ isLeaf?: (node: TNode) => node is TLeaf;
14
+ onLeaf?: (leaf: TLeaf, parent: TNode | null) => void;
15
+ onNode?: (node: TNode, parent: TNode | null) => void;
16
+ }): void;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.traverseTree = void 0;
4
+ /**
5
+ * Traverses a tree structure and allows modification of nodes and leaves via callback functions.
6
+ * @param options The options for traversing and modifying the tree.
7
+ * @param options.root The root node of the tree.
8
+ * @param options.getChildren Function to get the children of a node.
9
+ * @param options.isLeaf Function to determine if a node is a leaf.
10
+ * @param options.onLeaf Callback function to be called on each leaf node.
11
+ * @param options.onNode Callback function to be called on each non-leaf node.
12
+ */
13
+ function traverseTree(options) {
14
+ const { root, getChildren, isLeaf, onLeaf, onNode } = options;
15
+ /**
16
+ * Recursively walks the tree, calling the appropriate callbacks for nodes and leaves.
17
+ * @param node The current node being processed.
18
+ * @param parent The parent of the current node, or null if it's the root.
19
+ */
20
+ function walk(node, parent) {
21
+ onNode === null || onNode === void 0 ? void 0 : onNode(node, parent);
22
+ if (isLeaf === null || isLeaf === void 0 ? void 0 : isLeaf(node)) {
23
+ onLeaf === null || onLeaf === void 0 ? void 0 : onLeaf(node, parent);
24
+ return;
25
+ }
26
+ const children = getChildren(node);
27
+ if (!children)
28
+ return;
29
+ for (const child of children) {
30
+ walk(child, node);
31
+ }
32
+ }
33
+ const roots = Array.isArray(root) ? root : [root];
34
+ for (const r of roots)
35
+ walk(r, null);
36
+ }
37
+ exports.traverseTree = traverseTree;
@@ -1,8 +1,8 @@
1
- import { COLUMN } from "../../columns/constants/COLUMN";
2
1
  import { isEmpty } from "../../general/mix/isEmpty";
3
- import { objectCopy } from "../../general/object/objectCopy";
4
2
  import { _get } from "../../general/object/get";
5
- import { FILTER_OPERATOR } from "../constants/common/FILTER_OPERATOR";
3
+ import { objectCopy } from "../../general/object/objectCopy";
4
+ import { traverseTree } from "../../general/object/traverseTree";
5
+ import { adaptFilterData, FDToLogic } from "../adapters";
6
6
  /**
7
7
  * @deprecated soon
8
8
  * @param logics
@@ -11,33 +11,26 @@ import { FILTER_OPERATOR } from "../constants/common/FILTER_OPERATOR";
11
11
  */
12
12
  export function getAggFilters(logics = [], summaries = []) {
13
13
  const aggregateFilters = getAggregateFilters(logics);
14
- const aggFilters = aggregateFilters
15
- .map((aggFilter) => {
16
- const expressions = getExpressionsInLogic([aggFilter]);
17
- return (expressions &&
18
- expressions.length && {
19
- operator: FILTER_OPERATOR.OR,
20
- expressions: expressions
21
- .map((expression) => {
22
- const summaryIndex = summaries.findIndex((summary) => expression.questionid === summary.questionid &&
23
- expression.qrveyid === summary.qrveyid &&
24
- _get(expression.uiExtras.column, "aggregate") ===
25
- summary.aggregate);
26
- if (summaryIndex > -1 ||
27
- expression.questionType === COLUMN.AGGREGATED_FORMULA) {
28
- return {
29
- enabled: expression.enabled,
30
- summaryIndex,
31
- validationType: expression.validationType,
32
- value: expression.value,
33
- };
34
- }
35
- })
36
- .filter(Boolean),
37
- });
38
- })
39
- .filter((aggFilter) => aggFilter && aggFilter.expressions && aggFilter.expressions.length);
40
- return aggFilters && concatAggFilters(aggFilters);
14
+ const logic = objectCopy(FDToLogic(adaptFilterData({ logic: aggregateFilters })));
15
+ if (isEmpty(logic) || isEmpty(logic[0].filters))
16
+ return;
17
+ traverseTree({
18
+ root: logic[0].filters,
19
+ getChildren: (node) => node.expressions,
20
+ isLeaf: (node) => !("operator" in node),
21
+ onLeaf: (expression) => {
22
+ const { enabled, validationType, value } = expression;
23
+ const summaryIndex = getIndexByExpression(expression, summaries);
24
+ Object.keys(expression).forEach((key) => {
25
+ delete expression[key];
26
+ });
27
+ expression.enabled = enabled;
28
+ expression.summaryIndex = summaryIndex;
29
+ expression.validationType = validationType;
30
+ expression.value = value;
31
+ },
32
+ });
33
+ return logic[0].filters[0];
41
34
  }
42
35
  /**
43
36
  * Filters and gets the filters used for aggregate values.
@@ -65,23 +58,13 @@ function getAggregateFilters(logics = []) {
65
58
  return newLogics;
66
59
  }, []);
67
60
  }
68
- function concatAggFilters(aggFilters, operator = FILTER_OPERATOR.AND) {
69
- const expressions = aggFilters.filter(Boolean);
70
- if (!expressions.length)
71
- return undefined;
72
- else if (expressions.length === 1)
73
- return expressions[0];
74
- return {
75
- operator,
76
- expressions: aggFilters.filter(Boolean),
77
- };
78
- }
79
- function getExpressionsInLogic(logics = []) {
80
- return logics.reduce((expressions, logic) => {
81
- let _expressions = expressions;
82
- logic.filters.forEach((filter) => {
83
- _expressions = _expressions.concat(filter.expressions[0].expressions);
84
- });
85
- return _expressions;
86
- }, []);
61
+ /**
62
+ * Finds the index of a summary that matches the given filter expression based on question ID and Qrvey ID.
63
+ * @param expression The filter expression to match
64
+ * @param summaries The list of summaries to search
65
+ * @returns The index of the matching summary, or -1 if not found
66
+ */
67
+ function getIndexByExpression(expression, summaries = []) {
68
+ return summaries.findIndex((summary) => expression.questionid === summary.questionid &&
69
+ expression.qrveyid === summary.qrveyid);
87
70
  }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Traverses a tree structure and allows modification of nodes and leaves via callback functions.
3
+ * @param options The options for traversing and modifying the tree.
4
+ * @param options.root The root node of the tree.
5
+ * @param options.getChildren Function to get the children of a node.
6
+ * @param options.isLeaf Function to determine if a node is a leaf.
7
+ * @param options.onLeaf Callback function to be called on each leaf node.
8
+ * @param options.onNode Callback function to be called on each non-leaf node.
9
+ */
10
+ export declare function traverseTree<TNode, TLeaf extends TNode = TNode>(options: {
11
+ root: TNode | readonly TNode[];
12
+ getChildren: (node: TNode) => readonly TNode[] | undefined;
13
+ isLeaf?: (node: TNode) => node is TLeaf;
14
+ onLeaf?: (leaf: TLeaf, parent: TNode | null) => void;
15
+ onNode?: (node: TNode, parent: TNode | null) => void;
16
+ }): void;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Traverses a tree structure and allows modification of nodes and leaves via callback functions.
3
+ * @param options The options for traversing and modifying the tree.
4
+ * @param options.root The root node of the tree.
5
+ * @param options.getChildren Function to get the children of a node.
6
+ * @param options.isLeaf Function to determine if a node is a leaf.
7
+ * @param options.onLeaf Callback function to be called on each leaf node.
8
+ * @param options.onNode Callback function to be called on each non-leaf node.
9
+ */
10
+ export function traverseTree(options) {
11
+ const { root, getChildren, isLeaf, onLeaf, onNode } = options;
12
+ /**
13
+ * Recursively walks the tree, calling the appropriate callbacks for nodes and leaves.
14
+ * @param node The current node being processed.
15
+ * @param parent The parent of the current node, or null if it's the root.
16
+ */
17
+ function walk(node, parent) {
18
+ onNode === null || onNode === void 0 ? void 0 : onNode(node, parent);
19
+ if (isLeaf === null || isLeaf === void 0 ? void 0 : isLeaf(node)) {
20
+ onLeaf === null || onLeaf === void 0 ? void 0 : onLeaf(node, parent);
21
+ return;
22
+ }
23
+ const children = getChildren(node);
24
+ if (!children)
25
+ return;
26
+ for (const child of children) {
27
+ walk(child, node);
28
+ }
29
+ }
30
+ const roots = Array.isArray(root) ? root : [root];
31
+ for (const r of roots)
32
+ walk(r, null);
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qrvey/utils",
3
- "version": "1.16.1-5",
3
+ "version": "1.16.1-7",
4
4
  "description": "Helper, Utils for all Qrvey Projects",
5
5
  "homepage": "https://bitbucket.org/qrvey/qrvey_utils/wiki/Home",
6
6
  "main": "dist/index.js",