@carto/api-client 0.5.7 → 0.5.8-alpha-others-orderby.2

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.
package/build/worker.js CHANGED
@@ -5684,10 +5684,12 @@ function groupValuesByColumn({
5684
5684
  valuesColumns,
5685
5685
  joinOperation,
5686
5686
  keysColumn,
5687
- operation: operation2
5687
+ operation: operation2,
5688
+ othersThreshold,
5689
+ orderBy = "frequency_desc"
5688
5690
  }) {
5689
5691
  if (Array.isArray(data) && data.length === 0) {
5690
- return null;
5692
+ return { rows: null };
5691
5693
  }
5692
5694
  const groups = data.reduce((accumulator, item) => {
5693
5695
  const group = item[keysColumn];
@@ -5702,13 +5704,40 @@ function groupValuesByColumn({
5702
5704
  return accumulator;
5703
5705
  }, /* @__PURE__ */ new Map());
5704
5706
  const targetOperation = aggregationFunctions[operation2];
5705
- if (targetOperation) {
5706
- return Array.from(groups).map(([name, value]) => ({
5707
- name,
5708
- value: targetOperation(value)
5709
- }));
5707
+ if (!targetOperation) {
5708
+ return { rows: [] };
5710
5709
  }
5711
- return [];
5710
+ const allCategories = Array.from(groups).map(([name, value]) => ({
5711
+ name,
5712
+ value: targetOperation(value)
5713
+ })).sort(getSorter(orderBy));
5714
+ if (othersThreshold && allCategories.length > othersThreshold) {
5715
+ const otherValue = allCategories.slice(othersThreshold).flatMap(({ name }) => groups.get(name));
5716
+ return {
5717
+ rows: allCategories,
5718
+ metadata: {
5719
+ others: targetOperation(otherValue)
5720
+ }
5721
+ };
5722
+ }
5723
+ return {
5724
+ rows: allCategories
5725
+ };
5726
+ }
5727
+ function getSorter(orderBy) {
5728
+ switch (orderBy) {
5729
+ case "frequency_asc":
5730
+ return (a, b) => a.value - b.value || localeCompare(a.name, b.name);
5731
+ case "frequency_desc":
5732
+ return (a, b) => b.value - a.value || localeCompare(a.name, b.name);
5733
+ case "alphabetical_asc":
5734
+ return (a, b) => localeCompare(a.name, b.name) || b.value - a.value;
5735
+ case "alphabetical_desc":
5736
+ return (a, b) => localeCompare(b.name, a.name) || b.value - a.value;
5737
+ }
5738
+ }
5739
+ function localeCompare(a, b) {
5740
+ return (a ?? "null").localeCompare(b ?? "null");
5712
5741
  }
5713
5742
 
5714
5743
  // src/utils/dateUtils.ts
@@ -6213,6 +6242,9 @@ function getApplicableFilters(owner, filters) {
6213
6242
  return applicableFilters;
6214
6243
  }
6215
6244
 
6245
+ // src/widget-sources/constants.ts
6246
+ var OTHERS_CATEGORY_NAME = "_carto_others";
6247
+
6216
6248
  // src/widget-sources/widget-tileset-source-impl.ts
6217
6249
  var WidgetTilesetSourceImpl = class extends WidgetSource {
6218
6250
  constructor() {
@@ -6327,7 +6359,10 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
6327
6359
  joinOperation,
6328
6360
  filters,
6329
6361
  filterOwner,
6330
- spatialFilter
6362
+ spatialFilter,
6363
+ othersThreshold,
6364
+ orderBy = "frequency_desc",
6365
+ rawResult
6331
6366
  }) {
6332
6367
  const filteredFeatures = this._getFilteredFeatures(
6333
6368
  spatialFilter,
@@ -6338,14 +6373,25 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
6338
6373
  return [];
6339
6374
  }
6340
6375
  assertColumn(this._features, column, operationColumn);
6341
- const groups = groupValuesByColumn({
6376
+ const result = groupValuesByColumn({
6342
6377
  data: filteredFeatures,
6343
6378
  valuesColumns: normalizeColumns(operationColumn || column),
6344
6379
  joinOperation,
6345
6380
  keysColumn: column,
6346
- operation: operation2
6381
+ operation: operation2,
6382
+ othersThreshold,
6383
+ orderBy
6347
6384
  });
6348
- return groups || [];
6385
+ if (rawResult) {
6386
+ return result;
6387
+ }
6388
+ if (!othersThreshold) {
6389
+ return result?.rows || [];
6390
+ }
6391
+ return [
6392
+ ...result?.rows || [],
6393
+ { name: OTHERS_CATEGORY_NAME, value: result?.metadata?.others }
6394
+ ];
6349
6395
  }
6350
6396
  async getScatter({
6351
6397
  xAxisColumn,