@meterapp/vehicle-db 2.90.0 → 2.91.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.
- package/README.md +7 -7
- package/dist/data.js +1 -1
- package/dist/search-index.d.ts +50 -0
- package/dist/search-index.js +173 -0
- package/dist/search.js +5 -108
- package/package.json +1 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import data from "./data";
|
|
2
|
+
export interface SearchText {
|
|
3
|
+
spaced: string;
|
|
4
|
+
compact: string;
|
|
5
|
+
tokens: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface IndexedMakeRow {
|
|
8
|
+
year: number;
|
|
9
|
+
vehicleTypeId: number;
|
|
10
|
+
sourceMask: number;
|
|
11
|
+
}
|
|
12
|
+
export interface IndexedMake {
|
|
13
|
+
makeId: number;
|
|
14
|
+
makeName: string;
|
|
15
|
+
forms: SearchText[];
|
|
16
|
+
rows: IndexedMakeRow[];
|
|
17
|
+
}
|
|
18
|
+
export type IndexedModelRow = (typeof data.models)[number];
|
|
19
|
+
export interface IndexedModel {
|
|
20
|
+
makeId: number;
|
|
21
|
+
makeName: string;
|
|
22
|
+
vehicleTypeId: number;
|
|
23
|
+
vehicleTypeName: string;
|
|
24
|
+
makeForms: SearchText[];
|
|
25
|
+
modelForms: SearchText[];
|
|
26
|
+
combinedForms: SearchText[];
|
|
27
|
+
rows: IndexedModelRow[];
|
|
28
|
+
}
|
|
29
|
+
export interface SearchIndex {
|
|
30
|
+
makes: IndexedMake[];
|
|
31
|
+
models: IndexedModel[];
|
|
32
|
+
}
|
|
33
|
+
export declare const MAKE_SEARCH_ALIASES: Map<string, string[]>;
|
|
34
|
+
/**
|
|
35
|
+
* Builds the index searchVehicles matches against, once per process.
|
|
36
|
+
*
|
|
37
|
+
* The catalog has about 5.6 rows per model name, so each distinct model name is
|
|
38
|
+
* normalized once and rows are grouped by numeric keys. A combined make and
|
|
39
|
+
* model form is joined from the two normalized forms, which is exactly what
|
|
40
|
+
* normalizing the joined text returns. The index is the same, in content and
|
|
41
|
+
* order, as normalizing every row (the pre-2.90.1 build; see the test).
|
|
42
|
+
*/
|
|
43
|
+
export declare function buildSearchIndex(): SearchIndex;
|
|
44
|
+
export declare function normalizeSearchText(value: string): SearchText;
|
|
45
|
+
/**
|
|
46
|
+
* normalizeSearchText(`${left.spaced} ${right.spaced}`) for two normalized forms:
|
|
47
|
+
* their text is already lowercase ASCII letters and digits in single-spaced
|
|
48
|
+
* tokens, so normalizing the joined text only drops the space next to an empty side.
|
|
49
|
+
*/
|
|
50
|
+
export declare function joinSearchText(left: SearchText, right: SearchText): SearchText;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MAKE_SEARCH_ALIASES = void 0;
|
|
7
|
+
exports.buildSearchIndex = buildSearchIndex;
|
|
8
|
+
exports.normalizeSearchText = normalizeSearchText;
|
|
9
|
+
exports.joinSearchText = joinSearchText;
|
|
10
|
+
const data_1 = __importDefault(require("./data"));
|
|
11
|
+
// These are search-only colloquialisms. They do not rewrite catalog identities.
|
|
12
|
+
exports.MAKE_SEARCH_ALIASES = new Map([
|
|
13
|
+
["alfa romeo", ["alfa"]],
|
|
14
|
+
["bmw", ["bimmer", "beemer"]],
|
|
15
|
+
["chevrolet", ["chevy"]],
|
|
16
|
+
["general motors", ["gm"]],
|
|
17
|
+
["harley davidson", ["harley"]],
|
|
18
|
+
["mercedes benz", ["mercedes", "benz", "merc"]],
|
|
19
|
+
["volkswagen", ["vw"]],
|
|
20
|
+
// TQ WULING is the official Malaysian brand; retain its published identity.
|
|
21
|
+
["tq wuling", ["wuling"]],
|
|
22
|
+
// Reviewed manufacturer brand names; numeric catalog make IDs stay stable.
|
|
23
|
+
// Evidence: data/identity/catalog.json makeAliases (Hero FY2024-25 report,
|
|
24
|
+
// Maruti Suzuki March 2026 sales release). These are lexical candidates only.
|
|
25
|
+
["hero", ["hero motocorp"]],
|
|
26
|
+
["maruti", ["maruti suzuki"]],
|
|
27
|
+
]);
|
|
28
|
+
/**
|
|
29
|
+
* Builds the index searchVehicles matches against, once per process.
|
|
30
|
+
*
|
|
31
|
+
* The catalog has about 5.6 rows per model name, so each distinct model name is
|
|
32
|
+
* normalized once and rows are grouped by numeric keys. A combined make and
|
|
33
|
+
* model form is joined from the two normalized forms, which is exactly what
|
|
34
|
+
* normalizing the joined text returns. The index is the same, in content and
|
|
35
|
+
* order, as normalizing every row (the pre-2.90.1 build; see the test).
|
|
36
|
+
*/
|
|
37
|
+
function buildSearchIndex() {
|
|
38
|
+
const vehicleTypes = new Map();
|
|
39
|
+
data_1.default.vehicleTypes.forEach((type, index) => {
|
|
40
|
+
vehicleTypes.set(type.vehicle_type_id, { name: type.vehicle_type_name, index });
|
|
41
|
+
});
|
|
42
|
+
const typeCount = data_1.default.vehicleTypes.length;
|
|
43
|
+
const makes = new Map();
|
|
44
|
+
for (const make of data_1.default.makes) {
|
|
45
|
+
const baseForm = normalizeSearchText(make.make_name);
|
|
46
|
+
const aliases = exports.MAKE_SEARCH_ALIASES.get(baseForm.spaced) ?? [];
|
|
47
|
+
makes.set(make.make_id, {
|
|
48
|
+
make: {
|
|
49
|
+
makeId: make.make_id,
|
|
50
|
+
makeName: make.make_name,
|
|
51
|
+
forms: uniqueForms([baseForm, ...aliases.map(normalizeSearchText)]),
|
|
52
|
+
rows: [],
|
|
53
|
+
},
|
|
54
|
+
rows: new Map(),
|
|
55
|
+
models: new Map(),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const nameForms = new Array(data_1.default.modelNames.length);
|
|
59
|
+
const compactIds = new Map();
|
|
60
|
+
const models = [];
|
|
61
|
+
for (const row of data_1.default.models) {
|
|
62
|
+
// Indexed reads: destructuring each row took about a third of this loop.
|
|
63
|
+
const year = row[0];
|
|
64
|
+
const makeId = row[1];
|
|
65
|
+
const modelNameIndex = row[3];
|
|
66
|
+
const vehicleTypeId = row[4];
|
|
67
|
+
const sourceMask = row[5];
|
|
68
|
+
const make = makes.get(makeId);
|
|
69
|
+
const modelName = data_1.default.modelNames[modelNameIndex];
|
|
70
|
+
const vehicleType = vehicleTypes.get(vehicleTypeId);
|
|
71
|
+
if (!make || modelName == null || vehicleType == null || vehicleType.name == null)
|
|
72
|
+
continue;
|
|
73
|
+
// Catalog years are integers; any other year keeps the per-row build's text key.
|
|
74
|
+
const numericMakeRowKey = year * typeCount + vehicleType.index;
|
|
75
|
+
const makeRowKey = Number.isSafeInteger(year) && Number.isSafeInteger(numericMakeRowKey)
|
|
76
|
+
? numericMakeRowKey
|
|
77
|
+
: `${year}\u0000${vehicleTypeId}`;
|
|
78
|
+
const makeRow = make.rows.get(makeRowKey);
|
|
79
|
+
if (makeRow)
|
|
80
|
+
makeRow.sourceMask |= sourceMask;
|
|
81
|
+
else
|
|
82
|
+
make.rows.set(makeRowKey, { year, vehicleTypeId, sourceMask });
|
|
83
|
+
let name = nameForms[modelNameIndex];
|
|
84
|
+
if (name === undefined) {
|
|
85
|
+
const form = normalizeSearchText(modelName);
|
|
86
|
+
let compactId = compactIds.get(form.compact);
|
|
87
|
+
if (compactId === undefined) {
|
|
88
|
+
compactId = compactIds.size;
|
|
89
|
+
compactIds.set(form.compact, compactId);
|
|
90
|
+
}
|
|
91
|
+
name = { form, compactId };
|
|
92
|
+
nameForms[modelNameIndex] = name;
|
|
93
|
+
}
|
|
94
|
+
const modelKey = name.compactId * typeCount + vehicleType.index;
|
|
95
|
+
let model = make.models.get(modelKey);
|
|
96
|
+
if (!model) {
|
|
97
|
+
model = {
|
|
98
|
+
model: {
|
|
99
|
+
makeId,
|
|
100
|
+
makeName: make.make.makeName,
|
|
101
|
+
vehicleTypeId,
|
|
102
|
+
vehicleTypeName: vehicleType.name,
|
|
103
|
+
makeForms: make.make.forms,
|
|
104
|
+
modelForms: [],
|
|
105
|
+
combinedForms: [],
|
|
106
|
+
rows: [],
|
|
107
|
+
},
|
|
108
|
+
nameIndexes: [],
|
|
109
|
+
};
|
|
110
|
+
make.models.set(modelKey, model);
|
|
111
|
+
models.push(model);
|
|
112
|
+
}
|
|
113
|
+
const { nameIndexes } = model;
|
|
114
|
+
if (nameIndexes[nameIndexes.length - 1] !== modelNameIndex &&
|
|
115
|
+
!nameIndexes.includes(modelNameIndex)) {
|
|
116
|
+
nameIndexes.push(modelNameIndex);
|
|
117
|
+
}
|
|
118
|
+
model.model.rows.push(row);
|
|
119
|
+
}
|
|
120
|
+
for (const make of makes.values()) {
|
|
121
|
+
make.make.rows = [...make.rows.values()];
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
makes: [...makes.values()].map(({ make }) => make),
|
|
125
|
+
models: models.map(({ model, nameIndexes }) => {
|
|
126
|
+
model.modelForms = uniqueForms(nameIndexes.map((index) => nameForms[index].form));
|
|
127
|
+
model.combinedForms = uniqueForms(model.makeForms.flatMap((makeForm) => model.modelForms.map((modelForm) => joinSearchText(makeForm, modelForm))));
|
|
128
|
+
return model;
|
|
129
|
+
}),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function normalizeSearchText(value) {
|
|
133
|
+
const spaced = value
|
|
134
|
+
.normalize("NFKD")
|
|
135
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
136
|
+
.toLocaleLowerCase("en-US")
|
|
137
|
+
.replace(/&/g, " and ")
|
|
138
|
+
.replace(/[\u2019']/g, "")
|
|
139
|
+
.replace(/[^a-z0-9]+/g, " ")
|
|
140
|
+
.trim()
|
|
141
|
+
.replace(/\s+/g, " ");
|
|
142
|
+
return {
|
|
143
|
+
spaced,
|
|
144
|
+
compact: spaced.replace(/\s/g, ""),
|
|
145
|
+
tokens: spaced.length === 0 ? [] : spaced.split(" "),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* normalizeSearchText(`${left.spaced} ${right.spaced}`) for two normalized forms:
|
|
150
|
+
* their text is already lowercase ASCII letters and digits in single-spaced
|
|
151
|
+
* tokens, so normalizing the joined text only drops the space next to an empty side.
|
|
152
|
+
*/
|
|
153
|
+
function joinSearchText(left, right) {
|
|
154
|
+
return {
|
|
155
|
+
spaced: left.spaced.length > 0 && right.spaced.length > 0
|
|
156
|
+
? `${left.spaced} ${right.spaced}`
|
|
157
|
+
: left.spaced + right.spaced,
|
|
158
|
+
compact: left.compact + right.compact,
|
|
159
|
+
tokens: left.tokens.concat(right.tokens),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function uniqueForms(forms) {
|
|
163
|
+
if (forms.length < 2)
|
|
164
|
+
return forms;
|
|
165
|
+
const seen = new Set();
|
|
166
|
+
return forms.filter((form) => {
|
|
167
|
+
const key = `${form.spaced}\u0000${form.compact}`;
|
|
168
|
+
if (seen.has(key))
|
|
169
|
+
return false;
|
|
170
|
+
seen.add(key);
|
|
171
|
+
return true;
|
|
172
|
+
});
|
|
173
|
+
}
|
package/dist/search.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.searchVehicles = searchVehicles;
|
|
7
7
|
const data_1 = __importDefault(require("./data"));
|
|
8
|
+
const search_index_1 = require("./search-index");
|
|
8
9
|
const MATCH_RANK = {
|
|
9
10
|
exact: 0,
|
|
10
11
|
prefix: 1,
|
|
@@ -12,25 +13,7 @@ const MATCH_RANK = {
|
|
|
12
13
|
contains: 3,
|
|
13
14
|
fuzzy: 4,
|
|
14
15
|
};
|
|
15
|
-
// These are search-only colloquialisms. They do not rewrite catalog identities.
|
|
16
|
-
const MAKE_SEARCH_ALIASES = new Map([
|
|
17
|
-
["alfa romeo", ["alfa"]],
|
|
18
|
-
["bmw", ["bimmer", "beemer"]],
|
|
19
|
-
["chevrolet", ["chevy"]],
|
|
20
|
-
["general motors", ["gm"]],
|
|
21
|
-
["harley davidson", ["harley"]],
|
|
22
|
-
["mercedes benz", ["mercedes", "benz", "merc"]],
|
|
23
|
-
["volkswagen", ["vw"]],
|
|
24
|
-
// TQ WULING is the official Malaysian brand; retain its published identity.
|
|
25
|
-
["tq wuling", ["wuling"]],
|
|
26
|
-
// Reviewed manufacturer brand names; numeric catalog make IDs stay stable.
|
|
27
|
-
// Evidence: data/identity/catalog.json makeAliases (Hero FY2024-25 report,
|
|
28
|
-
// Maruti Suzuki March 2026 sales release). These are lexical candidates only.
|
|
29
|
-
["hero", ["hero motocorp"]],
|
|
30
|
-
["maruti", ["maruti suzuki"]],
|
|
31
|
-
]);
|
|
32
16
|
const sourceMaskById = new Map(data_1.default.sources.map((source, index) => [source.source_id, 2 ** index]));
|
|
33
|
-
const vehicleTypeNameById = new Map(data_1.default.vehicleTypes.map((type) => [type.vehicle_type_id, type.vehicle_type_name]));
|
|
34
17
|
let _searchIndex = null;
|
|
35
18
|
/**
|
|
36
19
|
* Finds canonical make and model candidates for a driver-entered query.
|
|
@@ -158,98 +141,12 @@ function collectMatches(index, query, year, interpretationRank, options, sourceM
|
|
|
158
141
|
}
|
|
159
142
|
}
|
|
160
143
|
function getSearchIndex() {
|
|
161
|
-
if (_searchIndex)
|
|
162
|
-
|
|
163
|
-
const makesById = new Map();
|
|
164
|
-
const makeRowsById = new Map();
|
|
165
|
-
for (const make of data_1.default.makes) {
|
|
166
|
-
const baseForm = normalizeSearchText(make.make_name);
|
|
167
|
-
const aliases = MAKE_SEARCH_ALIASES.get(baseForm.spaced) ?? [];
|
|
168
|
-
makesById.set(make.make_id, {
|
|
169
|
-
makeId: make.make_id,
|
|
170
|
-
makeName: make.make_name,
|
|
171
|
-
forms: uniqueForms([baseForm, ...aliases.map(normalizeSearchText)]),
|
|
172
|
-
rows: [],
|
|
173
|
-
});
|
|
174
|
-
makeRowsById.set(make.make_id, new Map());
|
|
175
|
-
}
|
|
176
|
-
const modelsByKey = new Map();
|
|
177
|
-
for (const row of data_1.default.models) {
|
|
178
|
-
const [year, makeId, modelId, modelNameIndex, vehicleTypeId, sourceMask] = row;
|
|
179
|
-
const make = makesById.get(makeId);
|
|
180
|
-
const modelName = data_1.default.modelNames[modelNameIndex];
|
|
181
|
-
const vehicleTypeName = vehicleTypeNameById.get(vehicleTypeId);
|
|
182
|
-
if (!make || modelName == null || vehicleTypeName == null)
|
|
183
|
-
continue;
|
|
184
|
-
const makeRows = makeRowsById.get(makeId);
|
|
185
|
-
const makeRowKey = `${year}\u0000${vehicleTypeId}`;
|
|
186
|
-
const existingMakeRow = makeRows.get(makeRowKey);
|
|
187
|
-
if (existingMakeRow)
|
|
188
|
-
existingMakeRow.sourceMask |= sourceMask;
|
|
189
|
-
else
|
|
190
|
-
makeRows.set(makeRowKey, { year, vehicleTypeId, sourceMask });
|
|
191
|
-
const modelForm = normalizeSearchText(modelName);
|
|
192
|
-
const key = `${makeId}\u0000${modelForm.compact}\u0000${vehicleTypeId}`;
|
|
193
|
-
let model = modelsByKey.get(key);
|
|
194
|
-
if (!model) {
|
|
195
|
-
model = {
|
|
196
|
-
makeId,
|
|
197
|
-
makeName: make.makeName,
|
|
198
|
-
vehicleTypeId,
|
|
199
|
-
vehicleTypeName,
|
|
200
|
-
makeForms: make.forms,
|
|
201
|
-
modelForms: [],
|
|
202
|
-
combinedForms: [],
|
|
203
|
-
modelFormMap: new Map(),
|
|
204
|
-
rows: [],
|
|
205
|
-
};
|
|
206
|
-
modelsByKey.set(key, model);
|
|
207
|
-
}
|
|
208
|
-
model.modelFormMap.set(`${modelForm.spaced}\u0000${modelForm.compact}`, modelForm);
|
|
209
|
-
model.rows.push(row);
|
|
210
|
-
}
|
|
211
|
-
for (const make of makesById.values()) {
|
|
212
|
-
make.rows = [...makeRowsById.get(make.makeId).values()];
|
|
213
|
-
}
|
|
214
|
-
const models = [...modelsByKey.values()].map(({ modelFormMap, ...model }) => {
|
|
215
|
-
model.modelForms = [...modelFormMap.values()];
|
|
216
|
-
model.combinedForms = uniqueForms(model.makeForms.flatMap((makeForm) => model.modelForms.map((modelForm) => normalizeSearchText(`${makeForm.spaced} ${modelForm.spaced}`))));
|
|
217
|
-
return model;
|
|
218
|
-
});
|
|
219
|
-
_searchIndex = {
|
|
220
|
-
makes: [...makesById.values()],
|
|
221
|
-
models,
|
|
222
|
-
};
|
|
144
|
+
if (!_searchIndex)
|
|
145
|
+
_searchIndex = (0, search_index_1.buildSearchIndex)();
|
|
223
146
|
return _searchIndex;
|
|
224
147
|
}
|
|
225
|
-
function normalizeSearchText(value) {
|
|
226
|
-
const spaced = value
|
|
227
|
-
.normalize("NFKD")
|
|
228
|
-
.replace(/[\u0300-\u036f]/g, "")
|
|
229
|
-
.toLocaleLowerCase("en-US")
|
|
230
|
-
.replace(/&/g, " and ")
|
|
231
|
-
.replace(/[\u2019']/g, "")
|
|
232
|
-
.replace(/[^a-z0-9]+/g, " ")
|
|
233
|
-
.trim()
|
|
234
|
-
.replace(/\s+/g, " ");
|
|
235
|
-
return {
|
|
236
|
-
spaced,
|
|
237
|
-
compact: spaced.replace(/\s/g, ""),
|
|
238
|
-
tokens: spaced.length === 0 ? [] : spaced.split(" "),
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function uniqueForms(forms) {
|
|
242
|
-
const seen = new Set();
|
|
243
|
-
return forms.filter((form) => {
|
|
244
|
-
const key = `${form.spaced}\u0000${form.compact}`;
|
|
245
|
-
if (seen.has(key))
|
|
246
|
-
return false;
|
|
247
|
-
seen.add(key);
|
|
248
|
-
return true;
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
148
|
function parseQueries(query, optionYear) {
|
|
252
|
-
const fullText = normalizeSearchText(query);
|
|
149
|
+
const fullText = (0, search_index_1.normalizeSearchText)(query);
|
|
253
150
|
if (fullText.spaced.length === 0)
|
|
254
151
|
return [];
|
|
255
152
|
const detectedYears = [...query.matchAll(/\b(?:19|20)\d{2}\b/g)].map((match) => Number(match[0]));
|
|
@@ -265,7 +162,7 @@ function parseQueries(query, optionYear) {
|
|
|
265
162
|
];
|
|
266
163
|
const inferredYears = optionYear == null ? uniqueYears : uniqueYears.filter((year) => year === optionYear);
|
|
267
164
|
for (const year of inferredYears) {
|
|
268
|
-
const text = normalizeSearchText(query.replace(new RegExp(`\\b${year}\\b`, "g"), " "));
|
|
165
|
+
const text = (0, search_index_1.normalizeSearchText)(query.replace(new RegExp(`\\b${year}\\b`, "g"), " "));
|
|
269
166
|
if (text.spaced.length === 0)
|
|
270
167
|
continue;
|
|
271
168
|
interpretations.push({ text, year: optionYear ?? year, interpretationRank: 1 });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meterapp/vehicle-db",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.91.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",
|