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

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/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "homepage": "https://github.com/CartoDB/carto-api-client#readme",
9
9
  "author": "Don McCurdy <donmccurdy@carto.com>",
10
10
  "packageManager": "yarn@4.3.1",
11
- "version": "0.5.8-alpha-others-orderby.2",
11
+ "version": "0.5.8",
12
12
  "license": "MIT",
13
13
  "publishConfig": {
14
14
  "access": "public"
@@ -126,7 +126,7 @@
126
126
  "typescript": "~5.8.2",
127
127
  "typescript-eslint": "^8.26.1",
128
128
  "vite": "^6.2.2",
129
- "vitest": "3.1.4"
129
+ "vitest": "3.2.3"
130
130
  },
131
131
  "resolutions": {
132
132
  "@carto/api-client": "portal:./",
@@ -1,8 +1,4 @@
1
- import {
2
- cellToChildren as _cellToChildren,
3
- cellToTile,
4
- getResolution,
5
- } from 'quadbin';
1
+ import {cellToTile, getResolution, tileToCell} from 'quadbin';
6
2
  import type {RasterTile, SpatialFilter, Tile} from '../types.js';
7
3
  import type {FeatureData} from '../types-internal.js';
8
4
  import type {
@@ -56,7 +52,7 @@ export function tileFeaturesRaster({
56
52
 
57
53
  if (intersection === false) continue;
58
54
 
59
- const tileSortedCells = cellToChildrenSorted(parent, cellResolution);
55
+ const tileSortedCells = cellToChildrenRaster(parent, cellResolution);
60
56
 
61
57
  // For each pixel/cell within the spatial filter, create a FeatureData.
62
58
  // Order is row-major, starting from NW and ending at SE.
@@ -100,21 +96,28 @@ function isRasterTileVisible(tile: RasterTile): tile is Required<RasterTile> {
100
96
  }
101
97
 
102
98
  /**
103
- * For the raster format, children are sorted in row-major order, starting from
104
- * NW and ending at SE. Order returned by quadbin's cellToChildren() is not
105
- * defined (and not related to the raster format), so sort explicitly here.
99
+ * Alternative to `quadbin` module's `cellToChildren()` function, modified to
100
+ * return cells in row-major order, NW to SE, as stored in CARTO raster tiles.
101
+ * Sorting after computing cells is too slow.
106
102
  */
107
- function cellToChildrenSorted(parent: bigint, resolution: bigint): bigint[] {
108
- return _cellToChildren(parent, resolution).sort(
109
- (cellA: bigint, cellB: bigint) => {
110
- const tileA = cellToTile(cellA);
111
- const tileB = cellToTile(cellB);
112
- if (tileA.y !== tileB.y) {
113
- return tileA.y > tileB.y ? 1 : -1;
114
- }
115
- return tileA.x > tileB.x ? 1 : -1;
116
- }
117
- );
103
+ function cellToChildrenRaster(parent: bigint, resolution: bigint): bigint[] {
104
+ const parentTile = cellToTile(parent);
105
+
106
+ // 1. Calculate x/y/z of upper left pixel in raster tile.
107
+ const childZ = Number(resolution);
108
+ const blockSize = 2 ** (childZ - parentTile.z);
109
+ const childBaseX = parentTile.x * blockSize;
110
+ const childBaseY = parentTile.y * blockSize;
111
+
112
+ // 2. Iterate pixels in raster tile order; compute cell ID from base x/y.
113
+ const cells: bigint[] = [];
114
+ for (let i = 0, il = blockSize ** 2; i < il; i++) {
115
+ const x = childBaseX + (i % blockSize);
116
+ const y = childBaseY + Math.floor(i / blockSize);
117
+ cells.push(tileToCell({x, y, z: childZ}));
118
+ }
119
+
120
+ return cells;
118
121
  }
119
122
 
120
123
  /**
@@ -1,11 +1,7 @@
1
1
  import {aggregationFunctions, aggregate} from './aggregation.js';
2
2
  import type {AggregationType} from '../types.js';
3
3
  import type {FeatureData} from '../types-internal.js';
4
- import type {
5
- CategoryOrderBy,
6
- CategoryResponseEntry,
7
- CategoryResponseRaw,
8
- } from '../widget-sources/types.js';
4
+ import type {CategoryResponseRaw} from '../widget-sources/types.js';
9
5
 
10
6
  /** @privateRemarks Source: @carto/react-core */
11
7
  export function groupValuesByColumn({
@@ -15,7 +11,6 @@ export function groupValuesByColumn({
15
11
  keysColumn,
16
12
  operation,
17
13
  othersThreshold,
18
- orderBy = 'frequency_desc',
19
14
  }: {
20
15
  data: FeatureData[];
21
16
  valuesColumns?: string[];
@@ -23,7 +18,6 @@ export function groupValuesByColumn({
23
18
  keysColumn: string;
24
19
  operation: AggregationType;
25
20
  othersThreshold?: number;
26
- orderBy?: CategoryOrderBy;
27
21
  }): CategoryResponseRaw | null {
28
22
  if (Array.isArray(data) && data.length === 0) {
29
23
  return {rows: null};
@@ -60,7 +54,7 @@ export function groupValuesByColumn({
60
54
  name,
61
55
  value: targetOperation(value),
62
56
  }))
63
- .sort(getSorter(orderBy));
57
+ .sort((a, b) => b.value - a.value);
64
58
 
65
59
  if (othersThreshold && allCategories.length > othersThreshold) {
66
60
  const otherValue = allCategories
@@ -78,26 +72,3 @@ export function groupValuesByColumn({
78
72
  rows: allCategories,
79
73
  };
80
74
  }
81
-
82
- export function getSorter(
83
- orderBy: CategoryOrderBy
84
- ): (a: CategoryResponseEntry, b: CategoryResponseEntry) => number {
85
- switch (orderBy) {
86
- case 'frequency_asc':
87
- // 'value ASC, name ASC'
88
- return (a, b) => a.value - b.value || localeCompare(a.name, b.name);
89
- case 'frequency_desc':
90
- // 'value DESC, name ASC'
91
- return (a, b) => b.value - a.value || localeCompare(a.name, b.name);
92
- case 'alphabetical_asc':
93
- // 'name ASC, value DESC'
94
- return (a, b) => localeCompare(a.name, b.name) || b.value - a.value;
95
- case 'alphabetical_desc':
96
- // 'name DESC, value DESC'
97
- return (a, b) => localeCompare(b.name, a.name) || b.value - a.value;
98
- }
99
- }
100
-
101
- function localeCompare(a?: string | null, b?: string | null) {
102
- return (a ?? 'null').localeCompare(b ?? 'null');
103
- }
@@ -31,12 +31,6 @@ export interface BaseRequestOptions {
31
31
  filterOwner?: string;
32
32
  }
33
33
 
34
- export type CategoryOrderBy =
35
- | 'frequency_asc' // sort by aggregate value ascending, then by name ascending
36
- | 'frequency_desc' // sort by aggregate value descending, then by name ascending
37
- | 'alphabetical_asc' // sort by category name ascending, then by value descending
38
- | 'alphabetical_desc'; // sort by category name descending, then by value descending
39
-
40
34
  /**
41
35
  * Examples:
42
36
  * * population by state
@@ -67,11 +61,6 @@ export interface CategoryRequestOptions extends BaseRequestOptions {
67
61
  joinOperation?: 'count' | 'avg' | 'min' | 'max' | 'sum';
68
62
  /** Calculate `_carto_others` category for all categories after first N (N is threshold). */
69
63
  othersThreshold?: number;
70
- /**
71
- * Order categories by frequency or alphabetically.
72
- * @default 'frequency_desc'
73
- */
74
- orderBy?: CategoryOrderBy;
75
64
  /** Return raw result (CategoryResponseRaw). */
76
65
  rawResult?: boolean;
77
66
  }
@@ -220,7 +209,6 @@ export type FeaturesResponse = {rows: Record<string, unknown>[]};
220
209
  /** Response from {@link WidgetRemoteSource#getFormula}. */
221
210
  export type FormulaResponse = {value: number | null};
222
211
 
223
- /** Entry in the category widget response, see {@link WidgetRemoteSource#getCategories}. */
224
212
  export type CategoryResponseEntry = {name: string | null; value: number};
225
213
  /** Response from {@link WidgetRemoteSource#getCategories}. */
226
214
  export type CategoryResponse = CategoryResponseEntry[];
@@ -76,14 +76,8 @@ export abstract class WidgetRemoteSource<
76
76
  rawResult,
77
77
  ...params
78
78
  } = options;
79
- const {
80
- column,
81
- operation,
82
- operationColumn,
83
- operationExp,
84
- othersThreshold,
85
- orderBy,
86
- } = params;
79
+ const {column, operation, operationColumn, operationExp, othersThreshold} =
80
+ params;
87
81
 
88
82
  if (operation === AggregationTypes.Custom) {
89
83
  assert(operationExp, 'operationExp is required for custom operation');
@@ -102,7 +96,6 @@ export abstract class WidgetRemoteSource<
102
96
  operationExp,
103
97
  operationColumn: operationColumn || column,
104
98
  othersThreshold,
105
- orderBy,
106
99
  },
107
100
  opts: {signal, headers: this.props.headers},
108
101
  });
@@ -191,7 +191,6 @@ export class WidgetTilesetSourceImpl extends WidgetSource<WidgetTilesetSourcePro
191
191
  filterOwner,
192
192
  spatialFilter,
193
193
  othersThreshold,
194
- orderBy = 'frequency_desc',
195
194
  rawResult,
196
195
  }: CategoryRequestOptions): Promise<CategoryResponse> {
197
196
  const filteredFeatures = this._getFilteredFeatures(
@@ -213,7 +212,6 @@ export class WidgetTilesetSourceImpl extends WidgetSource<WidgetTilesetSourcePro
213
212
  keysColumn: column,
214
213
  operation,
215
214
  othersThreshold,
216
- orderBy,
217
215
  });
218
216
 
219
217
  if (rawResult) {