@code-plate/iran-cities 1.0.0 → 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/react.cjs CHANGED
@@ -5232,61 +5232,114 @@ var data_default = [
5232
5232
  ];
5233
5233
 
5234
5234
  // src/index.ts
5235
- var provinces = data_default.map((p) => ({
5236
- en: p["province-en"],
5237
- fa: p["province-fa"],
5238
- cities: p.cities.map((c) => ({ en: c["city-en"], fa: c["city-fa"] }))
5239
- }));
5240
5235
  function normalize(input) {
5241
5236
  return input.trim().toLowerCase().replace(/ي/g, "\u06CC").replace(/ك/g, "\u06A9").replace(/[\u200c\s-]+/g, "");
5242
5237
  }
5243
- var _flatCache = null;
5244
- function getFlatCitiesInternal() {
5245
- if (_flatCache) return _flatCache;
5246
- const flat = [];
5247
- for (const p of provinces) {
5248
- for (const c of p.cities) {
5249
- flat.push({ en: c.en, fa: c.fa, provinceEn: p.en, provinceFa: p.fa });
5238
+ function levenshtein(a, b) {
5239
+ if (a === b) return 0;
5240
+ const al = a.length;
5241
+ const bl = b.length;
5242
+ if (al === 0) return bl;
5243
+ if (bl === 0) return al;
5244
+ let prev = new Array(bl + 1);
5245
+ for (let j = 0; j <= bl; j++) prev[j] = j;
5246
+ for (let i = 1; i <= al; i++) {
5247
+ const curr = new Array(bl + 1);
5248
+ curr[0] = i;
5249
+ for (let j = 1; j <= bl; j++) {
5250
+ const cost = a.charCodeAt(i - 1) === b.charCodeAt(j - 1) ? 0 : 1;
5251
+ curr[j] = Math.min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost);
5250
5252
  }
5253
+ prev = curr;
5251
5254
  }
5252
- _flatCache = flat;
5253
- return flat;
5255
+ return prev[bl];
5256
+ }
5257
+ function matchScore(query, target, fuzzy, maxDistance) {
5258
+ if (!target) return Infinity;
5259
+ if (target === query) return 0;
5260
+ if (target.startsWith(query)) return 1;
5261
+ if (target.includes(query)) return 2;
5262
+ if (fuzzy) {
5263
+ const dist = levenshtein(query, target);
5264
+ if (dist <= maxDistance) return 3 + dist;
5265
+ }
5266
+ return Infinity;
5267
+ }
5268
+ var provinces = data_default.map((p) => {
5269
+ const provinceEn = p["province-en"];
5270
+ const provinceFa = p["province-fa"];
5271
+ return {
5272
+ en: provinceEn,
5273
+ fa: provinceFa,
5274
+ cities: p.cities.map((c) => ({
5275
+ en: c["city-en"],
5276
+ fa: c["city-fa"],
5277
+ slug: `${provinceEn}/${c["city-en"]}`
5278
+ }))
5279
+ };
5280
+ });
5281
+ var flatCities = [];
5282
+ var cityBySlug = /* @__PURE__ */ new Map();
5283
+ var cityByNormKey = /* @__PURE__ */ new Map();
5284
+ var provinceByNormKey = /* @__PURE__ */ new Map();
5285
+ var cityToProvinceByNormKey = /* @__PURE__ */ new Map();
5286
+ var searchableCities = [];
5287
+ var searchableProvinces = [];
5288
+ for (const p of provinces) {
5289
+ const pNormEn = normalize(p.en);
5290
+ const pNormFa = normalize(p.fa);
5291
+ if (!provinceByNormKey.has(pNormEn)) provinceByNormKey.set(pNormEn, p);
5292
+ if (!provinceByNormKey.has(pNormFa)) provinceByNormKey.set(pNormFa, p);
5293
+ searchableProvinces.push({ item: p, normEn: pNormEn, normFa: pNormFa });
5294
+ for (const c of p.cities) {
5295
+ const flat = { en: c.en, fa: c.fa, slug: c.slug, provinceEn: p.en, provinceFa: p.fa };
5296
+ flatCities.push(flat);
5297
+ cityBySlug.set(flat.slug, flat);
5298
+ const cNormEn = normalize(c.en);
5299
+ const cNormFa = normalize(c.fa);
5300
+ if (!cityByNormKey.has(cNormEn)) cityByNormKey.set(cNormEn, flat);
5301
+ if (!cityByNormKey.has(cNormFa)) cityByNormKey.set(cNormFa, flat);
5302
+ if (!cityToProvinceByNormKey.has(cNormEn)) cityToProvinceByNormKey.set(cNormEn, p);
5303
+ if (!cityToProvinceByNormKey.has(cNormFa)) cityToProvinceByNormKey.set(cNormFa, p);
5304
+ searchableCities.push({ item: flat, normEn: cNormEn, normFa: cNormFa });
5305
+ }
5306
+ }
5307
+ function rankedSearch(pool, query, options) {
5308
+ var _a, _b, _c;
5309
+ const q = normalize(query);
5310
+ if (!q) {
5311
+ const all = pool.map((e) => e.item);
5312
+ return options.limit ? all.slice(0, options.limit) : all;
5313
+ }
5314
+ const locale = (_a = options.locale) != null ? _a : "both";
5315
+ const fuzzy = (_b = options.fuzzy) != null ? _b : false;
5316
+ const maxDistance = (_c = options.maxDistance) != null ? _c : 2;
5317
+ const scored = [];
5318
+ for (const entry of pool) {
5319
+ let score = Infinity;
5320
+ if (locale !== "fa") score = Math.min(score, matchScore(q, entry.normEn, fuzzy, maxDistance));
5321
+ if (locale !== "en") score = Math.min(score, matchScore(q, entry.normFa, fuzzy, maxDistance));
5322
+ if (score !== Infinity) scored.push({ item: entry.item, score });
5323
+ }
5324
+ scored.sort((a, b) => a.score - b.score);
5325
+ const results = scored.map((s) => s.item);
5326
+ return options.limit ? results.slice(0, options.limit) : results;
5254
5327
  }
5255
5328
  function getProvincesList() {
5256
5329
  return provinces.map((p) => ({ en: p.en, fa: p.fa }));
5257
5330
  }
5258
5331
  function getProvince(query) {
5259
- const q = normalize(query);
5260
- return provinces.find((p) => normalize(p.en) === q || normalize(p.fa) === q);
5332
+ return provinceByNormKey.get(normalize(query));
5261
5333
  }
5262
5334
  function getCities(provinceQuery) {
5263
- const province = getProvince(provinceQuery);
5264
- return province ? province.cities : [];
5335
+ var _a, _b;
5336
+ return (_b = (_a = getProvince(provinceQuery)) == null ? void 0 : _a.cities) != null ? _b : [];
5265
5337
  }
5266
5338
  function searchProvinces(query, options = {}) {
5267
- var _a;
5268
- const q = normalize(query);
5269
- if (!q) return options.limit ? provinces.slice(0, options.limit) : provinces;
5270
- const locale = (_a = options.locale) != null ? _a : "both";
5271
- const results = provinces.filter((p) => {
5272
- const matchEn = locale !== "fa" && normalize(p.en).includes(q);
5273
- const matchFa = locale !== "en" && normalize(p.fa).includes(q);
5274
- return matchEn || matchFa;
5275
- });
5276
- return options.limit ? results.slice(0, options.limit) : results;
5339
+ return rankedSearch(searchableProvinces, query, options);
5277
5340
  }
5278
5341
  function searchCities(query, options = {}) {
5279
- var _a;
5280
- const q = normalize(query);
5281
- const all = getFlatCitiesInternal();
5282
- if (!q) return options.limit ? all.slice(0, options.limit) : all;
5283
- const locale = (_a = options.locale) != null ? _a : "both";
5284
- const results = all.filter((c) => {
5285
- const matchEn = locale !== "fa" && normalize(c.en).includes(q);
5286
- const matchFa = locale !== "en" && normalize(c.fa).includes(q);
5287
- return matchEn || matchFa;
5288
- });
5289
- return options.limit ? results.slice(0, options.limit) : results;
5342
+ return rankedSearch(searchableCities, query, options);
5290
5343
  }
5291
5344
 
5292
5345
  // src/react.ts