@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.
@@ -20081,16 +20081,21 @@
20081
20081
  });
20082
20082
  }
20083
20083
 
20084
+ // src/widget-sources/constants.ts
20085
+ var OTHERS_CATEGORY_NAME = "_carto_others";
20086
+
20084
20087
  // src/operations/groupBy.ts
20085
20088
  function groupValuesByColumn({
20086
20089
  data,
20087
20090
  valuesColumns,
20088
20091
  joinOperation,
20089
20092
  keysColumn,
20090
- operation: operation2
20093
+ operation: operation2,
20094
+ othersThreshold,
20095
+ orderBy = "frequency_desc"
20091
20096
  }) {
20092
20097
  if (Array.isArray(data) && data.length === 0) {
20093
- return null;
20098
+ return { rows: null };
20094
20099
  }
20095
20100
  const groups = data.reduce((accumulator, item) => {
20096
20101
  const group = item[keysColumn];
@@ -20105,13 +20110,44 @@
20105
20110
  return accumulator;
20106
20111
  }, /* @__PURE__ */ new Map());
20107
20112
  const targetOperation = aggregationFunctions[operation2];
20108
- if (targetOperation) {
20109
- return Array.from(groups).map(([name, value]) => ({
20110
- name,
20111
- value: targetOperation(value)
20112
- }));
20113
+ if (!targetOperation) {
20114
+ return { rows: [] };
20115
+ }
20116
+ const allCategories = Array.from(groups).map(([name, value]) => ({
20117
+ name,
20118
+ value: targetOperation(value)
20119
+ })).sort(getSorter(orderBy));
20120
+ if (othersThreshold && allCategories.length > othersThreshold) {
20121
+ const otherValue = allCategories.slice(othersThreshold).flatMap(({ name }) => groups.get(name));
20122
+ allCategories.push({
20123
+ name: OTHERS_CATEGORY_NAME,
20124
+ value: targetOperation(otherValue)
20125
+ });
20126
+ return {
20127
+ rows: allCategories,
20128
+ metadata: {
20129
+ others: targetOperation(otherValue)
20130
+ }
20131
+ };
20132
+ }
20133
+ return {
20134
+ rows: allCategories
20135
+ };
20136
+ }
20137
+ function getSorter(orderBy) {
20138
+ switch (orderBy) {
20139
+ case "frequency_asc":
20140
+ return (a, b) => a.value - b.value || localeCompare(a.name, b.name);
20141
+ case "frequency_desc":
20142
+ return (a, b) => b.value - a.value || localeCompare(a.name, b.name);
20143
+ case "alphabetical_asc":
20144
+ return (a, b) => localeCompare(a.name, b.name) || b.value - a.value;
20145
+ case "alphabetical_desc":
20146
+ return (a, b) => localeCompare(b.name, a.name) || b.value - a.value;
20113
20147
  }
20114
- return [];
20148
+ }
20149
+ function localeCompare(a, b) {
20150
+ return (a ?? "null").localeCompare(b ?? "null");
20115
20151
  }
20116
20152
 
20117
20153
  // src/utils/dateUtils.ts
@@ -20730,7 +20766,10 @@
20730
20766
  joinOperation,
20731
20767
  filters,
20732
20768
  filterOwner,
20733
- spatialFilter
20769
+ spatialFilter,
20770
+ othersThreshold,
20771
+ orderBy = "frequency_desc",
20772
+ rawResult
20734
20773
  }) {
20735
20774
  const filteredFeatures = this._getFilteredFeatures(
20736
20775
  spatialFilter,
@@ -20741,14 +20780,25 @@
20741
20780
  return [];
20742
20781
  }
20743
20782
  assertColumn(this._features, column, operationColumn);
20744
- const groups = groupValuesByColumn({
20783
+ const result = groupValuesByColumn({
20745
20784
  data: filteredFeatures,
20746
20785
  valuesColumns: normalizeColumns(operationColumn || column),
20747
20786
  joinOperation,
20748
20787
  keysColumn: column,
20749
- operation: operation2
20788
+ operation: operation2,
20789
+ othersThreshold,
20790
+ orderBy
20750
20791
  });
20751
- return groups || [];
20792
+ if (rawResult) {
20793
+ return result;
20794
+ }
20795
+ if (!othersThreshold) {
20796
+ return result?.rows || [];
20797
+ }
20798
+ return [
20799
+ ...result?.rows || [],
20800
+ { name: OTHERS_CATEGORY_NAME, value: result?.metadata?.others }
20801
+ ];
20752
20802
  }
20753
20803
  async getScatter({
20754
20804
  xAxisColumn,