@code-plate/iran-cities 1.0.1 → 1.0.2
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/dist/index.cjs +107 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +21 -8
- package/dist/index.d.ts +21 -8
- package/dist/index.mjs +106 -52
- package/dist/index.mjs.map +1 -1
- package/dist/react.cjs +92 -39
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.mts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +92 -39
- package/dist/react.mjs.map +1 -1
- package/dist/{types--eoWJy5A.d.mts → types-DwFZSr9I.d.mts} +8 -1
- package/dist/{types--eoWJy5A.d.ts → types-DwFZSr9I.d.ts} +8 -1
- package/package.json +6 -3
package/dist/index.cjs
CHANGED
|
@@ -23,6 +23,7 @@ __export(src_exports, {
|
|
|
23
23
|
getAllCities: () => getAllCities,
|
|
24
24
|
getCities: () => getCities,
|
|
25
25
|
getCity: () => getCity,
|
|
26
|
+
getCityBySlug: () => getCityBySlug,
|
|
26
27
|
getProvince: () => getProvince,
|
|
27
28
|
getProvinceOfCity: () => getProvinceOfCity,
|
|
28
29
|
getProvinces: () => getProvinces,
|
|
@@ -5236,25 +5237,98 @@ var data_default = [
|
|
|
5236
5237
|
];
|
|
5237
5238
|
|
|
5238
5239
|
// src/index.ts
|
|
5239
|
-
var provinces = data_default.map((p) => ({
|
|
5240
|
-
en: p["province-en"],
|
|
5241
|
-
fa: p["province-fa"],
|
|
5242
|
-
cities: p.cities.map((c) => ({ en: c["city-en"], fa: c["city-fa"] }))
|
|
5243
|
-
}));
|
|
5244
5240
|
function normalize(input) {
|
|
5245
5241
|
return input.trim().toLowerCase().replace(/ي/g, "\u06CC").replace(/ك/g, "\u06A9").replace(/[\u200c\s-]+/g, "");
|
|
5246
5242
|
}
|
|
5247
|
-
|
|
5248
|
-
|
|
5249
|
-
|
|
5250
|
-
const
|
|
5251
|
-
|
|
5252
|
-
|
|
5253
|
-
|
|
5243
|
+
function levenshtein(a, b) {
|
|
5244
|
+
if (a === b) return 0;
|
|
5245
|
+
const al = a.length;
|
|
5246
|
+
const bl = b.length;
|
|
5247
|
+
if (al === 0) return bl;
|
|
5248
|
+
if (bl === 0) return al;
|
|
5249
|
+
let prev = new Array(bl + 1);
|
|
5250
|
+
for (let j = 0; j <= bl; j++) prev[j] = j;
|
|
5251
|
+
for (let i = 1; i <= al; i++) {
|
|
5252
|
+
const curr = new Array(bl + 1);
|
|
5253
|
+
curr[0] = i;
|
|
5254
|
+
for (let j = 1; j <= bl; j++) {
|
|
5255
|
+
const cost = a.charCodeAt(i - 1) === b.charCodeAt(j - 1) ? 0 : 1;
|
|
5256
|
+
curr[j] = Math.min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost);
|
|
5254
5257
|
}
|
|
5258
|
+
prev = curr;
|
|
5255
5259
|
}
|
|
5256
|
-
|
|
5257
|
-
|
|
5260
|
+
return prev[bl];
|
|
5261
|
+
}
|
|
5262
|
+
function matchScore(query, target, fuzzy, maxDistance) {
|
|
5263
|
+
if (!target) return Infinity;
|
|
5264
|
+
if (target === query) return 0;
|
|
5265
|
+
if (target.startsWith(query)) return 1;
|
|
5266
|
+
if (target.includes(query)) return 2;
|
|
5267
|
+
if (fuzzy) {
|
|
5268
|
+
const dist = levenshtein(query, target);
|
|
5269
|
+
if (dist <= maxDistance) return 3 + dist;
|
|
5270
|
+
}
|
|
5271
|
+
return Infinity;
|
|
5272
|
+
}
|
|
5273
|
+
var provinces = data_default.map((p) => {
|
|
5274
|
+
const provinceEn = p["province-en"];
|
|
5275
|
+
const provinceFa = p["province-fa"];
|
|
5276
|
+
return {
|
|
5277
|
+
en: provinceEn,
|
|
5278
|
+
fa: provinceFa,
|
|
5279
|
+
cities: p.cities.map((c) => ({
|
|
5280
|
+
en: c["city-en"],
|
|
5281
|
+
fa: c["city-fa"],
|
|
5282
|
+
slug: `${provinceEn}/${c["city-en"]}`
|
|
5283
|
+
}))
|
|
5284
|
+
};
|
|
5285
|
+
});
|
|
5286
|
+
var flatCities = [];
|
|
5287
|
+
var cityBySlug = /* @__PURE__ */ new Map();
|
|
5288
|
+
var cityByNormKey = /* @__PURE__ */ new Map();
|
|
5289
|
+
var provinceByNormKey = /* @__PURE__ */ new Map();
|
|
5290
|
+
var cityToProvinceByNormKey = /* @__PURE__ */ new Map();
|
|
5291
|
+
var searchableCities = [];
|
|
5292
|
+
var searchableProvinces = [];
|
|
5293
|
+
for (const p of provinces) {
|
|
5294
|
+
const pNormEn = normalize(p.en);
|
|
5295
|
+
const pNormFa = normalize(p.fa);
|
|
5296
|
+
if (!provinceByNormKey.has(pNormEn)) provinceByNormKey.set(pNormEn, p);
|
|
5297
|
+
if (!provinceByNormKey.has(pNormFa)) provinceByNormKey.set(pNormFa, p);
|
|
5298
|
+
searchableProvinces.push({ item: p, normEn: pNormEn, normFa: pNormFa });
|
|
5299
|
+
for (const c of p.cities) {
|
|
5300
|
+
const flat = { en: c.en, fa: c.fa, slug: c.slug, provinceEn: p.en, provinceFa: p.fa };
|
|
5301
|
+
flatCities.push(flat);
|
|
5302
|
+
cityBySlug.set(flat.slug, flat);
|
|
5303
|
+
const cNormEn = normalize(c.en);
|
|
5304
|
+
const cNormFa = normalize(c.fa);
|
|
5305
|
+
if (!cityByNormKey.has(cNormEn)) cityByNormKey.set(cNormEn, flat);
|
|
5306
|
+
if (!cityByNormKey.has(cNormFa)) cityByNormKey.set(cNormFa, flat);
|
|
5307
|
+
if (!cityToProvinceByNormKey.has(cNormEn)) cityToProvinceByNormKey.set(cNormEn, p);
|
|
5308
|
+
if (!cityToProvinceByNormKey.has(cNormFa)) cityToProvinceByNormKey.set(cNormFa, p);
|
|
5309
|
+
searchableCities.push({ item: flat, normEn: cNormEn, normFa: cNormFa });
|
|
5310
|
+
}
|
|
5311
|
+
}
|
|
5312
|
+
function rankedSearch(pool, query, options) {
|
|
5313
|
+
var _a, _b, _c;
|
|
5314
|
+
const q = normalize(query);
|
|
5315
|
+
if (!q) {
|
|
5316
|
+
const all = pool.map((e) => e.item);
|
|
5317
|
+
return options.limit ? all.slice(0, options.limit) : all;
|
|
5318
|
+
}
|
|
5319
|
+
const locale = (_a = options.locale) != null ? _a : "both";
|
|
5320
|
+
const fuzzy = (_b = options.fuzzy) != null ? _b : false;
|
|
5321
|
+
const maxDistance = (_c = options.maxDistance) != null ? _c : 2;
|
|
5322
|
+
const scored = [];
|
|
5323
|
+
for (const entry of pool) {
|
|
5324
|
+
let score = Infinity;
|
|
5325
|
+
if (locale !== "fa") score = Math.min(score, matchScore(q, entry.normEn, fuzzy, maxDistance));
|
|
5326
|
+
if (locale !== "en") score = Math.min(score, matchScore(q, entry.normFa, fuzzy, maxDistance));
|
|
5327
|
+
if (score !== Infinity) scored.push({ item: entry.item, score });
|
|
5328
|
+
}
|
|
5329
|
+
scored.sort((a, b) => a.score - b.score);
|
|
5330
|
+
const results = scored.map((s) => s.item);
|
|
5331
|
+
return options.limit ? results.slice(0, options.limit) : results;
|
|
5258
5332
|
}
|
|
5259
5333
|
function getProvinces() {
|
|
5260
5334
|
return provinces;
|
|
@@ -5263,61 +5337,41 @@ function getProvincesList() {
|
|
|
5263
5337
|
return provinces.map((p) => ({ en: p.en, fa: p.fa }));
|
|
5264
5338
|
}
|
|
5265
5339
|
function getProvince(query) {
|
|
5266
|
-
|
|
5267
|
-
return provinces.find((p) => normalize(p.en) === q || normalize(p.fa) === q);
|
|
5340
|
+
return provinceByNormKey.get(normalize(query));
|
|
5268
5341
|
}
|
|
5269
5342
|
function getCities(provinceQuery) {
|
|
5270
|
-
|
|
5271
|
-
return
|
|
5343
|
+
var _a, _b;
|
|
5344
|
+
return (_b = (_a = getProvince(provinceQuery)) == null ? void 0 : _a.cities) != null ? _b : [];
|
|
5272
5345
|
}
|
|
5273
5346
|
function getAllCities() {
|
|
5274
|
-
return
|
|
5347
|
+
return flatCities;
|
|
5348
|
+
}
|
|
5349
|
+
function getCityBySlug(slug) {
|
|
5350
|
+
return cityBySlug.get(slug);
|
|
5275
5351
|
}
|
|
5276
5352
|
function getCity(cityQuery, provinceQuery) {
|
|
5277
5353
|
const q = normalize(cityQuery);
|
|
5278
|
-
|
|
5279
|
-
|
|
5280
|
-
return
|
|
5281
|
-
|
|
5282
|
-
|
|
5283
|
-
|
|
5284
|
-
|
|
5285
|
-
}) : getFlatCitiesInternal();
|
|
5286
|
-
return pool.find((c) => normalize(c.en) === q || normalize(c.fa) === q);
|
|
5354
|
+
if (provinceQuery) {
|
|
5355
|
+
const province = getProvince(provinceQuery);
|
|
5356
|
+
if (!province) return void 0;
|
|
5357
|
+
const city = province.cities.find((c) => normalize(c.en) === q || normalize(c.fa) === q);
|
|
5358
|
+
return city ? { en: city.en, fa: city.fa, slug: city.slug, provinceEn: province.en, provinceFa: province.fa } : void 0;
|
|
5359
|
+
}
|
|
5360
|
+
return cityByNormKey.get(q);
|
|
5287
5361
|
}
|
|
5288
5362
|
function getProvinceOfCity(cityQuery) {
|
|
5289
|
-
|
|
5290
|
-
return provinces.find((p) => p.cities.some((c) => normalize(c.en) === q || normalize(c.fa) === q));
|
|
5363
|
+
return cityToProvinceByNormKey.get(normalize(cityQuery));
|
|
5291
5364
|
}
|
|
5292
5365
|
function searchProvinces(query, options = {}) {
|
|
5293
|
-
|
|
5294
|
-
const q = normalize(query);
|
|
5295
|
-
if (!q) return options.limit ? provinces.slice(0, options.limit) : provinces;
|
|
5296
|
-
const locale = (_a = options.locale) != null ? _a : "both";
|
|
5297
|
-
const results = provinces.filter((p) => {
|
|
5298
|
-
const matchEn = locale !== "fa" && normalize(p.en).includes(q);
|
|
5299
|
-
const matchFa = locale !== "en" && normalize(p.fa).includes(q);
|
|
5300
|
-
return matchEn || matchFa;
|
|
5301
|
-
});
|
|
5302
|
-
return options.limit ? results.slice(0, options.limit) : results;
|
|
5366
|
+
return rankedSearch(searchableProvinces, query, options);
|
|
5303
5367
|
}
|
|
5304
5368
|
function searchCities(query, options = {}) {
|
|
5305
|
-
|
|
5306
|
-
const q = normalize(query);
|
|
5307
|
-
const all = getFlatCitiesInternal();
|
|
5308
|
-
if (!q) return options.limit ? all.slice(0, options.limit) : all;
|
|
5309
|
-
const locale = (_a = options.locale) != null ? _a : "both";
|
|
5310
|
-
const results = all.filter((c) => {
|
|
5311
|
-
const matchEn = locale !== "fa" && normalize(c.en).includes(q);
|
|
5312
|
-
const matchFa = locale !== "en" && normalize(c.fa).includes(q);
|
|
5313
|
-
return matchEn || matchFa;
|
|
5314
|
-
});
|
|
5315
|
-
return options.limit ? results.slice(0, options.limit) : results;
|
|
5369
|
+
return rankedSearch(searchableCities, query, options);
|
|
5316
5370
|
}
|
|
5317
5371
|
function getStats() {
|
|
5318
5372
|
return {
|
|
5319
5373
|
provinceCount: provinces.length,
|
|
5320
|
-
cityCount:
|
|
5374
|
+
cityCount: flatCities.length
|
|
5321
5375
|
};
|
|
5322
5376
|
}
|
|
5323
5377
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -5325,6 +5379,7 @@ function getStats() {
|
|
|
5325
5379
|
getAllCities,
|
|
5326
5380
|
getCities,
|
|
5327
5381
|
getCity,
|
|
5382
|
+
getCityBySlug,
|
|
5328
5383
|
getProvince,
|
|
5329
5384
|
getProvinceOfCity,
|
|
5330
5385
|
getProvinces,
|