dtable-utils 0.0.3 → 0.0.4-beta.1

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.
Files changed (152) hide show
  1. package/README.md +42 -5
  2. package/dist/index.js +1 -1
  3. package/es/cell-value-get/cell-value.js +250 -20
  4. package/es/cell-value-get/collaborator.js +16 -3
  5. package/es/cell-value-get/digital-sign.js +1 -1
  6. package/es/cell-value-get/duration.js +3 -3
  7. package/es/cell-value-get/geolocation.js +52 -30
  8. package/es/cell-value-get/long-text.js +1 -1
  9. package/es/cell-value-get/number.js +51 -19
  10. package/es/cell-value-get/option.js +33 -9
  11. package/es/cell-value-get/rate.js +11 -0
  12. package/es/cell-value-set/date.js +26 -0
  13. package/es/cell-value-set/number.js +53 -1
  14. package/es/color/column-color.js +437 -0
  15. package/es/color/gradient-color.js +100 -0
  16. package/es/color/row-color.js +289 -0
  17. package/es/column/{common.js → core.js} +2 -14
  18. package/es/column/date.js +1 -1
  19. package/es/column/number.js +1 -1
  20. package/es/column/option.js +145 -0
  21. package/es/common.js +23 -3
  22. package/es/constants/color.js +16 -0
  23. package/es/constants/column-permission.js +7 -0
  24. package/es/constants/column.js +127 -2
  25. package/es/constants/filter/index.js +5 -1
  26. package/es/constants/formula.js +2 -1
  27. package/es/constants/grid-header.js +6 -0
  28. package/es/constants/group.js +23 -0
  29. package/es/constants/limit.js +3 -0
  30. package/es/constants/reg.js +4 -0
  31. package/es/constants/row-color.js +11 -0
  32. package/es/constants/sort.js +11 -0
  33. package/es/date.js +1 -1
  34. package/es/filter/core.js +322 -0
  35. package/es/filter/filter-column/array.js +87 -0
  36. package/es/filter/filter-column/checkbox.js +14 -0
  37. package/es/filter/filter-column/collaborator.js +72 -0
  38. package/es/filter/filter-column/creator.js +59 -0
  39. package/es/filter/filter-column/date.js +102 -0
  40. package/es/filter/filter-column/digital-sign.js +29 -0
  41. package/es/filter/filter-column/file.js +27 -0
  42. package/es/filter/filter-column/formula.js +57 -0
  43. package/es/filter/filter-column/geolocation.js +58 -0
  44. package/es/filter/filter-column/link.js +20 -0
  45. package/es/filter/filter-column/long-text.js +28 -0
  46. package/es/filter/filter-column/multiple-select.js +64 -0
  47. package/es/filter/filter-column/number.js +97 -0
  48. package/es/filter/filter-column/single-select.js +45 -0
  49. package/es/filter/filter-column/text.js +60 -0
  50. package/es/filter/filter-row.js +233 -0
  51. package/es/group/core.js +116 -0
  52. package/es/group/group-row.js +402 -0
  53. package/es/index.js +63 -11
  54. package/es/link/core.js +78 -0
  55. package/es/number.js +21 -0
  56. package/es/row/core.js +180 -0
  57. package/es/sort/core.js +91 -0
  58. package/es/sort/sort-column/array.js +72 -0
  59. package/es/sort/sort-column/checkbox.js +22 -0
  60. package/es/sort/sort-column/collaborator.js +16 -0
  61. package/es/sort/sort-column/date.js +25 -0
  62. package/es/sort/sort-column/formula.js +49 -0
  63. package/es/sort/sort-column/link.js +32 -0
  64. package/es/sort/sort-column/multiple-select.js +45 -0
  65. package/es/sort/sort-column/number.js +25 -0
  66. package/es/sort/sort-column/single-select.js +30 -0
  67. package/es/sort/sort-column/text.js +61 -0
  68. package/es/sort/sort-row.js +110 -0
  69. package/es/table/column.js +27 -0
  70. package/es/table/core.js +14 -0
  71. package/es/table/row.js +25 -0
  72. package/es/validate/filter.js +1 -1
  73. package/es/validate/geolocation.js +5 -0
  74. package/es/view/core.js +134 -0
  75. package/es/view/formula.js +147 -0
  76. package/es/view/group.js +32 -0
  77. package/es/view/summaries.js +167 -0
  78. package/lib/cell-value-get/cell-value.js +254 -19
  79. package/lib/cell-value-get/collaborator.js +16 -2
  80. package/lib/cell-value-get/digital-sign.js +1 -1
  81. package/lib/cell-value-get/duration.js +3 -2
  82. package/lib/cell-value-get/geolocation.js +53 -30
  83. package/lib/cell-value-get/long-text.js +1 -1
  84. package/lib/cell-value-get/number.js +51 -18
  85. package/lib/cell-value-get/option.js +34 -8
  86. package/lib/cell-value-get/rate.js +15 -0
  87. package/lib/cell-value-set/date.js +30 -0
  88. package/lib/cell-value-set/number.js +53 -0
  89. package/lib/color/column-color.js +446 -0
  90. package/lib/color/gradient-color.js +110 -0
  91. package/lib/color/row-color.js +297 -0
  92. package/lib/column/{common.js → core.js} +1 -14
  93. package/lib/column/date.js +2 -2
  94. package/lib/column/number.js +3 -3
  95. package/lib/column/option.js +153 -0
  96. package/lib/common.js +27 -3
  97. package/lib/constants/color.js +20 -0
  98. package/lib/constants/column-permission.js +11 -0
  99. package/lib/constants/column.js +134 -1
  100. package/lib/constants/filter/index.js +5 -0
  101. package/lib/constants/formula.js +2 -0
  102. package/lib/constants/grid-header.js +10 -0
  103. package/lib/constants/group.js +36 -0
  104. package/lib/constants/limit.js +7 -0
  105. package/lib/constants/reg.js +9 -0
  106. package/lib/constants/row-color.js +17 -0
  107. package/lib/constants/sort.js +18 -0
  108. package/lib/filter/core.js +336 -0
  109. package/lib/filter/filter-column/array.js +91 -0
  110. package/lib/filter/filter-column/checkbox.js +18 -0
  111. package/lib/filter/filter-column/collaborator.js +80 -0
  112. package/lib/filter/filter-column/creator.js +63 -0
  113. package/lib/filter/filter-column/date.js +106 -0
  114. package/lib/filter/filter-column/digital-sign.js +33 -0
  115. package/lib/filter/filter-column/file.js +31 -0
  116. package/lib/filter/filter-column/formula.js +61 -0
  117. package/lib/filter/filter-column/geolocation.js +62 -0
  118. package/lib/filter/filter-column/link.js +24 -0
  119. package/lib/filter/filter-column/long-text.js +32 -0
  120. package/lib/filter/filter-column/multiple-select.js +72 -0
  121. package/lib/filter/filter-column/number.js +101 -0
  122. package/lib/filter/filter-column/single-select.js +49 -0
  123. package/lib/filter/filter-column/text.js +64 -0
  124. package/lib/filter/filter-row.js +239 -0
  125. package/lib/group/core.js +126 -0
  126. package/lib/group/group-row.js +413 -0
  127. package/lib/index.js +177 -7
  128. package/lib/link/core.js +90 -0
  129. package/lib/number.js +26 -0
  130. package/lib/row/core.js +189 -0
  131. package/lib/sort/core.js +102 -0
  132. package/lib/sort/sort-column/array.js +76 -0
  133. package/lib/sort/sort-column/checkbox.js +26 -0
  134. package/lib/sort/sort-column/collaborator.js +20 -0
  135. package/lib/sort/sort-column/date.js +29 -0
  136. package/lib/sort/sort-column/formula.js +53 -0
  137. package/lib/sort/sort-column/link.js +36 -0
  138. package/lib/sort/sort-column/multiple-select.js +49 -0
  139. package/lib/sort/sort-column/number.js +29 -0
  140. package/lib/sort/sort-column/single-select.js +34 -0
  141. package/lib/sort/sort-column/text.js +66 -0
  142. package/lib/sort/sort-row.js +115 -0
  143. package/lib/table/column.js +32 -0
  144. package/lib/table/core.js +18 -0
  145. package/lib/table/row.js +30 -0
  146. package/lib/validate/filter.js +3 -3
  147. package/lib/validate/geolocation.js +9 -0
  148. package/lib/view/core.js +143 -0
  149. package/lib/view/formula.js +154 -0
  150. package/lib/view/group.js +36 -0
  151. package/lib/view/summaries.js +173 -0
  152. package/package.json +5 -3
@@ -0,0 +1,110 @@
1
+ import { deleteInvalidSort } from './core.js';
2
+ import { sortCheckbox } from './sort-column/checkbox.js';
3
+ import { sortCollaborator } from './sort-column/collaborator.js';
4
+ import { sortDate } from './sort-column/date.js';
5
+ import { getCollaboratorsNames } from '../cell-value-get/collaborator.js';
6
+ import '../cell-value-get/cell-value.js';
7
+ import { sortNumber } from './sort-column/number.js';
8
+ import { sortText } from './sort-column/text.js';
9
+ import { CellType } from '../constants/cell-type.js';
10
+ import { DATE_COLUMN_OPTIONS } from '../constants/column.js';
11
+ import { FORMULA_COLUMN_TYPES_MAP } from '../constants/formula.js';
12
+ import { sortFormula } from './sort-column/formula.js';
13
+ import { sortLink } from './sort-column/link.js';
14
+ import { sortMultipleSelect } from './sort-column/multiple-select.js';
15
+ import { sortSingleSelect } from './sort-column/single-select.js';
16
+ import { NUMBER_SORTER_COLUMN_TYPES } from '../constants/sort.js';
17
+
18
+ /**
19
+ * Sort rows with multiple sorts
20
+ * @param {array} tableRows e.g. [{ _id, [column.key]: '', ...}, ...]
21
+ * @param {array} sorts e.g. [{ column_key, sort_type, column, ... }, ...]
22
+ * @param {object} formulaRows computed value of formula, link-formula, link etc.
23
+ * @param {object} value e.g. { collaborators, ... }
24
+ */
25
+ var sortRowsWithMultiSorts = function sortRowsWithMultiSorts(tableRows, sorts, _ref) {
26
+ var _ref$formulaRows = _ref.formulaRows,
27
+ formulaRows = _ref$formulaRows === void 0 ? {} : _ref$formulaRows,
28
+ value = _ref.value;
29
+ tableRows.sort(function (currentRow, nextRow) {
30
+ var initValue = 0;
31
+ sorts.forEach(function (sort) {
32
+ var column_key = sort.column_key,
33
+ sort_type = sort.sort_type,
34
+ column = sort.column;
35
+ var columnType = column.type,
36
+ columnData = column.data;
37
+ var currCellVal = currentRow[column_key];
38
+ var nextCellVal = nextRow[column_key];
39
+ if (DATE_COLUMN_OPTIONS.includes(columnType)) {
40
+ initValue = initValue || sortDate(currCellVal, nextCellVal, sort_type);
41
+ } else if (columnType === CellType.SINGLE_SELECT) {
42
+ initValue = initValue || sortSingleSelect(currCellVal, nextCellVal, sort);
43
+ } else if (NUMBER_SORTER_COLUMN_TYPES.includes(columnType)) {
44
+ initValue = initValue || sortNumber(currCellVal, nextCellVal, sort_type);
45
+ } else if (FORMULA_COLUMN_TYPES_MAP[columnType]) {
46
+ var currentFormulaRow = formulaRows[currentRow._id] || {};
47
+ var nextFormulaRow = formulaRows[nextRow._id] || {};
48
+ currCellVal = currentFormulaRow[column_key];
49
+ nextCellVal = nextFormulaRow[column_key];
50
+ initValue = initValue || sortFormula(currCellVal, nextCellVal, sort_type, {
51
+ columnData: columnData,
52
+ value: value
53
+ });
54
+ } else if (columnType === CellType.COLLABORATOR) {
55
+ var collaborators = value.collaborators;
56
+ var currValidCollaborators = currCellVal;
57
+ var nextValidCollaborators = nextCellVal;
58
+ if (collaborators) {
59
+ currValidCollaborators = getCollaboratorsNames(currCellVal, collaborators);
60
+ nextValidCollaborators = getCollaboratorsNames(nextCellVal, collaborators);
61
+ }
62
+ initValue = initValue || sortCollaborator(currValidCollaborators, nextValidCollaborators, sort_type);
63
+ } else if (columnType === CellType.CHECKBOX) {
64
+ initValue = initValue || sortCheckbox(currCellVal, nextCellVal, sort_type);
65
+ } else if (columnType === CellType.MULTIPLE_SELECT) {
66
+ initValue = initValue || sortMultipleSelect(currCellVal, nextCellVal, sort);
67
+ } else if (columnType === CellType.LINK) {
68
+ var _currentFormulaRow = formulaRows[currentRow._id] || {};
69
+ var _nextFormulaRow = formulaRows[nextRow._id] || {};
70
+ currCellVal = _currentFormulaRow[column_key];
71
+ nextCellVal = _nextFormulaRow[column_key];
72
+ initValue = initValue || sortLink(currCellVal, nextCellVal, sort_type, {
73
+ columnData: columnData,
74
+ value: value
75
+ });
76
+ } else {
77
+ initValue = initValue || sortText(currCellVal, nextCellVal, sort_type);
78
+ }
79
+ });
80
+ return initValue;
81
+ });
82
+ };
83
+
84
+ /**
85
+ * Get sorted rows ids from table rows with multiple sorts
86
+ * @param {array} sorts e.g. [{ column_key, sort_type, column, ... }, ...]
87
+ * @param {array} rows e.g. [{ _id, [column.key]: '', ...}, ...]
88
+ * @param {array} columns e.g. [{ key, type, ... }, ...]
89
+ * @param {object} formulaRows computed value of formula, link-formula, link etc.
90
+ * @param {object} value e.g. { collaborators, ... }
91
+ * @returns sorted rows ids, array
92
+ */
93
+ var sortTableRows = function sortTableRows(sorts, rows, columns) {
94
+ var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
95
+ formulaRows = _ref2.formulaRows,
96
+ value = _ref2.value;
97
+ if (!Array.isArray(rows) || rows.length === 0) return [];
98
+ var sortRows = rows.slice(0);
99
+ var validSorts = deleteInvalidSort(sorts, columns);
100
+ var normalizedFormulaRows = formulaRows || {};
101
+ sortRowsWithMultiSorts(sortRows, validSorts, {
102
+ formulaRows: normalizedFormulaRows,
103
+ value: value
104
+ });
105
+ return sortRows.map(function (row) {
106
+ return row._id;
107
+ });
108
+ };
109
+
110
+ export { sortRowsWithMultiSorts, sortTableRows };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Get column by key from table
3
+ * @param {object} table
4
+ * @param {string} columnKey
5
+ * @returns column, object
6
+ */
7
+ var getTableColumnByKey = function getTableColumnByKey(table, columnKey) {
8
+ if (!table || !Array.isArray(table.columns) || !columnKey) return null;
9
+ return table.columns.find(function (column) {
10
+ return column.key === columnKey;
11
+ });
12
+ };
13
+
14
+ /**
15
+ * Get table column by name
16
+ * @param {object} table
17
+ * @param {string} columnName
18
+ * @returns column, object
19
+ */
20
+ var getTableColumnByName = function getTableColumnByName(table, columnName) {
21
+ if (!table || !Array.isArray(table.columns) || !columnName) return null;
22
+ return table.columns.find(function (column) {
23
+ return column.name === columnName;
24
+ });
25
+ };
26
+
27
+ export { getTableColumnByKey, getTableColumnByName };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Get table by id
3
+ * @param {array} tables
4
+ * @param {string} tableId
5
+ * @returns table, object
6
+ */
7
+ var getTableById = function getTableById(tables, tableId) {
8
+ if (!Array.isArray(tables) || !tableId) return null;
9
+ return tables.find(function (table) {
10
+ return table._id === tableId;
11
+ });
12
+ };
13
+
14
+ export { getTableById };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Get table row by id
3
+ * @param {object} table
4
+ * @param {string} rowId the id of row
5
+ * @returns row, object
6
+ */
7
+ var getRowById = function getRowById(table, rowId) {
8
+ if (!table || !table.id_row_map || !rowId) return null;
9
+ return table.id_row_map[rowId];
10
+ };
11
+
12
+ /**
13
+ * Get table rows by ids
14
+ * @param {object} table { id_row_map, ... }
15
+ * @param {array} rowsIds [ row._id, ... ]
16
+ * @returns rows, array
17
+ */
18
+ var getRowsByIds = function getRowsByIds(table, rowsIds) {
19
+ if (!table || !table.id_row_map || !Array.isArray(rowsIds)) return [];
20
+ return rowsIds.map(function (rowId) {
21
+ return table.id_row_map[rowId];
22
+ }).filter(Boolean);
23
+ };
24
+
25
+ export { getRowById, getRowsByIds };
@@ -5,7 +5,7 @@ import { CellType } from '../constants/cell-type.js';
5
5
  import { COLLABORATOR_COLUMN_TYPES } from '../constants/column.js';
6
6
  import { FORMULA_COLUMN_TYPES_MAP, FORMULA_RESULT_TYPE } from '../constants/formula.js';
7
7
  import { FILTER_ERR_MSG } from '../constants/filter/index.js';
8
- import { getColumnOptions } from '../column/common.js';
8
+ import { getColumnOptions } from '../column/option.js';
9
9
  import { isDateColumn } from '../column/date.js';
10
10
  import { FILTER_PREDICATE_TYPE } from '../constants/filter/filter-predicate.js';
11
11
  import { FILTER_TERM_MODIFIER_TYPE } from '../constants/filter/filter-modifier.js';
@@ -0,0 +1,5 @@
1
+ var isValidPosition = function isValidPosition(lng, lat) {
2
+ return (lng || lng === 0) && (lat || lat === 0);
3
+ };
4
+
5
+ export { isValidPosition };
@@ -0,0 +1,134 @@
1
+ import { getValidFilters, getValidFiltersWithoutError } from '../filter/core.js';
2
+ import { getValidGroupbys } from '../group/core.js';
3
+ import { getValidSorts } from '../sort/core.js';
4
+ import { CellType } from '../constants/cell-type.js';
5
+
6
+ /**
7
+ * Get view by id
8
+ * @param {array} views e.g. [{ _id, ... }, ...]
9
+ * @param {string} viewId
10
+ * @returns view, object
11
+ */
12
+ var getViewById = function getViewById(views, viewId) {
13
+ if (!Array.isArray(views) || !viewId) return null;
14
+ return views.find(function (view) {
15
+ return view._id === viewId;
16
+ });
17
+ };
18
+
19
+ /**
20
+ * Check whether the view contains filters
21
+ * @param {object} view e.g. { filters, ... }
22
+ * @param {array} columns
23
+ * @returns bool
24
+ */
25
+ var isFilterView = function isFilterView(view, columns) {
26
+ var validFilters = getValidFilters(view.filters, columns);
27
+ return validFilters.length > 0;
28
+ };
29
+
30
+ /**
31
+ * Check whether the view contains groupbys
32
+ * @param {object} view e.g. { groupbys, ... }
33
+ * @param {array} columns
34
+ * @returns bool
35
+ */
36
+ var isGroupView = function isGroupView(view, columns) {
37
+ var validGroupbys = getValidGroupbys(view.groupbys, columns);
38
+ return validGroupbys.length > 0;
39
+ };
40
+
41
+ /**
42
+ * Check whether the view contains sorts
43
+ * @param {object} view e.g. { sorts, ... }
44
+ * @param {array} columns
45
+ * @returns bool
46
+ */
47
+ var isSortView = function isSortView(view, columns) {
48
+ var validSorts = getValidSorts(view.sorts, columns);
49
+ return validSorts.length > 0;
50
+ };
51
+
52
+ /**
53
+ * Check is default view which no contains filters, sorts, groupbys etc.
54
+ * @param {object} view e.g. { filters, groupbys, sorts, ... }
55
+ * @param {array} columns
56
+ * @returns bool
57
+ */
58
+ var isDefaultView = function isDefaultView(view, columns) {
59
+ return !isFilterView(view, columns) && !isSortView(view, columns) && !isGroupView(view, columns);
60
+ };
61
+
62
+ /**
63
+ * Get link columns which used either in filters, groupbys, sorts etc.
64
+ * @param {object} view e.g. { filters, groupbys, sorts, ... }
65
+ * @param {object} table e.g. { columns, ... }
66
+ * @returns link columns, array
67
+ */
68
+ var getLinkColumnsUsedInFilters = function getLinkColumnsUsedInFilters(view, table) {
69
+ var filters = view.filters,
70
+ sorts = view.sorts,
71
+ groupbys = view.groupbys;
72
+ var columns = table.columns;
73
+ if (!Array.isArray(columns)) return [];
74
+ var validFilters = getValidFiltersWithoutError(filters, columns);
75
+ var validSorts = getValidSorts(sorts, columns);
76
+ var validGroupbys = getValidGroupbys(groupbys, columns);
77
+ var hasFilters = validFilters && validFilters.length > 0;
78
+ var hasSorts = validSorts && validSorts.length > 0;
79
+ var hasGroupbys = validGroupbys && validGroupbys.length > 0;
80
+ if (!hasFilters && !hasSorts && !hasGroupbys) return [];
81
+ var keyLinkColumnMap = {};
82
+ var keyExistLinkColumnMap = {};
83
+ var linkColumns = [];
84
+ columns.forEach(function (column) {
85
+ var key = column.key,
86
+ name = column.name,
87
+ type = column.type,
88
+ data = column.data,
89
+ width = column.width;
90
+ if (type === CellType.LINK) {
91
+ keyLinkColumnMap[column.key] = {
92
+ key: key,
93
+ name: name,
94
+ type: type,
95
+ data: data,
96
+ width: width
97
+ };
98
+ }
99
+ });
100
+ if (Object.keys(keyLinkColumnMap).length === 0) return [];
101
+ if (hasFilters) {
102
+ validFilters.forEach(function (filter) {
103
+ var filterColumnKey = filter.column_key;
104
+ var filterLinkColumn = keyLinkColumnMap[filterColumnKey];
105
+ if (filterLinkColumn && !keyExistLinkColumnMap[filterColumnKey]) {
106
+ linkColumns.push(filterLinkColumn);
107
+ keyExistLinkColumnMap[filterColumnKey] = true;
108
+ }
109
+ });
110
+ }
111
+ if (hasSorts) {
112
+ validSorts.forEach(function (sort) {
113
+ var sortColumnKey = sort.column_key;
114
+ var sortLinkColumn = keyLinkColumnMap[sortColumnKey];
115
+ if (sortLinkColumn && !keyExistLinkColumnMap[sortColumnKey]) {
116
+ linkColumns.push(sortLinkColumn);
117
+ keyExistLinkColumnMap[sortColumnKey] = true;
118
+ }
119
+ });
120
+ }
121
+ if (hasGroupbys) {
122
+ validGroupbys.forEach(function (groupby) {
123
+ var groupbyColumnKey = groupby.column_key;
124
+ var groupbyLinkColumn = keyLinkColumnMap[groupbyColumnKey];
125
+ if (groupbyLinkColumn && !keyExistLinkColumnMap[groupbyColumnKey]) {
126
+ linkColumns.push(groupbyLinkColumn);
127
+ keyExistLinkColumnMap[groupbyColumnKey] = true;
128
+ }
129
+ });
130
+ }
131
+ return linkColumns;
132
+ };
133
+
134
+ export { getLinkColumnsUsedInFilters, getViewById, isDefaultView, isFilterView, isGroupView, isSortView };
@@ -0,0 +1,147 @@
1
+ import { CellType } from '../constants/cell-type.js';
2
+ import { FORMULA_COLUMN_TYPES_MAP } from '../constants/formula.js';
3
+
4
+ /**
5
+ * Transform the formats of link column to link-formula's
6
+ * @param {object} linkColumn e.g. { key, data: { display_column_key, ... }, ... }
7
+ * @returns converted link column, object
8
+ */
9
+ var transLink2LinkFormula = function transLink2LinkFormula(linkColumn) {
10
+ var key = linkColumn.key,
11
+ name = linkColumn.name,
12
+ data = linkColumn.data;
13
+ var display_column_key = data.display_column_key;
14
+ return {
15
+ key: key,
16
+ name: name,
17
+ type: CellType.LINK_FORMULA,
18
+ data: {
19
+ formula: 'lookup',
20
+ link_column_key: key,
21
+ level1_linked_table_column_key: display_column_key,
22
+ level2_linked_table_column_key: null,
23
+ operated_columns: []
24
+ }
25
+ };
26
+ };
27
+
28
+ /**
29
+ * Get sorted formula columns
30
+ * @param {array} formulaColumns the formula columns waiting to sort
31
+ * @param {object} table e.g. { columns, ... }
32
+ * @returns sorted formula columns, array
33
+ */
34
+ var getSortedFormulaColumns = function getSortedFormulaColumns(formulaColumns, table) {
35
+ if (!Array.isArray(formulaColumns) || !table || !Array.isArray(table.columns)) return [];
36
+ var keyColumnMap = {};
37
+ table.columns.forEach(function (column) {
38
+ var key = column.key,
39
+ type = column.type;
40
+ if (FORMULA_COLUMN_TYPES_MAP[type]) {
41
+ keyColumnMap[key] = column;
42
+ } else if (type === CellType.LINK && column.data) {
43
+ keyColumnMap[key] = transLink2LinkFormula(column);
44
+ }
45
+ });
46
+
47
+ // get none-repeating formulas
48
+ var key_sorting_formula_column = {};
49
+ var sorting_formula_column_keys = [];
50
+ formulaColumns.forEach(function (column) {
51
+ var columnKey = column.key;
52
+ if (!key_sorting_formula_column[columnKey]) {
53
+ key_sorting_formula_column[columnKey] = column;
54
+ sorting_formula_column_keys.push(columnKey);
55
+ }
56
+ });
57
+
58
+ /**
59
+ * e.g. formula1 = formula2 + formula3
60
+ * e.g. formula2 = 1 + 2
61
+ * e.g. formula3 = formula2 + 3
62
+ * edges: formula2 --> formula1, formula2 --> formula3; formula3 --> formula1
63
+ * keyInDegreeMap, the total number of formula columns that depend on: { formula1_key: 2, formula2_key: 0, formula3_key: 1 }
64
+ * keyEdgesMap: { formula2_key: [formula1_key, formula3_key], formula3_kye: [formula1_key]}
65
+ */
66
+ var keyInDegreeMap = {};
67
+ var keyEdgesMap = {};
68
+ sorting_formula_column_keys.forEach(function (formulaColumnKey) {
69
+ var formulaColumn = key_sorting_formula_column[formulaColumnKey];
70
+ var data = formulaColumn.data;
71
+ var operated_columns = data.operated_columns;
72
+ keyInDegreeMap[formulaColumnKey] = 0;
73
+ if (Array.isArray(operated_columns)) {
74
+ operated_columns.forEach(function (dependColumnKey) {
75
+ var dependColumn = keyColumnMap[dependColumnKey];
76
+ if (!dependColumn) {
77
+ return;
78
+ }
79
+ keyInDegreeMap[formulaColumnKey] += 1;
80
+ if (keyEdgesMap[dependColumnKey]) {
81
+ keyEdgesMap[dependColumnKey].push(formulaColumnKey);
82
+ } else {
83
+ keyEdgesMap[dependColumnKey] = [formulaColumnKey];
84
+ }
85
+ });
86
+ }
87
+ });
88
+ var sortedList = [];
89
+ var waitingQueue = [];
90
+ sorting_formula_column_keys.forEach(function (formulaColumnKey) {
91
+ if (keyInDegreeMap[formulaColumnKey] === 0) {
92
+ waitingQueue.push(formulaColumnKey);
93
+ }
94
+ });
95
+ while (waitingQueue.length) {
96
+ var columnKey = waitingQueue.shift();
97
+ var edges = keyEdgesMap[columnKey];
98
+ sortedList.push(columnKey);
99
+ if (Array.isArray(edges)) {
100
+ edges.forEach(function (edge) {
101
+ keyInDegreeMap[edge] -= 1;
102
+ if (keyInDegreeMap[edge] === 0) {
103
+ waitingQueue.push(edge);
104
+ }
105
+ });
106
+ }
107
+ }
108
+ return sortedList.map(function (columnKey) {
109
+ return keyColumnMap[columnKey];
110
+ });
111
+ };
112
+
113
+ /**
114
+ * Get table formula columns and link columns
115
+ * @param {object} table e.g. { columns, ... }
116
+ * @returns formula and link columns, array
117
+ */
118
+ var getFormulaColumnsContainLinks = function getFormulaColumnsContainLinks(table) {
119
+ var columns = table.columns;
120
+ var formulaColumns = columns.filter(function (column) {
121
+ var type = column.type,
122
+ data = column.data;
123
+ if (type === CellType.LINK) {
124
+ return !!data;
125
+ }
126
+ return FORMULA_COLUMN_TYPES_MAP[type] && data;
127
+ });
128
+ return formulaColumns.map(function (column) {
129
+ if (column.type === CellType.LINK) {
130
+ return transLink2LinkFormula(column);
131
+ }
132
+ return column;
133
+ });
134
+ };
135
+
136
+ /**
137
+ * Get sorted formula and link columns from table
138
+ * @param {object} table e.g. { columns, ... }
139
+ * @returns sorted formula and link columns, array
140
+ */
141
+ var getSortedFormulaColumnsContainLinks = function getSortedFormulaColumnsContainLinks(table) {
142
+ var formulaColumns = getFormulaColumnsContainLinks(table);
143
+ if (formulaColumns.length === 0) return [];
144
+ return getSortedFormulaColumns(formulaColumns, table);
145
+ };
146
+
147
+ export { getFormulaColumnsContainLinks, getSortedFormulaColumns, getSortedFormulaColumnsContainLinks, transLink2LinkFormula };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Get group by paths
3
+ * @param {array} paths e.g. [ 0, 1, 2 ]
4
+ * @param {array} groups grouped rows
5
+ * @returns group, object
6
+ */
7
+ var getGroupByPath = function getGroupByPath(paths, groups) {
8
+ if (!Array.isArray(paths) || !Array.isArray(groups)) {
9
+ return null;
10
+ }
11
+ var level0GroupIndex = paths[0];
12
+ if (level0GroupIndex < 0 || level0GroupIndex >= groups.length) {
13
+ return null;
14
+ }
15
+ var level = 1;
16
+ var foundGroup = groups[level0GroupIndex];
17
+ while (level < paths.length) {
18
+ if (!foundGroup) {
19
+ break;
20
+ }
21
+ var subGroups = foundGroup.subgroups;
22
+ var currentLevelGroupIndex = paths[level];
23
+ if (!Array.isArray(subGroups) || currentLevelGroupIndex < 0 || currentLevelGroupIndex >= subGroups.length) {
24
+ break;
25
+ }
26
+ foundGroup = subGroups[currentLevelGroupIndex];
27
+ level += 1;
28
+ }
29
+ return foundGroup;
30
+ };
31
+
32
+ export { getGroupByPath };
@@ -0,0 +1,167 @@
1
+ import { getRowsByIds } from '../table/row.js';
2
+ import { isTableRows } from '../row/core.js';
3
+ import { isNumericColumn } from '../column/number.js';
4
+ import { isNumber } from '../number.js';
5
+ import { FORMULA_COLUMN_TYPES_MAP } from '../constants/formula.js';
6
+
7
+ /**
8
+ * Calculate summaries of table numeric columns
9
+ * @param {object} table e.g. { columns, ... }
10
+ * @param {array} rows e.g. table rows: [{ _id, ...}, ...] | view rows: [ row._id, ... ]
11
+ * @param {object} formulaRows for numeric formula columns
12
+ * @returns summaries: { [column.key]: { sum, average, median, max, min, ... }, ... }, object
13
+ */
14
+ var getSummaries = function getSummaries(table, rows, formulaRows) {
15
+ var numericColumns = table.columns.filter(function (column) {
16
+ return isNumericColumn(column);
17
+ });
18
+ if (numericColumns.length === 0) {
19
+ return {};
20
+ }
21
+ var summaryRows = isTableRows(rows) ? rows : getRowsByIds(table, rows);
22
+ var summaries = {};
23
+ numericColumns.forEach(function (column) {
24
+ var type = column.type,
25
+ key = column.key;
26
+ var validNumbersCount = 0;
27
+ var medianArr = [];
28
+ var sum = 0;
29
+ var min;
30
+ var max;
31
+ var median;
32
+ summaryRows.forEach(function (row) {
33
+ var cellValue = row[key];
34
+ if (FORMULA_COLUMN_TYPES_MAP[type]) {
35
+ var formulaRow = formulaRows && formulaRows[row._id];
36
+ cellValue = formulaRow && formulaRow[key];
37
+ if (Array.isArray(cellValue) && cellValue.length === 1) {
38
+ cellValue = cellValue[0];
39
+ }
40
+ }
41
+ if (isNumber(cellValue)) {
42
+ medianArr.push(cellValue);
43
+ validNumbersCount += 1;
44
+ sum += cellValue;
45
+ if (min === undefined) {
46
+ min = cellValue;
47
+ } else {
48
+ min = cellValue < min ? cellValue : min;
49
+ }
50
+ if (max === undefined) {
51
+ max = cellValue;
52
+ } else {
53
+ max = cellValue > max ? cellValue : max;
54
+ }
55
+ }
56
+ });
57
+ if (validNumbersCount > 0) {
58
+ medianArr.sort();
59
+ if (medianArr.length % 2 === 0) {
60
+ median = (medianArr[medianArr.length / 2 - 1] + medianArr[medianArr.length / 2]) / 2;
61
+ } else {
62
+ median = medianArr[parseInt(medianArr.length / 2)];
63
+ }
64
+ }
65
+ var average = validNumbersCount === 0 ? 0 : sum / validNumbersCount;
66
+ summaries[key] = {
67
+ sum: sum,
68
+ average: average,
69
+ median: median,
70
+ max: max,
71
+ min: min
72
+ };
73
+ });
74
+ return summaries;
75
+ };
76
+
77
+ /**
78
+ * Calculate group summaries by subgroup
79
+ * @param {array} subgroups e.g. [{ summaries, ... }, ...]
80
+ * @param {array} numericColumns
81
+ * @returns summaries: { [column.key]: { sum, average, median, max, min, ... }, ... }, object
82
+ */
83
+ var getSummariesWithSubgroups = function getSummariesWithSubgroups(subgroups, numericColumns) {
84
+ var subgroupsLen = subgroups.length;
85
+ var summaries = {};
86
+ numericColumns.forEach(function (column) {
87
+ var medianArr = [];
88
+ var sum = 0;
89
+ var average = 0;
90
+ var median;
91
+ var max;
92
+ var min;
93
+ subgroups.forEach(function (subgroup) {
94
+ var subSummaries = subgroup.summaries;
95
+ var summary = subSummaries[column.key];
96
+ var subgroupSum = summary.sum,
97
+ subgroupAverage = summary.average,
98
+ subgroupMedian = summary.median,
99
+ subgroupMax = summary.max,
100
+ subgroupMin = summary.min;
101
+ sum += subgroupSum || 0;
102
+ average += subgroupAverage || 0;
103
+ if (isNumber(subgroupMedian)) {
104
+ medianArr.push(subgroupMedian);
105
+ }
106
+ if (isNumber(subgroupMax)) {
107
+ if (max === undefined) {
108
+ max = subgroupMax;
109
+ } else {
110
+ max = subgroupMax > max ? subgroupMax : max;
111
+ }
112
+ }
113
+ if (isNumber(subgroupMin)) {
114
+ if (min === undefined) {
115
+ min = subgroupMin;
116
+ } else {
117
+ min = subgroupMin < min ? subgroupMin : min;
118
+ }
119
+ }
120
+ });
121
+ if (medianArr.length > 0) {
122
+ medianArr.sort();
123
+ if (medianArr.length % 2 === 0) {
124
+ median = (medianArr[medianArr.length / 2 - 1] + medianArr[medianArr.length / 2]) / 2;
125
+ } else {
126
+ median = medianArr[parseInt(medianArr.length / 2)];
127
+ }
128
+ }
129
+ average /= subgroupsLen;
130
+ summaries[column.key] = {
131
+ sum: sum,
132
+ average: average,
133
+ median: median,
134
+ max: max,
135
+ min: min
136
+ };
137
+ });
138
+ return summaries;
139
+ };
140
+
141
+ /**
142
+ * Calculate group summaries
143
+ * @param {array} groups e.g. [{ subgroups, row_ids, ... }, ...]
144
+ * @param {object} table e.g. { columns, ... }
145
+ * @param {object} formulaRows for numeric formula columns
146
+ * @returns void
147
+ */
148
+ var updateGroupSummaries = function updateGroupSummaries(groups, table, formulaRows) {
149
+ var numericColumns = table.columns.filter(function (column) {
150
+ return isNumericColumn(column);
151
+ });
152
+ if (!Array.isArray(groups) || numericColumns.length === 0) {
153
+ return;
154
+ }
155
+ groups.forEach(function (group) {
156
+ var subgroups = group.subgroups,
157
+ row_ids = group.row_ids;
158
+ if (Array.isArray(subgroups) && subgroups.length > 0) {
159
+ updateGroupSummaries(subgroups, table, formulaRows);
160
+ group.summaries = getSummariesWithSubgroups(subgroups, numericColumns);
161
+ } else if (row_ids) {
162
+ group.summaries = getSummaries(table, row_ids, formulaRows);
163
+ }
164
+ });
165
+ };
166
+
167
+ export { getSummaries, getSummariesWithSubgroups, updateGroupSummaries };