@emuanalytics/flow-rdf-core 4.1.8 → 4.3.0

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.
Files changed (27) hide show
  1. package/dist/index.d.mts +322 -4
  2. package/dist/index.d.ts +322 -4
  3. package/dist/index.mjs +6 -0
  4. package/dist/lib/map/flow-core-map.mjs +120 -13
  5. package/dist/lib/map/plugins/index.d.mts +252 -223
  6. package/dist/lib/map/plugins/index.d.ts +252 -223
  7. package/dist/lib/map/plugins/selection/selection.plugin.mjs +73 -7
  8. package/dist/lib/map/plugins/selection/source-selection.mjs +12 -0
  9. package/dist/lib/map-style/composite/builder.mjs +31 -0
  10. package/dist/lib/map-style/composite/editing.mjs +123 -0
  11. package/dist/lib/map-style/composite/expand.mjs +182 -0
  12. package/dist/lib/map-style/composite/stored-format.mjs +18 -0
  13. package/dist/lib/map-style/composite/validate.mjs +376 -0
  14. package/dist/lib/map-style/flow-style/builder.mjs +97 -0
  15. package/dist/lib/map-style/flow-style/data-driven.mjs +164 -0
  16. package/dist/lib/map-style/flow-style/expand.mjs +145 -0
  17. package/dist/lib/map-style/flow-style/literals.mjs +58 -0
  18. package/dist/lib/map-style/flow-style/stored-format.mjs +93 -0
  19. package/dist/lib/map-style/flow-style/validate.mjs +117 -0
  20. package/dist/lib/map-style/flow-style/values.mjs +249 -0
  21. package/dist/lib/map-style/map-data-source.mjs +9 -0
  22. package/dist/lib/map-style/map-layer.mjs +555 -17
  23. package/dist/lib/map-style/map-style.mjs +250 -39
  24. package/dist/package.json.mjs +1 -1
  25. package/dist/shared/{flow-rdf-core.1ee62df9.d.mts → flow-rdf-core.62a36d8b.d.mts} +1077 -234
  26. package/dist/shared/{flow-rdf-core.1ee62df9.d.ts → flow-rdf-core.62a36d8b.d.ts} +1077 -234
  27. package/package.json +1 -1
@@ -0,0 +1,376 @@
1
+ import * as maplibreSpec from '@emuanalytics/maplibre-gl-style-spec';
2
+ import { isPlainObject } from '../../utils/type-checks.mjs';
3
+ import { expressionParseError } from '../flow-style/literals.mjs';
4
+ import { validateFlowStyle } from '../flow-style/validate.mjs';
5
+ import { flowStyleVersion } from '../flow-style/values.mjs';
6
+ import { compositeLayerVersion } from './stored-format.mjs';
7
+ import { ReactiveExpression } from '../../expressions/reactive-expression.mjs';
8
+
9
+ var __defProp = Object.defineProperty;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __publicField = (obj, key, value) => {
12
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
13
+ return value;
14
+ };
15
+ const SPEC_KEYS = ["version", "id", "layers", "dataSources"];
16
+ const SUB_LAYER_KEYS = [
17
+ "id",
18
+ "type",
19
+ "source",
20
+ "filter",
21
+ "interactive",
22
+ "minZoom",
23
+ "maxZoom",
24
+ "style"
25
+ ];
26
+ const SUB_LAYER_KEYS_V1 = [...SUB_LAYER_KEYS, "above", "below"];
27
+ const FLOW_GEOMETRIES = ["circle", "line", "fill"];
28
+ const DATA_SOURCE_TYPES = ["vector", "geojson", "raster"];
29
+ const isString = (value) => typeof value === "string";
30
+ const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
31
+ const isBoolean = (value) => typeof value === "boolean";
32
+ const DATA_SOURCE_OPTIONS = {
33
+ featureId: [isString, "a string"],
34
+ limit: [isNumber, "a number"],
35
+ apiKey: [isString, "a string"],
36
+ cluster: [isBoolean, "a boolean"],
37
+ clusterRadius: [isNumber, "a number"],
38
+ clusterMaxZoom: [isNumber, "a number"],
39
+ selectionMode: [(value) => ["single", "multi", "none"].includes(value), "'single', 'multi' or 'none'"],
40
+ selectionState: [isString, "a string"],
41
+ selectionClear: [(value) => isBoolean(value) || value === "group", "a boolean or 'group'"],
42
+ selectionGroup: [isString, "a string"]
43
+ };
44
+ const DATA_SOURCE_KEYS = ["id", "type", "src", ...Object.keys(DATA_SOURCE_OPTIONS)];
45
+ function validateCompositeLayer(spec) {
46
+ try {
47
+ return validate(spec);
48
+ } catch (e) {
49
+ return [
50
+ { path: "", message: `The spec could not be read: ${e?.message ?? String(e)}`, severity: "error" }
51
+ ];
52
+ }
53
+ }
54
+ class Issues {
55
+ constructor() {
56
+ __publicField(this, "list", []);
57
+ }
58
+ error(path, message) {
59
+ this.list.push({ path, message, severity: "error" });
60
+ }
61
+ warning(path, message) {
62
+ this.list.push({ path, message, severity: "warning" });
63
+ }
64
+ unknownKeys(object, known, path) {
65
+ for (const key of Object.keys(object)) {
66
+ if (!known.includes(key)) {
67
+ this.warning(join(path, key), `Unknown key '${key}' is kept but not used.`);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ function validate(spec) {
73
+ const issues = new Issues();
74
+ if (!isObject(spec)) {
75
+ issues.error("", `A composite layer spec must be an object, not ${describe(spec)}.`);
76
+ return issues.list;
77
+ }
78
+ if (Object.keys(spec).length === 0) {
79
+ issues.error("", "The spec is empty: it needs `layers`. Nothing is rendered.");
80
+ return issues.list;
81
+ }
82
+ const version = compositeLayerVersion(spec);
83
+ if (version === void 0) {
84
+ issues.error(
85
+ "version",
86
+ `Unknown version ${describe(spec.version)}: expected 2, or none for version 1. Nothing is rendered.`
87
+ );
88
+ return issues.list;
89
+ }
90
+ issues.unknownKeys(spec, SPEC_KEYS, "");
91
+ const dataSourceIds = validateDataSources(version, spec.dataSources, issues);
92
+ const { layers } = spec;
93
+ if (layers === void 0) {
94
+ issues.error("layers", "The spec has no `layers`. Nothing is rendered.");
95
+ } else if (!Array.isArray(layers)) {
96
+ issues.error("layers", `\`layers\` must be an array, not ${describe(layers)}. Nothing is rendered.`);
97
+ } else if (layers.length === 0) {
98
+ issues.warning("layers", "`layers` is empty. Nothing is rendered.");
99
+ } else {
100
+ const layerIds = /* @__PURE__ */ new Set();
101
+ layers.forEach(
102
+ (layer, index) => validateSubLayer(version, layer, `layers[${index}]`, dataSourceIds, layerIds, issues)
103
+ );
104
+ }
105
+ return issues.list;
106
+ }
107
+ function validateDataSources(version, dataSources, issues) {
108
+ const ids = /* @__PURE__ */ new Set();
109
+ const added = /* @__PURE__ */ new Set();
110
+ if (dataSources === void 0) {
111
+ return { all: ids, added };
112
+ }
113
+ if (!Array.isArray(dataSources)) {
114
+ issues.error(
115
+ "dataSources",
116
+ `\`dataSources\` must be an array, not ${describe(dataSources)}. No data sources are added.`
117
+ );
118
+ return { all: ids, added };
119
+ }
120
+ dataSources.forEach((source, index) => {
121
+ const path = `dataSources[${index}]`;
122
+ if (!isObject(source)) {
123
+ issues.error(path, `A data source must be an object, not ${describe(source)}. It isn't added.`);
124
+ return;
125
+ }
126
+ const errorCount = issues.list.length;
127
+ if (!isStringFull(source.id)) {
128
+ issues.error(join(path, "id"), "A data source needs an `id` (a string). It isn't added.");
129
+ } else if (ids.has(source.id)) {
130
+ issues.error(join(path, "id"), `Duplicate data source id '${source.id}'. Only the first is used.`);
131
+ } else {
132
+ ids.add(source.id);
133
+ }
134
+ if (source.type === void 0 && version === 1) {
135
+ issues.warning(join(path, "type"), "The data source has no `type`: 'vector' is used, as in legacy.");
136
+ } else if (!DATA_SOURCE_TYPES.includes(source.type)) {
137
+ issues.error(
138
+ join(path, "type"),
139
+ `Unknown data source type ${describe(source.type)}: expected 'vector', 'geojson' or 'raster'. It isn't added.`
140
+ );
141
+ }
142
+ if (!isStringFull(source.src)) {
143
+ issues.error(
144
+ join(path, "src"),
145
+ "A data source needs a `src`: a dataset ID, tile source ID or URL. It isn't added."
146
+ );
147
+ }
148
+ if (!issues.list.slice(errorCount).some((issue) => issue.severity === "error")) {
149
+ added.add(source.id);
150
+ }
151
+ for (const [key, [check, expected]] of Object.entries(DATA_SOURCE_OPTIONS)) {
152
+ if (source[key] !== void 0 && !check(source[key])) {
153
+ issues.error(join(path, key), `\`${key}\` must be ${expected}, not ${describe(source[key])}. It isn't used.`);
154
+ }
155
+ }
156
+ if (source.selectionMode === "single" || source.selectionMode === "multi") {
157
+ if (source.type === "raster") {
158
+ issues.error(
159
+ join(path, "selectionMode"),
160
+ "Selection isn't supported on a raster data source. The selection options aren't used."
161
+ );
162
+ } else if (!isStringFull(source.featureId)) {
163
+ issues.error(
164
+ join(path, "selectionMode"),
165
+ "Selection needs `featureId`, to tell features apart. The selection options aren't used."
166
+ );
167
+ }
168
+ }
169
+ issues.unknownKeys(source, DATA_SOURCE_KEYS, path);
170
+ });
171
+ return { all: ids, added };
172
+ }
173
+ function validateSubLayer(version, layer, path, dataSourceIds, layerIds, issues) {
174
+ if (!isObject(layer)) {
175
+ issues.error(path, `A sub-layer must be an object, not ${describe(layer)}. It isn't rendered.`);
176
+ return;
177
+ }
178
+ if (!isStringFull(layer.id)) {
179
+ issues.error(join(path, "id"), "A sub-layer needs an `id` (a string). It isn't rendered.");
180
+ } else if (layerIds.has(layer.id)) {
181
+ issues.error(join(path, "id"), `Duplicate sub-layer id '${layer.id}'. Only the first is rendered.`);
182
+ } else {
183
+ layerIds.add(layer.id);
184
+ }
185
+ if (!isStringFull(layer.source)) {
186
+ issues.error(join(path, "source"), "A sub-layer needs a `source` (a data source ID). It isn't rendered.");
187
+ } else if (!dataSourceIds.added.has(layer.source)) {
188
+ const where = dataSourceIds.all.has(layer.source) ? "is in `dataSources` but isn't added (see its errors)" : "isn't in `dataSources`";
189
+ issues.warning(
190
+ join(path, "source"),
191
+ `Data source '${layer.source}' ${where}. The sub-layer is rendered only if the application has added it to the map.`
192
+ );
193
+ }
194
+ validateFilter(layer.filter, join(path, "filter"), issues);
195
+ validateZoom(layer, "minZoom", 0, path, issues);
196
+ validateZoom(layer, "maxZoom", 18, path, issues);
197
+ if (version === 1) {
198
+ validateStyleV1(layer, path, issues);
199
+ } else {
200
+ validateStyleV2(layer, path, issues);
201
+ }
202
+ issues.unknownKeys(layer, version === 1 ? SUB_LAYER_KEYS_V1 : SUB_LAYER_KEYS, path);
203
+ }
204
+ function validateFilter(filter, path, issues) {
205
+ if (filter === void 0 || filter === "") {
206
+ return;
207
+ }
208
+ if (typeof filter !== "string") {
209
+ issues.error(path, `A filter must be a reactive expression string, not ${describe(filter)}. It isn't used.`);
210
+ return;
211
+ }
212
+ const parseError = expressionParseError(filter);
213
+ if (parseError) {
214
+ issues.error(path, `The filter \`${filter}\` does not parse: ${parseError}. The sub-layer isn't rendered.`);
215
+ }
216
+ }
217
+ function validateZoom(layer, key, defaultZoom, path, issues) {
218
+ if (layer[key] !== void 0 && !isNumber(layer[key])) {
219
+ issues.error(
220
+ join(path, key),
221
+ `\`${key}\` must be a number, not ${describe(layer[key])}. The default, ${defaultZoom}, applies.`
222
+ );
223
+ }
224
+ }
225
+ function validateStyleV1(layer, path, issues) {
226
+ const { type, style, interactive } = layer;
227
+ const stylePath = join(path, "style");
228
+ if (!isObject(style)) {
229
+ issues.error(
230
+ stylePath,
231
+ `A version 1 sub-layer's style must be \`{type: 'flow', \u2026}\` or \`{type: 'maplibre', style}\`, not ${describe(style)}. The sub-layer isn't rendered.`
232
+ );
233
+ return;
234
+ }
235
+ if (style.type === "flow") {
236
+ if (!FLOW_GEOMETRIES.includes(type)) {
237
+ issues.error(join(path, "type"), `${geometryProblemV1(type)} The sub-layer isn't rendered.`);
238
+ }
239
+ issues.list.push(...prefixIssues(validateFlowStyle(style), stylePath));
240
+ if (interactive !== void 0 && typeof interactive !== "boolean") {
241
+ issues.warning(
242
+ join(path, "interactive"),
243
+ `\`interactive\` must be a boolean, not ${describe(interactive)}. The default, true, applies, as in legacy.`
244
+ );
245
+ }
246
+ } else if (style.type === "maplibre") {
247
+ if (isMapLibreLayerType(type)) {
248
+ validateStyleProgram(type, style.style, join(stylePath, "style"), issues);
249
+ } else {
250
+ issues.error(
251
+ join(path, "type"),
252
+ `Unknown type ${describe(type)}: a MapLibre-style sub-layer needs a MapLibre layer type. The sub-layer isn't rendered.`
253
+ );
254
+ }
255
+ issues.unknownKeys(style, ["type", "style"], stylePath);
256
+ if (interactive !== void 0 && typeof interactive !== "boolean") {
257
+ issues.warning(
258
+ join(path, "interactive"),
259
+ `\`interactive\` must be a boolean, not ${describe(interactive)}. It is read as ${Boolean(interactive)}, as in legacy.`
260
+ );
261
+ }
262
+ } else {
263
+ issues.error(
264
+ join(stylePath, "type"),
265
+ `A version 1 sub-layer's style type must be 'flow' or 'maplibre', not ${describe(style.type)}. The sub-layer isn't rendered.`
266
+ );
267
+ }
268
+ }
269
+ function geometryProblemV1(type) {
270
+ if (type === "symbol") {
271
+ return "A version 1 flow style can't draw a `symbol` sub-layer: use a MapLibre style, `{type: 'maplibre', style}`.";
272
+ }
273
+ if (type === "flow") {
274
+ return "`type: 'flow'` needs a version 2 spec (`version: 2`). A version 1 sub-layer's `type` is its geometry.";
275
+ }
276
+ return `Unknown geometry ${describe(type)} for a version 1 flow style: expected 'circle', 'line' or 'fill'.`;
277
+ }
278
+ function validateStyleV2(layer, path, issues) {
279
+ const { type, style, interactive } = layer;
280
+ const stylePath = join(path, "style");
281
+ if (interactive !== void 0 && typeof interactive !== "boolean") {
282
+ issues.warning(
283
+ join(path, "interactive"),
284
+ `\`interactive\` must be a boolean, not ${describe(interactive)}. The default, true, applies.`
285
+ );
286
+ }
287
+ if (type === "flow") {
288
+ if (!isObject(style)) {
289
+ issues.error(
290
+ stylePath,
291
+ `A flow sub-layer's style must be a flow style, not ${describe(style)}. It isn't rendered.`
292
+ );
293
+ } else if (flowStyleVersion(style) === 1 && style.type === "flow") {
294
+ issues.error(
295
+ join(stylePath, "version"),
296
+ "A flow sub-layer takes a version 2 flow style, with `version: 2` and its geometry as `type`. Build one with `flowLayerStyle()`. The sub-layer isn't rendered."
297
+ );
298
+ } else {
299
+ issues.list.push(...prefixIssues(validateFlowStyle(style), stylePath));
300
+ }
301
+ } else if (isMapLibreLayerType(type)) {
302
+ if (typeof style === "string") {
303
+ validateStyleProgram(type, style, stylePath, issues);
304
+ } else {
305
+ issues.error(
306
+ stylePath,
307
+ `A '${type}' sub-layer's style must be a style program string, not ${describe(style)}; a flow style needs \`type: 'flow'\`. The sub-layer isn't rendered.`
308
+ );
309
+ }
310
+ } else {
311
+ issues.error(
312
+ join(path, "type"),
313
+ `Unknown type ${describe(type)}: expected 'flow' or a MapLibre layer type. The sub-layer isn't rendered.`
314
+ );
315
+ }
316
+ }
317
+ function validateStyleProgram(type, program, path, issues) {
318
+ if (typeof program !== "string") {
319
+ issues.error(path, `A style program must be a string, not ${describe(program)}. The sub-layer isn't rendered.`);
320
+ return;
321
+ }
322
+ let properties;
323
+ try {
324
+ properties = Object.keys(ReactiveExpression.parseProgram(program));
325
+ } catch (e) {
326
+ issues.error(
327
+ path,
328
+ `The style program does not parse: ${e?.message ?? String(e)}. The sub-layer isn't rendered.`
329
+ );
330
+ return;
331
+ }
332
+ const spec = maplibreSpec.latest;
333
+ for (const property of properties) {
334
+ if (!(property in spec[`paint_${type}`]) && !(property in spec[`layout_${type}`])) {
335
+ issues.error(path, `Unknown property '${property}' for a '${type}' layer. The sub-layer isn't rendered.`);
336
+ }
337
+ }
338
+ }
339
+ function isMapLibreLayerType(type) {
340
+ return typeof type === "string" && Object.prototype.hasOwnProperty.call(maplibreSpec.latest.layer.type.values, type);
341
+ }
342
+ function prefixIssues(issues, prefix) {
343
+ return issues.map((issue) => ({ ...issue, path: join(prefix, issue.path) }));
344
+ }
345
+ function join(path, key) {
346
+ if (!path) {
347
+ return key;
348
+ }
349
+ return key ? `${path}.${key}` : path;
350
+ }
351
+ function isObject(value) {
352
+ return isPlainObject(value) && !Array.isArray(value);
353
+ }
354
+ function isStringFull(value) {
355
+ return typeof value === "string" && value.length > 0;
356
+ }
357
+ function describe(value) {
358
+ if (value === null) {
359
+ return "null";
360
+ }
361
+ if (Array.isArray(value)) {
362
+ return "an array";
363
+ }
364
+ if (typeof value === "string") {
365
+ return `'${value}'`;
366
+ }
367
+ if (typeof value === "object") {
368
+ return "an object";
369
+ }
370
+ if (typeof value === "function") {
371
+ return "a function";
372
+ }
373
+ return String(value);
374
+ }
375
+
376
+ export { prefixIssues, validateCompositeLayer };
@@ -0,0 +1,97 @@
1
+ import { isPlainObject } from '../../utils/type-checks.mjs';
2
+ import { dataDrivenExpression } from './data-driven.mjs';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => {
7
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ return value;
9
+ };
10
+ function cloneStoredValue(value) {
11
+ if (Array.isArray(value)) {
12
+ return value.map((item) => cloneStoredValue(item));
13
+ }
14
+ if (isPlainObject(value)) {
15
+ const copy = {};
16
+ for (const key of Object.keys(value)) {
17
+ Object.defineProperty(copy, key, {
18
+ value: cloneStoredValue(value[key]),
19
+ enumerable: true,
20
+ writable: true,
21
+ configurable: true
22
+ });
23
+ }
24
+ return copy;
25
+ }
26
+ return value;
27
+ }
28
+ function isStyleExpression(value) {
29
+ return typeof value === "object" && value !== null && !!value.__styleExpression;
30
+ }
31
+ function isDataDrivenTable(value) {
32
+ if (!isPlainObject(value) || Array.isArray(value) || "type" in value) {
33
+ return false;
34
+ }
35
+ return "mode" in value || "attribute" in value || "values" in value;
36
+ }
37
+ function isFlowStyleExpression(value) {
38
+ return isPlainObject(value) && value.type === "expression";
39
+ }
40
+ function toStoredValueV2(value) {
41
+ if (isStyleExpression(value)) {
42
+ return { type: "expression", value: value.toString() };
43
+ }
44
+ if (isDataDrivenTable(value)) {
45
+ const dataDriven = cloneStoredValue(value);
46
+ return { type: "expression", value: dataDrivenExpression(dataDriven), dataDriven };
47
+ }
48
+ return cloneStoredValue(value);
49
+ }
50
+ class FlowStyleBuilder {
51
+ constructor(type, version) {
52
+ __publicField(this, "style");
53
+ this.style = { type, version };
54
+ }
55
+ fillColor(value) {
56
+ return this.set("fillColor", value);
57
+ }
58
+ strokeColor(value) {
59
+ return this.set("strokeColor", value);
60
+ }
61
+ strokeWidth(value) {
62
+ return this.set("strokeWidth", value);
63
+ }
64
+ radius(value) {
65
+ return this.set("radius", value);
66
+ }
67
+ opacity(value) {
68
+ return this.set("opacity", value);
69
+ }
70
+ labelText(value) {
71
+ return this.set("labelText", value);
72
+ }
73
+ labelColor(value) {
74
+ return this.set("labelColor", value);
75
+ }
76
+ labelHaloColor(value) {
77
+ return this.set("labelHaloColor", value);
78
+ }
79
+ labelSize(value) {
80
+ return this.set("labelSize", value);
81
+ }
82
+ visibility(value) {
83
+ return this.set("visibility", value);
84
+ }
85
+ build() {
86
+ return cloneStoredValue(this.style);
87
+ }
88
+ set(property, value) {
89
+ this.style[property] = toStoredValueV2(value);
90
+ return this;
91
+ }
92
+ }
93
+ function flowLayerStyle(type, options) {
94
+ return new FlowStyleBuilder(type, options?.version ?? 2);
95
+ }
96
+
97
+ export { cloneStoredValue, flowLayerStyle, isDataDrivenTable, isFlowStyleExpression, isStyleExpression, toStoredValueV2 };
@@ -0,0 +1,164 @@
1
+ import { isPlainObject } from '../../utils/type-checks.mjs';
2
+ import { isExactString, attributeExpression, quoteString, formatNumber } from './literals.mjs';
3
+
4
+ function expandDataDriven(dataDriven, path, format) {
5
+ const issues = [];
6
+ const problem = (at, message) => issues.push({ path: at, message, severity: format.severity });
7
+ if (!isPlainObject(dataDriven) || Array.isArray(dataDriven)) {
8
+ problem(path, "A data-driven table must be an object.");
9
+ return { expression: void 0, issues };
10
+ }
11
+ const table = dataDriven;
12
+ if (!Array.isArray(table.values)) {
13
+ problem(`${path}.values`, "A data-driven table needs a `values` array.");
14
+ return { expression: void 0, issues };
15
+ }
16
+ const rows = [];
17
+ let keyType;
18
+ let mixed = false;
19
+ table.values.forEach((row, index) => {
20
+ const rowPath = `${path}.values[${index}]`;
21
+ if (!Array.isArray(row) || row.length < 2 || row.length > 3) {
22
+ problem(rowPath, "A row must be an array of a key, a value and optional options.");
23
+ return;
24
+ }
25
+ const [key, , options] = row;
26
+ if (!(typeof key === "string" || typeof key === "number" && Number.isFinite(key))) {
27
+ problem(rowPath, "A row key must be a string or a number.");
28
+ return;
29
+ }
30
+ if (options !== void 0 && (!isPlainObject(options) || Array.isArray(options))) {
31
+ problem(rowPath, "Row options must be an object.");
32
+ return;
33
+ }
34
+ const type = typeof key;
35
+ if (keyType === void 0) {
36
+ keyType = type;
37
+ } else if (keyType !== type) {
38
+ mixed = true;
39
+ }
40
+ rows.push(row);
41
+ });
42
+ if (mixed) {
43
+ problem(`${path}.values`, "All row keys must be of one type: strings or numbers.");
44
+ }
45
+ const mode = format.mode(table.mode, keyType, `${path}.mode`, issues);
46
+ if (mode !== "off" && !(typeof table.attribute === "string" && table.attribute.length > 0)) {
47
+ problem(`${path}.attribute`, "A data-driven table needs an `attribute`: the feature property to read.");
48
+ } else if (mode !== "off" && !isExactString(table.attribute, '"')) {
49
+ problem(`${path}.attribute`, "This feature property name cannot be written in a reactive expression.");
50
+ }
51
+ if ((mode === "step" || mode === "interpolate") && keyType === "string") {
52
+ problem(`${path}.values`, `Keys in ${mode} mode must be numbers.`);
53
+ }
54
+ if (mode === "interpolate") {
55
+ const keys = rows.map(([key]) => key);
56
+ if (new Set(keys).size !== keys.length) {
57
+ problem(`${path}.values`, "Keys in interpolate mode must be distinct.");
58
+ }
59
+ if (keys.some((key) => typeof key === "number" && key < 0)) {
60
+ problem(`${path}.values`, "Keys in interpolate mode cannot be negative.");
61
+ }
62
+ }
63
+ if (mode === "match") {
64
+ rows.forEach(([key, , options], index) => {
65
+ if (typeof key === "string" && options?.regexp) {
66
+ try {
67
+ new RegExp(key);
68
+ } catch {
69
+ problem(`${path}.values[${index}]`, `'${key}' is not a valid regular expression.`);
70
+ }
71
+ }
72
+ });
73
+ }
74
+ if (!("default" in table) || table.default === void 0) {
75
+ problem(`${path}.default`, "A data-driven table needs a `default` value.");
76
+ }
77
+ if (hasProblem(issues, format.severity)) {
78
+ return { expression: void 0, issues };
79
+ }
80
+ const defaultValue = format.value(table.default, `${path}.default`, issues);
81
+ const writtenValues = rows.map(([, value], index) => format.value(value, `${path}.values[${index}]`, issues));
82
+ if (defaultValue === void 0 || writtenValues.some((value) => value === void 0)) {
83
+ return { expression: void 0, issues };
84
+ }
85
+ const written = rows.map(([key, , options], index) => [
86
+ key,
87
+ writtenValues[index],
88
+ options
89
+ ]);
90
+ if (mode === "off" || written.length === 0) {
91
+ return { expression: defaultValue, issues };
92
+ }
93
+ const attribute = attributeExpression(table.attribute);
94
+ switch (mode) {
95
+ case "match":
96
+ return { expression: matchExpression(attribute, written, defaultValue, format), issues };
97
+ case "step":
98
+ return { expression: stepExpression(attribute, written, defaultValue, format), issues };
99
+ case "interpolate":
100
+ return { expression: interpolateExpression(attribute, written, defaultValue, format), issues };
101
+ }
102
+ }
103
+ function hasProblem(issues, severity) {
104
+ return issues.some((issue) => issue.severity === severity);
105
+ }
106
+ function matchExpression(attribute, rows, defaultValue, format) {
107
+ const cases = rows.map(([key, value, options]) => {
108
+ if (typeof key === "number") {
109
+ return `${attribute} == ${format.number(key)}, ${value}`;
110
+ }
111
+ const operator = options?.regexp ? options.caseSensitive ? "~" : "~*" : options?.caseSensitive ? "==" : "=*";
112
+ return `${attribute} ${operator} ${format.key(key)}, ${value}`;
113
+ });
114
+ return `case(${cases.join(", ")}, ${defaultValue})`;
115
+ }
116
+ function stepExpression(attribute, rows, defaultValue, format) {
117
+ const cases = [...rows].sort((a, b) => b[0] - a[0]).map(([key, value]) => `${attribute} >= ${format.number(key)}, ${value}`);
118
+ return `case(${attribute} == null, ${defaultValue}, ${cases.join(", ")}, ${defaultValue})`;
119
+ }
120
+ function interpolateExpression(attribute, rows, defaultValue, format) {
121
+ const stops = [...rows].sort((a, b) => a[0] - b[0]).map(([key, value]) => `${format.number(key)}, ${value}`);
122
+ return `typeof(${attribute}) == 'number' ? interpolate(linear(), ${attribute}, ${stops.join(", ")}) : ${defaultValue}`;
123
+ }
124
+ function writeLiteralV2(value, path, issues, severity) {
125
+ if (typeof value === "string") {
126
+ if (!isExactString(value)) {
127
+ issues.push({ path, message: "This string cannot be written in a reactive expression.", severity });
128
+ return void 0;
129
+ }
130
+ return quoteString(value);
131
+ }
132
+ if (typeof value === "number" && Number.isFinite(value)) {
133
+ return formatNumber(value);
134
+ }
135
+ if (typeof value === "boolean") {
136
+ return String(value);
137
+ }
138
+ issues.push({ path, message: "Expected a string, a number or a boolean.", severity });
139
+ return void 0;
140
+ }
141
+ const V2_MODES = ["match", "step", "interpolate", "off"];
142
+ const DATA_DRIVEN_FORMAT_V2 = {
143
+ severity: "warning",
144
+ mode(mode, _keyType, path, issues) {
145
+ if (V2_MODES.includes(mode)) {
146
+ return mode;
147
+ }
148
+ issues.push({ path, message: "The mode must be `match`, `step`, `interpolate` or `off`.", severity: "warning" });
149
+ return "off";
150
+ },
151
+ value: (value, path, issues) => writeLiteralV2(value, path, issues, "warning"),
152
+ key: (key) => quoteString(key),
153
+ number: formatNumber
154
+ };
155
+ function dataDrivenExpression(dataDriven) {
156
+ const { expression } = expandDataDriven(dataDriven, "dataDriven", DATA_DRIVEN_FORMAT_V2);
157
+ if (expression !== void 0) {
158
+ return expression;
159
+ }
160
+ const fallback = isPlainObject(dataDriven) ? writeLiteralV2(dataDriven.default, "dataDriven.default", [], "warning") : void 0;
161
+ return fallback ?? "null";
162
+ }
163
+
164
+ export { DATA_DRIVEN_FORMAT_V2, dataDrivenExpression, expandDataDriven, writeLiteralV2 };