@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.
@@ -108,6 +108,7 @@ __export(src_exports, {
108
108
  FEATURE_GEOM_PROPERTY: () => FEATURE_GEOM_PROPERTY,
109
109
  FilterType: () => FilterType,
110
110
  OPACITY_MAP: () => OPACITY_MAP,
111
+ OTHERS_CATEGORY_NAME: () => OTHERS_CATEGORY_NAME,
111
112
  Provider: () => Provider,
112
113
  SOURCE_DEFAULTS: () => SOURCE_DEFAULTS,
113
114
  SchemaFieldType: () => SchemaFieldType,
@@ -158,6 +159,7 @@ __export(src_exports, {
158
159
  getLayerProps: () => getLayerProps,
159
160
  getMaxMarkerSize: () => getMaxMarkerSize,
160
161
  getSizeAccessor: () => getSizeAccessor,
162
+ getSorter: () => getSorter,
161
163
  getSpatialIndexFromGeoColumn: () => getSpatialIndexFromGeoColumn,
162
164
  getTextAccessor: () => getTextAccessor,
163
165
  groupValuesByColumn: () => groupValuesByColumn,
@@ -6551,6 +6553,10 @@ function getApplicableFilters(owner, filters) {
6551
6553
  return applicableFilters;
6552
6554
  }
6553
6555
 
6556
+ // src/widget-sources/constants.ts
6557
+ init_cjs_shims();
6558
+ var OTHERS_CATEGORY_NAME = "_carto_others";
6559
+
6554
6560
  // src/widget-sources/widget-remote-source.ts
6555
6561
  var WidgetRemoteSource = class extends WidgetSource {
6556
6562
  _getModelSource(filters, filterOwner) {
@@ -6574,13 +6580,21 @@ var WidgetRemoteSource = class extends WidgetSource {
6574
6580
  filterOwner,
6575
6581
  spatialFilter,
6576
6582
  spatialFiltersMode,
6583
+ rawResult,
6577
6584
  ...params
6578
6585
  } = options;
6579
- const { column, operation: operation2, operationColumn, operationExp } = params;
6586
+ const {
6587
+ column,
6588
+ operation: operation2,
6589
+ operationColumn,
6590
+ operationExp,
6591
+ othersThreshold,
6592
+ orderBy
6593
+ } = params;
6580
6594
  if (operation2 === AggregationTypes.Custom) {
6581
6595
  assert2(operationExp, "operationExp is required for custom operation");
6582
6596
  }
6583
- return executeModel({
6597
+ const result = await executeModel({
6584
6598
  model: "category",
6585
6599
  source: {
6586
6600
  ...this.getModelSource(filters, filterOwner),
@@ -6591,10 +6605,23 @@ var WidgetRemoteSource = class extends WidgetSource {
6591
6605
  column,
6592
6606
  operation: operation2,
6593
6607
  operationExp,
6594
- operationColumn: operationColumn || column
6608
+ operationColumn: operationColumn || column,
6609
+ othersThreshold,
6610
+ orderBy
6595
6611
  },
6596
6612
  opts: { signal, headers: this.props.headers }
6597
- }).then((res) => normalizeObjectKeys(res.rows));
6613
+ });
6614
+ const normalizedRows = normalizeObjectKeys(result.rows || []);
6615
+ if (rawResult) {
6616
+ return result;
6617
+ }
6618
+ if (!othersThreshold) {
6619
+ return normalizedRows;
6620
+ }
6621
+ return [
6622
+ ...normalizedRows,
6623
+ { name: OTHERS_CATEGORY_NAME, value: result?.metadata?.others }
6624
+ ];
6598
6625
  }
6599
6626
  async getFeatures(options) {
6600
6627
  const {
@@ -6999,10 +7026,12 @@ function groupValuesByColumn({
6999
7026
  valuesColumns,
7000
7027
  joinOperation,
7001
7028
  keysColumn,
7002
- operation: operation2
7029
+ operation: operation2,
7030
+ othersThreshold,
7031
+ orderBy = "frequency_desc"
7003
7032
  }) {
7004
7033
  if (Array.isArray(data) && data.length === 0) {
7005
- return null;
7034
+ return { rows: null };
7006
7035
  }
7007
7036
  const groups = data.reduce((accumulator, item) => {
7008
7037
  const group2 = item[keysColumn];
@@ -7017,13 +7046,44 @@ function groupValuesByColumn({
7017
7046
  return accumulator;
7018
7047
  }, /* @__PURE__ */ new Map());
7019
7048
  const targetOperation = aggregationFunctions[operation2];
7020
- if (targetOperation) {
7021
- return Array.from(groups).map(([name, value]) => ({
7022
- name,
7023
- value: targetOperation(value)
7024
- }));
7049
+ if (!targetOperation) {
7050
+ return { rows: [] };
7051
+ }
7052
+ const allCategories = Array.from(groups).map(([name, value]) => ({
7053
+ name,
7054
+ value: targetOperation(value)
7055
+ })).sort(getSorter(orderBy));
7056
+ if (othersThreshold && allCategories.length > othersThreshold) {
7057
+ const otherValue = allCategories.slice(othersThreshold).flatMap(({ name }) => groups.get(name));
7058
+ allCategories.push({
7059
+ name: OTHERS_CATEGORY_NAME,
7060
+ value: targetOperation(otherValue)
7061
+ });
7062
+ return {
7063
+ rows: allCategories,
7064
+ metadata: {
7065
+ others: targetOperation(otherValue)
7066
+ }
7067
+ };
7068
+ }
7069
+ return {
7070
+ rows: allCategories
7071
+ };
7072
+ }
7073
+ function getSorter(orderBy) {
7074
+ switch (orderBy) {
7075
+ case "frequency_asc":
7076
+ return (a, b) => a.value - b.value || localeCompare(a.name, b.name);
7077
+ case "frequency_desc":
7078
+ return (a, b) => b.value - a.value || localeCompare(a.name, b.name);
7079
+ case "alphabetical_asc":
7080
+ return (a, b) => localeCompare(a.name, b.name) || b.value - a.value;
7081
+ case "alphabetical_desc":
7082
+ return (a, b) => localeCompare(b.name, a.name) || b.value - a.value;
7025
7083
  }
7026
- return [];
7084
+ }
7085
+ function localeCompare(a, b) {
7086
+ return (a ?? "null").localeCompare(b ?? "null");
7027
7087
  }
7028
7088
 
7029
7089
  // src/operations/groupByDate.ts
@@ -7597,7 +7657,10 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
7597
7657
  joinOperation,
7598
7658
  filters,
7599
7659
  filterOwner,
7600
- spatialFilter
7660
+ spatialFilter,
7661
+ othersThreshold,
7662
+ orderBy = "frequency_desc",
7663
+ rawResult
7601
7664
  }) {
7602
7665
  const filteredFeatures = this._getFilteredFeatures(
7603
7666
  spatialFilter,
@@ -7608,14 +7671,25 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
7608
7671
  return [];
7609
7672
  }
7610
7673
  assertColumn(this._features, column, operationColumn);
7611
- const groups = groupValuesByColumn({
7674
+ const result = groupValuesByColumn({
7612
7675
  data: filteredFeatures,
7613
7676
  valuesColumns: normalizeColumns(operationColumn || column),
7614
7677
  joinOperation,
7615
7678
  keysColumn: column,
7616
- operation: operation2
7679
+ operation: operation2,
7680
+ othersThreshold,
7681
+ orderBy
7617
7682
  });
7618
- return groups || [];
7683
+ if (rawResult) {
7684
+ return result;
7685
+ }
7686
+ if (!othersThreshold) {
7687
+ return result?.rows || [];
7688
+ }
7689
+ return [
7690
+ ...result?.rows || [],
7691
+ { name: OTHERS_CATEGORY_NAME, value: result?.metadata?.others }
7692
+ ];
7619
7693
  }
7620
7694
  async getScatter({
7621
7695
  xAxisColumn,
@@ -10408,6 +10482,7 @@ function hashBuckets(initialCount) {
10408
10482
  FEATURE_GEOM_PROPERTY,
10409
10483
  FilterType,
10410
10484
  OPACITY_MAP,
10485
+ OTHERS_CATEGORY_NAME,
10411
10486
  Provider,
10412
10487
  SOURCE_DEFAULTS,
10413
10488
  SchemaFieldType,
@@ -10458,6 +10533,7 @@ function hashBuckets(initialCount) {
10458
10533
  getLayerProps,
10459
10534
  getMaxMarkerSize,
10460
10535
  getSizeAccessor,
10536
+ getSorter,
10461
10537
  getSpatialIndexFromGeoColumn,
10462
10538
  getTextAccessor,
10463
10539
  groupValuesByColumn,