@bicharts/chart-host 0.1.7 → 0.1.9

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.
@@ -1,4 +1,9 @@
1
1
  /*! @bicharts/chart-host — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; Natural Earth and US Census/TIGER (public domain). Full text: NOTICE in this package. */
2
+ import {
3
+ NA_ISO3,
4
+ NA_LAT_MAX,
5
+ clipFeatureToLat
6
+ } from "./chunk-X7PWWEGM.mjs";
2
7
 
3
8
  // src/contract.ts
4
9
  var HOST_CONTRACT_VERSION = "1.0.0";
@@ -75,8 +80,11 @@ function resolveOptions(p) {
75
80
  }
76
81
 
77
82
  // src/geoLazy.ts
78
- var cache = /* @__PURE__ */ new Map();
79
- function assetFor(geoKind) {
83
+ var byKind = /* @__PURE__ */ new Map();
84
+ var byAsset = /* @__PURE__ */ new Map();
85
+ var _na;
86
+ function geoAssetFor(geoKind) {
87
+ if (!geoKind) return null;
80
88
  const k = geoKind.toLowerCase();
81
89
  if (/^country/.test(k)) return "world";
82
90
  if (k === "north-america") return "world";
@@ -84,37 +92,55 @@ function assetFor(geoKind) {
84
92
  if (k === "us-zip5") return "us-zip3";
85
93
  return null;
86
94
  }
95
+ function northAmerica(world) {
96
+ if (!_na) {
97
+ _na = {
98
+ type: "FeatureCollection",
99
+ features: world.features.filter((f) => NA_ISO3.has(f?.id) && f?.geometry && f.geometry.type !== "Point").map((f) => clipFeatureToLat(f, NA_LAT_MAX)).filter((f) => f !== null)
100
+ };
101
+ }
102
+ return _na;
103
+ }
104
+ function fromAssets(key) {
105
+ const asset = geoAssetFor(key);
106
+ if (!asset) return void 0;
107
+ const geo = byAsset.get(asset);
108
+ if (!geo) return void 0;
109
+ return key === "north-america" ? northAmerica(geo) : geo;
110
+ }
87
111
  async function loadGeo(geoKind) {
88
112
  if (!geoKind) return void 0;
89
113
  const key = geoKind.toLowerCase();
90
- if (cache.has(key)) return cache.get(key);
91
- const asset = assetFor(key);
114
+ const already = geoFromCache(key);
115
+ if (already) return already;
116
+ const asset = geoAssetFor(key);
92
117
  if (!asset) return void 0;
93
- let geo;
94
118
  if (asset === "us-states") {
95
- geo = (await import("./geoUsStates.generated-2ZZRXWWJ.mjs")).US_STATES;
119
+ byAsset.set(asset, (await import("./geoUsStates.generated-2ZZRXWWJ.mjs")).US_STATES);
96
120
  } else if (asset === "us-zip3") {
97
- geo = (await import("./geoUsZip3.generated-56OL4TTZ.mjs")).US_ZIP3;
121
+ byAsset.set(asset, (await import("./geoUsZip3.generated-56OL4TTZ.mjs")).US_ZIP3);
98
122
  } else {
99
- const { WORLD_110M } = await import("./geoWorld110m.generated-O2WOIRYY.mjs");
100
- if (key === "north-america") {
101
- const { NA_ISO3, NA_LAT_MAX, clipFeatureToLat } = await import("./geoNaRegions-SAINMZXG.mjs");
102
- geo = {
103
- type: "FeatureCollection",
104
- features: WORLD_110M.features.filter((f) => NA_ISO3.has(f?.id) && f?.geometry && f.geometry.type !== "Point").map((f) => clipFeatureToLat(f, NA_LAT_MAX)).filter((f) => f !== null)
105
- };
106
- } else {
107
- geo = WORLD_110M;
108
- }
123
+ byAsset.set(asset, (await import("./geoWorld110m.generated-O2WOIRYY.mjs")).WORLD_110M);
109
124
  }
110
- cache.set(key, geo);
111
- return geo;
125
+ return fromAssets(key);
112
126
  }
113
127
  function geoFromCache(geoKind) {
114
- return geoKind ? cache.get(geoKind.toLowerCase()) : void 0;
128
+ if (!geoKind) return void 0;
129
+ const key = geoKind.toLowerCase();
130
+ return byKind.get(key) ?? fromAssets(key);
115
131
  }
116
132
  function registerGeo(geoKind, geo) {
117
- if (geoKind && geo) cache.set(geoKind.toLowerCase(), geo);
133
+ if (geoKind && geo) byKind.set(geoKind.toLowerCase(), geo);
134
+ }
135
+ function registerGeoAsset(asset, geo) {
136
+ if (!asset || !geo) return;
137
+ byAsset.set(asset, geo);
138
+ if (asset === "world") _na = void 0;
139
+ }
140
+ function clearGeoCache() {
141
+ byKind.clear();
142
+ byAsset.clear();
143
+ _na = void 0;
118
144
  }
119
145
 
120
146
  // ../shape-core/dist/chunk-U5UKZYHK.mjs
@@ -749,7 +775,11 @@ function distinctNormalized(values) {
749
775
  function looksLikeCountryColumn(values) {
750
776
  const d = distinctNormalized(values);
751
777
  if (d.length === 0) return false;
752
- return d.every((v) => COUNTRY_IDS.has(v));
778
+ if (!d.every((v) => COUNTRY_IDS.has(v))) return false;
779
+ return d.some((v) => !resolveAdmin1(v));
780
+ }
781
+ function isAmbiguousCountryState(normalized) {
782
+ return COUNTRY_IDS.has(normalized) && !!resolveAdmin1(normalized);
753
783
  }
754
784
  function admin1MatchPct(values) {
755
785
  const d = distinctNormalized(values);
@@ -808,7 +838,10 @@ function resolvePointRoles(columns, rows, hint) {
808
838
  const hintedState = hint?.state;
809
839
  if (hintedState && colSet.has(hintedState)) {
810
840
  const vals = valuesOf(hintedState);
811
- if (looksLikeCountryColumn(vals)) {
841
+ const dvals = distinctNormalized(vals);
842
+ if (dvals.length > 0 && dvals.every(isAmbiguousCountryState)) {
843
+ refused.push(`state=${hintedState} (every value is "CA", which is both Canada and California)`);
844
+ } else if (looksLikeCountryColumn(vals)) {
812
845
  refused.push(`state=${hintedState} (every value is a country, not a state)`);
813
846
  if (!out.country) out.country = hintedState;
814
847
  } else if (admin1MatchPct(vals) < THRESHOLD_PCT) {
@@ -826,7 +859,9 @@ function resolvePointRoles(columns, rows, hint) {
826
859
  if (looksLikeCountryColumn(vals)) continue;
827
860
  const pct = admin1MatchPct(vals);
828
861
  if (pct < THRESHOLD_PCT) continue;
829
- if (distinctMatches(vals, (v) => !!resolveAdmin1(v)) < MIN_DISTINCT_BACKFILL) continue;
862
+ const hits = distinctMatches(vals, (v) => !!resolveAdmin1(v));
863
+ const unambiguous = distinctMatches(vals, (v) => !!resolveAdmin1(v) && !isAmbiguousCountryState(v));
864
+ if (hits < MIN_DISTINCT_BACKFILL && unambiguous < 1) continue;
830
865
  if (!best || pct > best.pct) best = { name: c, pct };
831
866
  }
832
867
  if (best) {
@@ -1384,12 +1419,15 @@ export {
1384
1419
  COLOR_SCALE_SELF_CLAMP_PCT_MIN,
1385
1420
  COLOR_SCALE_SELF_CLAMP_PCT_MAX,
1386
1421
  resolveOptions,
1422
+ geoAssetFor,
1387
1423
  loadGeo,
1388
1424
  geoFromCache,
1389
1425
  registerGeo,
1426
+ registerGeoAsset,
1427
+ clearGeoCache,
1390
1428
  buildRenderPayload,
1391
1429
  createMarkResolver,
1392
1430
  compileRenderFn,
1393
1431
  createChartHost
1394
1432
  };
1395
- //# sourceMappingURL=chunk-DF5QUB2N.mjs.map
1433
+ //# sourceMappingURL=chunk-3NDJV3DA.mjs.map