@carto/api-client 0.5.7 → 0.5.8-alpha-others-orderby.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.
package/build/worker.js CHANGED
@@ -5678,16 +5678,21 @@ function normalizeSortByOptions({
5678
5678
  });
5679
5679
  }
5680
5680
 
5681
+ // src/widget-sources/constants.ts
5682
+ var OTHERS_CATEGORY_NAME = "_carto_others";
5683
+
5681
5684
  // src/operations/groupBy.ts
5682
5685
  function groupValuesByColumn({
5683
5686
  data,
5684
5687
  valuesColumns,
5685
5688
  joinOperation,
5686
5689
  keysColumn,
5687
- operation: operation2
5690
+ operation: operation2,
5691
+ othersThreshold,
5692
+ orderBy = "frequency_desc"
5688
5693
  }) {
5689
5694
  if (Array.isArray(data) && data.length === 0) {
5690
- return null;
5695
+ return { rows: null };
5691
5696
  }
5692
5697
  const groups = data.reduce((accumulator, item) => {
5693
5698
  const group = item[keysColumn];
@@ -5702,13 +5707,44 @@ function groupValuesByColumn({
5702
5707
  return accumulator;
5703
5708
  }, /* @__PURE__ */ new Map());
5704
5709
  const targetOperation = aggregationFunctions[operation2];
5705
- if (targetOperation) {
5706
- return Array.from(groups).map(([name, value]) => ({
5707
- name,
5708
- value: targetOperation(value)
5709
- }));
5710
+ if (!targetOperation) {
5711
+ return { rows: [] };
5712
+ }
5713
+ const allCategories = Array.from(groups).map(([name, value]) => ({
5714
+ name,
5715
+ value: targetOperation(value)
5716
+ })).sort(getSorter(orderBy));
5717
+ if (othersThreshold && allCategories.length > othersThreshold) {
5718
+ const otherValue = allCategories.slice(othersThreshold).flatMap(({ name }) => groups.get(name));
5719
+ allCategories.push({
5720
+ name: OTHERS_CATEGORY_NAME,
5721
+ value: targetOperation(otherValue)
5722
+ });
5723
+ return {
5724
+ rows: allCategories,
5725
+ metadata: {
5726
+ others: targetOperation(otherValue)
5727
+ }
5728
+ };
5729
+ }
5730
+ return {
5731
+ rows: allCategories
5732
+ };
5733
+ }
5734
+ function getSorter(orderBy) {
5735
+ switch (orderBy) {
5736
+ case "frequency_asc":
5737
+ return (a, b) => a.value - b.value || localeCompare(a.name, b.name);
5738
+ case "frequency_desc":
5739
+ return (a, b) => b.value - a.value || localeCompare(a.name, b.name);
5740
+ case "alphabetical_asc":
5741
+ return (a, b) => localeCompare(a.name, b.name) || b.value - a.value;
5742
+ case "alphabetical_desc":
5743
+ return (a, b) => localeCompare(b.name, a.name) || b.value - a.value;
5710
5744
  }
5711
- return [];
5745
+ }
5746
+ function localeCompare(a, b) {
5747
+ return (a ?? "null").localeCompare(b ?? "null");
5712
5748
  }
5713
5749
 
5714
5750
  // src/utils/dateUtils.ts
@@ -6327,7 +6363,10 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
6327
6363
  joinOperation,
6328
6364
  filters,
6329
6365
  filterOwner,
6330
- spatialFilter
6366
+ spatialFilter,
6367
+ othersThreshold,
6368
+ orderBy = "frequency_desc",
6369
+ rawResult
6331
6370
  }) {
6332
6371
  const filteredFeatures = this._getFilteredFeatures(
6333
6372
  spatialFilter,
@@ -6338,14 +6377,25 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
6338
6377
  return [];
6339
6378
  }
6340
6379
  assertColumn(this._features, column, operationColumn);
6341
- const groups = groupValuesByColumn({
6380
+ const result = groupValuesByColumn({
6342
6381
  data: filteredFeatures,
6343
6382
  valuesColumns: normalizeColumns(operationColumn || column),
6344
6383
  joinOperation,
6345
6384
  keysColumn: column,
6346
- operation: operation2
6385
+ operation: operation2,
6386
+ othersThreshold,
6387
+ orderBy
6347
6388
  });
6348
- return groups || [];
6389
+ if (rawResult) {
6390
+ return result;
6391
+ }
6392
+ if (!othersThreshold) {
6393
+ return result?.rows || [];
6394
+ }
6395
+ return [
6396
+ ...result?.rows || [],
6397
+ { name: OTHERS_CATEGORY_NAME, value: result?.metadata?.others }
6398
+ ];
6349
6399
  }
6350
6400
  async getScatter({
6351
6401
  xAxisColumn,