@meterapp/vehicle-db 2.72.0 → 2.74.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.
@@ -130,7 +130,15 @@ export interface AppearancePackageQuery {
130
130
  makeId: number;
131
131
  modelId: number;
132
132
  year: number;
133
+ /** The market (ISO 3166-1 alpha-2) to answer for. Without it, only packages with no market scope. Ignored with `anyMarket`. */
133
134
  market?: string;
135
+ /**
136
+ * Every package offered on the row in the year in any market, in one call: the union, by `specificationId`,
137
+ * of the call without a market and the call for every market. A package is included when its specification
138
+ * and its exterior are both unscoped, only one of them is market-scoped (to at least one market), or both
139
+ * are scoped and their market lists share a market. `anyMarket: true` takes precedence: `market` is ignored.
140
+ */
141
+ anyMarket?: boolean;
134
142
  }
135
143
  /** The stable slug of a package name: lower-case kebab, accents and punctuation dropped. */
136
144
  export declare function appearancePackageSlug(name: string): string;
@@ -139,5 +147,7 @@ export declare function packageVisual(graph: Pick<IdentityGraph, "identities">,
139
147
  /**
140
148
  * The appearance packages offered on a catalog vehicle in a year. A market-scoped package is returned
141
149
  * only when `market` is given and in its scope; with no market, only packages without a market scope.
150
+ * With `anyMarket: true`, every package offered in any market or none (see `AppearancePackageQuery.anyMarket`);
151
+ * `market` is then ignored. Indexes are cached per graph object, so treat a graph as read-only once queried.
142
152
  */
143
153
  export declare function listAppearancePackages(graph: IdentityGraph, query: AppearancePackageQuery): AppearancePackage[];
package/dist/identity.js CHANGED
@@ -38,25 +38,67 @@ function appearancePackageSlug(name) {
38
38
  function packageVisual(graph, specificationId) {
39
39
  return graph.identities.find(n => n.kind === "visual" && n.parentId === specificationId && n.exteriorDifferences?.length);
40
40
  }
41
+ const packageIndexes = new WeakMap();
42
+ /** Built once per graph object; rebuilt when its identity or link arrays are replaced or change length. */
43
+ function packageIndex(graph) {
44
+ const cached = packageIndexes.get(graph);
45
+ if (cached && cached.identities === graph.identities && cached.identityCount === graph.identities.length &&
46
+ cached.catalogLinks === graph.catalogLinks && cached.linkCount === graph.catalogLinks.length)
47
+ return cached;
48
+ const nodes = new Map(graph.identities.map(n => [n.id, n]));
49
+ const offeredOn = new Map();
50
+ for (const link of graph.catalogLinks) {
51
+ if (link.relation !== "offered-on")
52
+ continue;
53
+ const key = `${link.makeId}|${link.modelId}|${link.year}`;
54
+ const list = offeredOn.get(key);
55
+ if (list)
56
+ list.push(link);
57
+ else
58
+ offeredOn.set(key, [link]);
59
+ }
60
+ const visuals = new Map();
61
+ for (const n of graph.identities) {
62
+ if (n.kind === "visual" && n.parentId !== undefined && n.exteriorDifferences?.length && !visuals.has(n.parentId))
63
+ visuals.set(n.parentId, n);
64
+ }
65
+ const index = {
66
+ identities: graph.identities, identityCount: graph.identities.length,
67
+ catalogLinks: graph.catalogLinks, linkCount: graph.catalogLinks.length, nodes, offeredOn, visuals,
68
+ };
69
+ packageIndexes.set(graph, index);
70
+ return index;
71
+ }
72
+ /** Whether some single market, or no market, is in both scopes (what `anyMarket` asks). */
73
+ function marketsMeet(a, b) {
74
+ if (!a.markets)
75
+ return !b.markets || b.markets.length > 0;
76
+ if (!b.markets)
77
+ return a.markets.length > 0;
78
+ return a.markets.some(m => b.markets.includes(m));
79
+ }
41
80
  /**
42
81
  * The appearance packages offered on a catalog vehicle in a year. A market-scoped package is returned
43
82
  * only when `market` is given and in its scope; with no market, only packages without a market scope.
83
+ * With `anyMarket: true`, every package offered in any market or none (see `AppearancePackageQuery.anyMarket`);
84
+ * `market` is then ignored. Indexes are cached per graph object, so treat a graph as read-only once queried.
44
85
  */
45
86
  function listAppearancePackages(graph, query) {
46
- const { makeId, modelId, year, market } = query;
87
+ const { makeId, modelId, year, market, anyMarket } = query;
47
88
  if (![makeId, modelId, year].every(Number.isInteger))
48
89
  return [];
49
- const nodes = new Map(graph.identities.map(n => [n.id, n]));
90
+ const { nodes, offeredOn, visuals } = packageIndex(graph);
50
91
  const inMarkets = (scope) => !scope.markets || (market !== undefined && scope.markets.includes(market));
92
+ const marketsMatch = anyMarket === true ? marketsMeet : (spec, visual) => inMarkets(spec) && inMarkets(visual);
51
93
  const result = new Map();
52
- for (const link of graph.catalogLinks) {
53
- if (link.relation !== "offered-on" || link.makeId !== makeId || link.modelId !== modelId || link.year !== year || result.has(link.identityId))
94
+ for (const link of offeredOn.get(`${makeId}|${modelId}|${year}`) ?? []) {
95
+ if (result.has(link.identityId))
54
96
  continue;
55
97
  const spec = nodes.get(link.identityId);
56
- const visual = spec && packageVisual(graph, spec.id);
98
+ const visual = spec && visuals.get(spec.id);
57
99
  if (!spec?.specification?.trim || !visual || !spec.years || !spec.yearBasis)
58
100
  continue;
59
- if (!spec.years.includes(year) || (visual.years && !visual.years.includes(year)) || !inMarkets(spec) || !inMarkets(visual))
101
+ if (!spec.years.includes(year) || (visual.years && !visual.years.includes(year)) || !marketsMatch(spec, visual))
60
102
  continue;
61
103
  const markets = visual.markets ?? spec.markets;
62
104
  result.set(spec.id, {
@@ -4,6 +4,8 @@ export { EXTERIOR_DIFFERENCES, appearancePackageSlug } from "./identity";
4
4
  /**
5
5
  * The appearance packages (Lexus F SPORT, BMW M Sport…) offered on a catalog vehicle in a year, from the
6
6
  * bundled identity graph. A market-scoped package is returned only when `market` is given and matches;
7
- * with no market, only packages without a market scope.
7
+ * with no market, only packages without a market scope. With `anyMarket: true`, every package offered on
8
+ * the row in the year in any market or none: the union, by `specificationId`, of the market-less call and
9
+ * the call for every market (`market` is then ignored).
8
10
  */
9
11
  export declare function getAppearancePackages(query: AppearancePackageQuery): AppearancePackage[];
package/dist/packages.js CHANGED
@@ -13,7 +13,9 @@ Object.defineProperty(exports, "appearancePackageSlug", { enumerable: true, get:
13
13
  /**
14
14
  * The appearance packages (Lexus F SPORT, BMW M Sport…) offered on a catalog vehicle in a year, from the
15
15
  * bundled identity graph. A market-scoped package is returned only when `market` is given and matches;
16
- * with no market, only packages without a market scope.
16
+ * with no market, only packages without a market scope. With `anyMarket: true`, every package offered on
17
+ * the row in the year in any market or none: the union, by `specificationId`, of the market-less call and
18
+ * the call for every market (`market` is then ignored).
17
19
  */
18
20
  function getAppearancePackages(query) {
19
21
  return (0, identity_1.listAppearancePackages)(identity_data_1.default, query);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meterapp/vehicle-db",
3
- "version": "2.72.0",
3
+ "version": "2.74.0",
4
4
  "description": "Offline international database of car, motorcycle, truck and bus makes, models and years (1990-2027) for vehicle pickers, autocomplete and validation, with transparent car images for every entry through carimage.dev",
5
5
  "repository": {
6
6
  "type": "git",