qvac-charts 0.1.2 → 0.2.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.
@@ -1,43 +1,33 @@
1
- const key = (s) => s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim();
1
+ export const WORLD = 'world';
2
+ export const key = (s) => s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim();
2
3
  const indexed = new WeakMap();
3
- function names(maps) {
4
- const known = indexed.get(maps);
4
+ function names(map) {
5
+ const known = indexed.get(map);
5
6
  if (known) {
6
7
  return known;
7
8
  }
8
- const spell = (entries) => new Map(entries.map(([name, id]) => [key(name), id]));
9
- const world = spell(maps.world.regions.flatMap((r) => [r.name, ...(maps.world.aliases[r.name] ?? [])].map((n) => [n, r.name])));
10
- const tiles = new Map(Object.entries(maps.tiles).map(([map, grid]) => [
11
- map,
12
- spell(grid.flatMap((t) => [
13
- [t.name, t.code],
14
- [t.code, t.code]
15
- ]))
16
- ]));
17
- const built = { world, tiles };
18
- indexed.set(maps, built);
9
+ const built = new Map();
10
+ for (const r of map.regions) {
11
+ for (const name of [r.name, ...(map.aliases[r.name] ?? [])]) {
12
+ if (!built.has(key(name))) {
13
+ built.set(key(name), r);
14
+ }
15
+ }
16
+ }
17
+ indexed.set(map, built);
19
18
  return built;
20
19
  }
21
- export const isTiles = (maps, map) => map in maps.tiles;
22
- export function region(maps, { map, label }) {
23
- const n = names(maps);
24
- return (isTiles(maps, map) ? n.tiles.get(map) : n.world)?.get(key(label ?? ''));
25
- }
26
- export function canonical(maps, place) {
27
- const id = region(maps, place);
28
- if (!isTiles(maps, place.map)) {
29
- return id;
30
- }
31
- return maps.tiles[place.map].find((t) => t.code === id)?.name;
20
+ export function geoMap(maps, map) {
21
+ return map === WORLD ? maps.world : maps.continents[map];
32
22
  }
33
- export function regionCount(maps, map) {
34
- return isTiles(maps, map) ? maps.tiles[map].length : maps.world.regions.length;
23
+ export function lookup(maps, { map, label }) {
24
+ return names(geoMap(maps, map)).get(key(label ?? ''));
35
25
  }
36
26
  export function mapFor(maps, labels) {
37
- const hits = (map) => labels.filter((label) => region(maps, { map, label })).length;
38
- let best = 'world';
39
- for (const map of Object.keys(maps.tiles)) {
40
- if (hits(map) > hits(best)) {
27
+ const hits = (map) => labels.filter((label) => lookup(maps, { map, label })).length;
28
+ let best = WORLD;
29
+ for (const map of Object.keys(maps.continents)) {
30
+ if (hits(map) >= hits(best)) {
41
31
  best = map;
42
32
  }
43
33
  }
@@ -1,5 +1,5 @@
1
1
  import type { Maps } from './places.ts';
2
- export type ChartType = 'ranked-bar' | 'bubbles' | 'treemap' | 'line' | 'column' | 'multi-line' | 'donut' | 'waffle' | 'stacked' | 'slope' | 'radial' | 'choropleth' | 'bubble-map' | 'tiles';
2
+ export type ChartType = 'ranked-bar' | 'bubbles' | 'treemap' | 'line' | 'column' | 'multi-line' | 'donut' | 'waffle' | 'stacked' | 'slope' | 'radial' | 'choropleth' | 'bubble-map';
3
3
  export interface Item {
4
4
  label: string;
5
5
  value: number;
@@ -1,3 +1,5 @@
1
- import type { Maps } from './lib/places.ts';
2
- export type { Maps };
1
+ import { type GeoMap, type GeoRegion, type Maps } from './lib/places.ts';
2
+ export type { GeoMap, GeoRegion, Maps };
3
3
  export declare const maps: Maps;
4
+ export declare const continents: ReadonlyArray<string>;
5
+ export declare function mapsFor(labels: ReadonlyArray<string>): Promise<Maps>;
package/dist-lib/maps.js CHANGED
@@ -1,7 +1,20 @@
1
1
  import { aliases } from './lib/maps/aliases.js';
2
- import { brazil } from './lib/maps/brazil.js';
2
+ import { loaders, members } from './lib/maps/continents/index.js';
3
3
  import { world } from './lib/maps/world.js';
4
- export const maps = {
5
- world: { ...world, aliases },
6
- tiles: { brazil }
7
- };
4
+ import { key } from './lib/places.js';
5
+ export const maps = { world: { ...world, aliases }, continents: {} };
6
+ export const continents = Object.keys(loaders);
7
+ const sets = new Map(Object.entries(members).map(([id, names]) => [id, new Set(names)]));
8
+ export async function mapsFor(labels) {
9
+ let best;
10
+ for (const [id, names] of sets) {
11
+ const hits = labels.filter((label) => names.has(key(label))).length;
12
+ if (hits > 0 && (!best || hits > best.hits)) {
13
+ best = { id, hits };
14
+ }
15
+ }
16
+ if (!best) {
17
+ return maps;
18
+ }
19
+ return { ...maps, continents: { [best.id]: await loaders[best.id]() } };
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qvac-charts",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Renders a small JSON chart spec into an SVG, on Bare and Node",
5
5
  "type": "module",
6
6
  "exports": {
@@ -32,6 +32,7 @@
32
32
  "test:generate": "brittle-make-test test/all.mjs test/*.ts",
33
33
  "build:lib": "node scripts/build-lib.mjs",
34
34
  "build:world": "node scripts/project-world.mjs lib/maps/world.ts",
35
+ "build:continents": "node scripts/project-continents.mjs",
35
36
  "snapshots:update": "node scripts/snapshots.mjs"
36
37
  },
37
38
  "devDependencies": {
@@ -44,6 +45,8 @@
44
45
  "prettier": "^3.9.6",
45
46
  "prettier-config-holepunch": "^2.0.0",
46
47
  "topojson-client": "3.1.0",
48
+ "topojson-server": "3.0.1",
49
+ "topojson-simplify": "3.0.3",
47
50
  "typescript": "^7.0.2",
48
51
  "world-atlas": "2.0.2"
49
52
  },
@@ -1,2 +0,0 @@
1
- import type { Tile } from '../places.ts';
2
- export declare const brazil: ReadonlyArray<Tile>;
@@ -1,29 +0,0 @@
1
- export const brazil = [
2
- { code: 'AC', name: 'Acre', col: 0, row: 2 },
3
- { code: 'AL', name: 'Alagoas', col: 5, row: 3 },
4
- { code: 'AP', name: 'Amapá', col: 2, row: 0 },
5
- { code: 'AM', name: 'Amazonas', col: 1, row: 1 },
6
- { code: 'BA', name: 'Bahia', col: 3, row: 3 },
7
- { code: 'CE', name: 'Ceará', col: 4, row: 1 },
8
- { code: 'DF', name: 'Distrito Federal', col: 2, row: 4 },
9
- { code: 'ES', name: 'Espírito Santo', col: 4, row: 4 },
10
- { code: 'GO', name: 'Goiás', col: 2, row: 3 },
11
- { code: 'MA', name: 'Maranhão', col: 3, row: 1 },
12
- { code: 'MT', name: 'Mato Grosso', col: 1, row: 3 },
13
- { code: 'MS', name: 'Mato Grosso do Sul', col: 1, row: 4 },
14
- { code: 'MG', name: 'Minas Gerais', col: 3, row: 4 },
15
- { code: 'PA', name: 'Pará', col: 2, row: 1 },
16
- { code: 'PB', name: 'Paraíba', col: 5, row: 2 },
17
- { code: 'PR', name: 'Paraná', col: 1, row: 5 },
18
- { code: 'PE', name: 'Pernambuco', col: 4, row: 2 },
19
- { code: 'PI', name: 'Piauí', col: 3, row: 2 },
20
- { code: 'RJ', name: 'Rio de Janeiro', col: 3, row: 5 },
21
- { code: 'RN', name: 'Rio Grande do Norte', col: 5, row: 1 },
22
- { code: 'RS', name: 'Rio Grande do Sul', col: 1, row: 7 },
23
- { code: 'RO', name: 'Rondônia', col: 1, row: 2 },
24
- { code: 'RR', name: 'Roraima', col: 1, row: 0 },
25
- { code: 'SC', name: 'Santa Catarina', col: 1, row: 6 },
26
- { code: 'SP', name: 'São Paulo', col: 2, row: 5 },
27
- { code: 'SE', name: 'Sergipe', col: 4, row: 3 },
28
- { code: 'TO', name: 'Tocantins', col: 2, row: 2 }
29
- ];