@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.
@@ -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,40 @@ 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
+ return {
7059
+ rows: allCategories,
7060
+ metadata: {
7061
+ others: targetOperation(otherValue)
7062
+ }
7063
+ };
7064
+ }
7065
+ return {
7066
+ rows: allCategories
7067
+ };
7068
+ }
7069
+ function getSorter(orderBy) {
7070
+ switch (orderBy) {
7071
+ case "frequency_asc":
7072
+ return (a, b) => a.value - b.value || localeCompare(a.name, b.name);
7073
+ case "frequency_desc":
7074
+ return (a, b) => b.value - a.value || localeCompare(a.name, b.name);
7075
+ case "alphabetical_asc":
7076
+ return (a, b) => localeCompare(a.name, b.name) || b.value - a.value;
7077
+ case "alphabetical_desc":
7078
+ return (a, b) => localeCompare(b.name, a.name) || b.value - a.value;
7025
7079
  }
7026
- return [];
7080
+ }
7081
+ function localeCompare(a, b) {
7082
+ return (a ?? "null").localeCompare(b ?? "null");
7027
7083
  }
7028
7084
 
7029
7085
  // src/operations/groupByDate.ts
@@ -7597,7 +7653,10 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
7597
7653
  joinOperation,
7598
7654
  filters,
7599
7655
  filterOwner,
7600
- spatialFilter
7656
+ spatialFilter,
7657
+ othersThreshold,
7658
+ orderBy = "frequency_desc",
7659
+ rawResult
7601
7660
  }) {
7602
7661
  const filteredFeatures = this._getFilteredFeatures(
7603
7662
  spatialFilter,
@@ -7608,14 +7667,25 @@ var WidgetTilesetSourceImpl = class extends WidgetSource {
7608
7667
  return [];
7609
7668
  }
7610
7669
  assertColumn(this._features, column, operationColumn);
7611
- const groups = groupValuesByColumn({
7670
+ const result = groupValuesByColumn({
7612
7671
  data: filteredFeatures,
7613
7672
  valuesColumns: normalizeColumns(operationColumn || column),
7614
7673
  joinOperation,
7615
7674
  keysColumn: column,
7616
- operation: operation2
7675
+ operation: operation2,
7676
+ othersThreshold,
7677
+ orderBy
7617
7678
  });
7618
- return groups || [];
7679
+ if (rawResult) {
7680
+ return result;
7681
+ }
7682
+ if (!othersThreshold) {
7683
+ return result?.rows || [];
7684
+ }
7685
+ return [
7686
+ ...result?.rows || [],
7687
+ { name: OTHERS_CATEGORY_NAME, value: result?.metadata?.others }
7688
+ ];
7619
7689
  }
7620
7690
  async getScatter({
7621
7691
  xAxisColumn,
@@ -10408,6 +10478,7 @@ function hashBuckets(initialCount) {
10408
10478
  FEATURE_GEOM_PROPERTY,
10409
10479
  FilterType,
10410
10480
  OPACITY_MAP,
10481
+ OTHERS_CATEGORY_NAME,
10411
10482
  Provider,
10412
10483
  SOURCE_DEFAULTS,
10413
10484
  SchemaFieldType,
@@ -10458,6 +10529,7 @@ function hashBuckets(initialCount) {
10458
10529
  getLayerProps,
10459
10530
  getMaxMarkerSize,
10460
10531
  getSizeAccessor,
10532
+ getSorter,
10461
10533
  getSpatialIndexFromGeoColumn,
10462
10534
  getTextAccessor,
10463
10535
  groupValuesByColumn,