@softwear/latestcollectioncore 1.0.161 → 1.0.162

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.
@@ -99,6 +99,28 @@ function buildAggregationEvaluator(aggregationExpression) {
99
99
  function prepareSubTotalAggKey(aggKey) {
100
100
  return aggKey.split('\t').slice(0, -1).join('\t').concat(' Σ');
101
101
  }
102
+ /** Same as filtrex std.coerceBoolean(customProp(...)) for bare join keys on the transaction */
103
+ function coerceJoinKeyValue(v) {
104
+ return typeof v === 'boolean' ? +v : v;
105
+ }
106
+ function getTrieLeaf(root, transaction, keys) {
107
+ const nrKeys = keys.length;
108
+ if (nrKeys === 0)
109
+ return root;
110
+ let node = root;
111
+ for (let i = 0; i < nrKeys; i++) {
112
+ const k = coerceJoinKeyValue(transaction[keys[i]]);
113
+ if (!node.children)
114
+ node.children = new Map();
115
+ let next = node.children.get(k);
116
+ if (!next) {
117
+ next = {};
118
+ node.children.set(k, next);
119
+ }
120
+ node = next;
121
+ }
122
+ return node;
123
+ }
102
124
  /*
103
125
  * A factory function that returns a function that can be used to aggregate transactions
104
126
  * The factory function is called once for each aggregationExpression
@@ -122,47 +144,36 @@ function prepareSubTotalAggKey(aggKey) {
122
144
  * @returns {Function} - The aggregator function
123
145
  */
124
146
  function aggregator({ beginTimestamp, endTimestamp, filterExpression, aggregationExpression, rawAggregations, maxRows, totals, subtotalsForOuterGrouper, parentGroupTotals, }) {
125
- const filterExpressionCache = {};
126
147
  let filter;
127
- let filterDependencyEvaluator;
148
+ let filterGranularity = [];
149
+ const filterTrieRoot = {};
128
150
  if (filterExpression) {
129
151
  filter = (0, filtrex_1.compileExpression)(filterExpression, filtrexOptions);
130
- // Maintain a cache of evaluated filterExpressions
131
- // The granularity of the cache is determined by the joined tables in the filterExpression
132
- const filterGranularity = Object.entries(globalJoinableTables)
152
+ filterGranularity = Object.entries(globalJoinableTables)
133
153
  .filter((join) => filterExpression.includes(join[0] + '.'))
134
154
  .map((join) => join[1]);
135
- filterDependencyEvaluator = (0, filtrex_1.compileExpression)(filterGranularity.join('+'), filtrexOptions);
136
155
  }
137
156
  const aggregationEvaluator = buildAggregationEvaluator(aggregationExpression);
138
- // Maintain a cache of evaluated aggregationExpressions
139
- // The granularity of the cache is determined by the joined tables in the aggregationExpression
140
- const aggregationExpressionCache = {};
141
157
  const aggregationGranularity = Object.entries(globalJoinableTables)
142
158
  .filter((join) => aggregationExpression.includes(join[0] + '.'))
143
159
  .map((join) => join[1]);
144
- let aggregationDependencyExpression = aggregationGranularity.join('+');
145
- if (!aggregationDependencyExpression)
146
- aggregationDependencyExpression = '"N.A."';
147
- const aggregationDependencyEvaluator = (0, filtrex_1.compileExpression)(aggregationDependencyExpression, filtrexOptions);
160
+ const aggregationTrieRoot = {};
148
161
  return function (transaction) {
149
162
  if (filter) {
150
- const filterDependency = filterDependencyEvaluator(transaction);
151
- const filterExpression = filterExpressionCache[filterDependency];
152
- if (filterExpression == -1)
163
+ const filterLeaf = getTrieLeaf(filterTrieRoot, transaction, filterGranularity);
164
+ if (filterLeaf.filterResult === -1)
153
165
  return;
154
- if (!filterExpression) {
155
- filterExpressionCache[filterDependency] = filter(transaction) ? 1 : -1;
156
- if (filterExpressionCache[filterDependency] == -1)
166
+ if (filterLeaf.filterResult === undefined) {
167
+ filterLeaf.filterResult = filter(transaction) ? 1 : -1;
168
+ if (filterLeaf.filterResult === -1)
157
169
  return;
158
170
  }
159
171
  }
160
- const aggregationDependency = aggregationDependencyEvaluator(transaction);
161
- let aggregateKey = aggregationExpressionCache[aggregationDependency];
162
- if (!aggregateKey) {
163
- aggregateKey = aggregationEvaluator(transaction);
164
- aggregationExpressionCache[aggregationDependency] = aggregateKey;
172
+ const aggregateKeyLeaf = getTrieLeaf(aggregationTrieRoot, transaction, aggregationGranularity);
173
+ if (!aggregateKeyLeaf.aggregateKey) {
174
+ aggregateKeyLeaf.aggregateKey = aggregationEvaluator(transaction);
165
175
  }
176
+ const aggregateKey = aggregateKeyLeaf.aggregateKey;
166
177
  // Prepare joined values for derived fields
167
178
  if (transaction.type == types_1.transactionTypeE.SALE) {
168
179
  const sku = globalRelations['sku'][transaction.ean];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softwear/latestcollectioncore",
3
- "version": "1.0.161",
3
+ "version": "1.0.162",
4
4
  "description": "Core functions for LatestCollections applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -167,6 +167,31 @@ function prepareSubTotalAggKey(aggKey) {
167
167
  return aggKey.split('\t').slice(0, -1).join('\t').concat(' Σ')
168
168
  }
169
169
 
170
+ /** Same as filtrex std.coerceBoolean(customProp(...)) for bare join keys on the transaction */
171
+ function coerceJoinKeyValue(v: unknown): unknown {
172
+ return typeof v === 'boolean' ? +v : v
173
+ }
174
+
175
+ type AggregateKeyTrieNode = { aggregateKey?: string; children?: Map<unknown, AggregateKeyTrieNode> }
176
+ type FilterResultTrieNode = { filterResult?: 1 | -1; children?: Map<unknown, FilterResultTrieNode> }
177
+
178
+ function getTrieLeaf<T extends { children?: Map<unknown, T> }>(root: T, transaction: TransactionI, keys: string[]): T {
179
+ const nrKeys = keys.length
180
+ if (nrKeys === 0) return root
181
+ let node = root
182
+ for (let i = 0; i < nrKeys; i++) {
183
+ const k = coerceJoinKeyValue(transaction[keys[i] as keyof TransactionI] as unknown)
184
+ if (!node.children) node.children = new Map()
185
+ let next = node.children.get(k) as T | undefined
186
+ if (!next) {
187
+ next = {} as T
188
+ node.children.set(k, next)
189
+ }
190
+ node = next
191
+ }
192
+ return node
193
+ }
194
+
170
195
  /*
171
196
  * A factory function that returns a function that can be used to aggregate transactions
172
197
  * The factory function is called once for each aggregationExpression
@@ -200,46 +225,35 @@ function aggregator({
200
225
  subtotalsForOuterGrouper,
201
226
  parentGroupTotals,
202
227
  }: AggregatorFnI) {
203
- const filterExpressionCache = {}
204
-
205
- let filter
206
- let filterDependencyEvaluator
228
+ let filter: ((transaction: TransactionI) => unknown) | undefined
229
+ let filterGranularity: string[] = []
230
+ const filterTrieRoot: FilterResultTrieNode = {}
207
231
  if (filterExpression) {
208
232
  filter = compileExpression(filterExpression, filtrexOptions)
209
- // Maintain a cache of evaluated filterExpressions
210
- // The granularity of the cache is determined by the joined tables in the filterExpression
211
- const filterGranularity = Object.entries(globalJoinableTables)
233
+ filterGranularity = Object.entries(globalJoinableTables)
212
234
  .filter((join) => filterExpression.includes(join[0] + '.'))
213
235
  .map((join) => join[1])
214
- filterDependencyEvaluator = compileExpression(filterGranularity.join('+'), filtrexOptions)
215
236
  }
216
237
  const aggregationEvaluator = buildAggregationEvaluator(aggregationExpression)
217
- // Maintain a cache of evaluated aggregationExpressions
218
- // The granularity of the cache is determined by the joined tables in the aggregationExpression
219
- const aggregationExpressionCache = {}
220
238
  const aggregationGranularity = Object.entries(globalJoinableTables)
221
239
  .filter((join) => aggregationExpression.includes(join[0] + '.'))
222
240
  .map((join) => join[1])
241
+ const aggregationTrieRoot: AggregateKeyTrieNode = {}
223
242
 
224
- let aggregationDependencyExpression = aggregationGranularity.join('+')
225
- if (!aggregationDependencyExpression) aggregationDependencyExpression = '"N.A."'
226
- const aggregationDependencyEvaluator = compileExpression(aggregationDependencyExpression, filtrexOptions)
227
243
  return function (transaction: TransactionI): void {
228
244
  if (filter) {
229
- const filterDependency = filterDependencyEvaluator(transaction)
230
- const filterExpression = filterExpressionCache[filterDependency]
231
- if (filterExpression == -1) return
232
- if (!filterExpression) {
233
- filterExpressionCache[filterDependency] = filter(transaction) ? 1 : -1
234
- if (filterExpressionCache[filterDependency] == -1) return
245
+ const filterLeaf = getTrieLeaf<FilterResultTrieNode>(filterTrieRoot, transaction, filterGranularity)
246
+ if (filterLeaf.filterResult === -1) return
247
+ if (filterLeaf.filterResult === undefined) {
248
+ filterLeaf.filterResult = filter(transaction) ? 1 : -1
249
+ if (filterLeaf.filterResult === -1) return
235
250
  }
236
251
  }
237
- const aggregationDependency = aggregationDependencyEvaluator(transaction)
238
- let aggregateKey = aggregationExpressionCache[aggregationDependency]
239
- if (!aggregateKey) {
240
- aggregateKey = aggregationEvaluator(transaction)
241
- aggregationExpressionCache[aggregationDependency] = aggregateKey
252
+ const aggregateKeyLeaf = getTrieLeaf<AggregateKeyTrieNode>(aggregationTrieRoot, transaction, aggregationGranularity)
253
+ if (!aggregateKeyLeaf.aggregateKey) {
254
+ aggregateKeyLeaf.aggregateKey = aggregationEvaluator(transaction) as string
242
255
  }
256
+ const aggregateKey = aggregateKeyLeaf.aggregateKey as string
243
257
  // Prepare joined values for derived fields
244
258
  if (transaction.type == transactionTypeE.SALE) {
245
259
  const sku = globalRelations['sku'][transaction.ean]