@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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react from 'react';
2
- import { C as City, S as SearchOptions, F as FlatCity, P as Province } from './types--eoWJy5A.mjs';
2
+ import { C as City, S as SearchOptions, F as FlatCity, P as Province } from './types-DwFZSr9I.mjs';
3
3
 
4
4
  /** لیست استان‌ها (بدون شهرها) - برای Select/Dropdown استان */
5
5
  declare function useProvinces(): {
package/dist/react.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react from 'react';
2
- import { C as City, S as SearchOptions, F as FlatCity, P as Province } from './types--eoWJy5A.js';
2
+ import { C as City, S as SearchOptions, F as FlatCity, P as Province } from './types-DwFZSr9I.js';
3
3
 
4
4
  /** لیست استان‌ها (بدون شهرها) - برای Select/Dropdown استان */
5
5
  declare function useProvinces(): {
package/dist/react.mjs CHANGED
@@ -5204,61 +5204,114 @@ var data_default = [
5204
5204
  ];
5205
5205
 
5206
5206
  // src/index.ts
5207
- var provinces = data_default.map((p) => ({
5208
- en: p["province-en"],
5209
- fa: p["province-fa"],
5210
- cities: p.cities.map((c) => ({ en: c["city-en"], fa: c["city-fa"] }))
5211
- }));
5212
5207
  function normalize(input) {
5213
5208
  return input.trim().toLowerCase().replace(/ي/g, "\u06CC").replace(/ك/g, "\u06A9").replace(/[\u200c\s-]+/g, "");
5214
5209
  }
5215
- var _flatCache = null;
5216
- function getFlatCitiesInternal() {
5217
- if (_flatCache) return _flatCache;
5218
- const flat = [];
5219
- for (const p of provinces) {
5220
- for (const c of p.cities) {
5221
- flat.push({ en: c.en, fa: c.fa, provinceEn: p.en, provinceFa: p.fa });
5210
+ function levenshtein(a, b) {
5211
+ if (a === b) return 0;
5212
+ const al = a.length;
5213
+ const bl = b.length;
5214
+ if (al === 0) return bl;
5215
+ if (bl === 0) return al;
5216
+ let prev = new Array(bl + 1);
5217
+ for (let j = 0; j <= bl; j++) prev[j] = j;
5218
+ for (let i = 1; i <= al; i++) {
5219
+ const curr = new Array(bl + 1);
5220
+ curr[0] = i;
5221
+ for (let j = 1; j <= bl; j++) {
5222
+ const cost = a.charCodeAt(i - 1) === b.charCodeAt(j - 1) ? 0 : 1;
5223
+ curr[j] = Math.min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost);
5222
5224
  }
5225
+ prev = curr;
5223
5226
  }
5224
- _flatCache = flat;
5225
- return flat;
5227
+ return prev[bl];
5228
+ }
5229
+ function matchScore(query, target, fuzzy, maxDistance) {
5230
+ if (!target) return Infinity;
5231
+ if (target === query) return 0;
5232
+ if (target.startsWith(query)) return 1;
5233
+ if (target.includes(query)) return 2;
5234
+ if (fuzzy) {
5235
+ const dist = levenshtein(query, target);
5236
+ if (dist <= maxDistance) return 3 + dist;
5237
+ }
5238
+ return Infinity;
5239
+ }
5240
+ var provinces = data_default.map((p) => {
5241
+ const provinceEn = p["province-en"];
5242
+ const provinceFa = p["province-fa"];
5243
+ return {
5244
+ en: provinceEn,
5245
+ fa: provinceFa,
5246
+ cities: p.cities.map((c) => ({
5247
+ en: c["city-en"],
5248
+ fa: c["city-fa"],
5249
+ slug: `${provinceEn}/${c["city-en"]}`
5250
+ }))
5251
+ };
5252
+ });
5253
+ var flatCities = [];
5254
+ var cityBySlug = /* @__PURE__ */ new Map();
5255
+ var cityByNormKey = /* @__PURE__ */ new Map();
5256
+ var provinceByNormKey = /* @__PURE__ */ new Map();
5257
+ var cityToProvinceByNormKey = /* @__PURE__ */ new Map();
5258
+ var searchableCities = [];
5259
+ var searchableProvinces = [];
5260
+ for (const p of provinces) {
5261
+ const pNormEn = normalize(p.en);
5262
+ const pNormFa = normalize(p.fa);
5263
+ if (!provinceByNormKey.has(pNormEn)) provinceByNormKey.set(pNormEn, p);
5264
+ if (!provinceByNormKey.has(pNormFa)) provinceByNormKey.set(pNormFa, p);
5265
+ searchableProvinces.push({ item: p, normEn: pNormEn, normFa: pNormFa });
5266
+ for (const c of p.cities) {
5267
+ const flat = { en: c.en, fa: c.fa, slug: c.slug, provinceEn: p.en, provinceFa: p.fa };
5268
+ flatCities.push(flat);
5269
+ cityBySlug.set(flat.slug, flat);
5270
+ const cNormEn = normalize(c.en);
5271
+ const cNormFa = normalize(c.fa);
5272
+ if (!cityByNormKey.has(cNormEn)) cityByNormKey.set(cNormEn, flat);
5273
+ if (!cityByNormKey.has(cNormFa)) cityByNormKey.set(cNormFa, flat);
5274
+ if (!cityToProvinceByNormKey.has(cNormEn)) cityToProvinceByNormKey.set(cNormEn, p);
5275
+ if (!cityToProvinceByNormKey.has(cNormFa)) cityToProvinceByNormKey.set(cNormFa, p);
5276
+ searchableCities.push({ item: flat, normEn: cNormEn, normFa: cNormFa });
5277
+ }
5278
+ }
5279
+ function rankedSearch(pool, query, options) {
5280
+ var _a, _b, _c;
5281
+ const q = normalize(query);
5282
+ if (!q) {
5283
+ const all = pool.map((e) => e.item);
5284
+ return options.limit ? all.slice(0, options.limit) : all;
5285
+ }
5286
+ const locale = (_a = options.locale) != null ? _a : "both";
5287
+ const fuzzy = (_b = options.fuzzy) != null ? _b : false;
5288
+ const maxDistance = (_c = options.maxDistance) != null ? _c : 2;
5289
+ const scored = [];
5290
+ for (const entry of pool) {
5291
+ let score = Infinity;
5292
+ if (locale !== "fa") score = Math.min(score, matchScore(q, entry.normEn, fuzzy, maxDistance));
5293
+ if (locale !== "en") score = Math.min(score, matchScore(q, entry.normFa, fuzzy, maxDistance));
5294
+ if (score !== Infinity) scored.push({ item: entry.item, score });
5295
+ }
5296
+ scored.sort((a, b) => a.score - b.score);
5297
+ const results = scored.map((s) => s.item);
5298
+ return options.limit ? results.slice(0, options.limit) : results;
5226
5299
  }
5227
5300
  function getProvincesList() {
5228
5301
  return provinces.map((p) => ({ en: p.en, fa: p.fa }));
5229
5302
  }
5230
5303
  function getProvince(query) {
5231
- const q = normalize(query);
5232
- return provinces.find((p) => normalize(p.en) === q || normalize(p.fa) === q);
5304
+ return provinceByNormKey.get(normalize(query));
5233
5305
  }
5234
5306
  function getCities(provinceQuery) {
5235
- const province = getProvince(provinceQuery);
5236
- return province ? province.cities : [];
5307
+ var _a, _b;
5308
+ return (_b = (_a = getProvince(provinceQuery)) == null ? void 0 : _a.cities) != null ? _b : [];
5237
5309
  }
5238
5310
  function searchProvinces(query, options = {}) {
5239
- var _a;
5240
- const q = normalize(query);
5241
- if (!q) return options.limit ? provinces.slice(0, options.limit) : provinces;
5242
- const locale = (_a = options.locale) != null ? _a : "both";
5243
- const results = provinces.filter((p) => {
5244
- const matchEn = locale !== "fa" && normalize(p.en).includes(q);
5245
- const matchFa = locale !== "en" && normalize(p.fa).includes(q);
5246
- return matchEn || matchFa;
5247
- });
5248
- return options.limit ? results.slice(0, options.limit) : results;
5311
+ return rankedSearch(searchableProvinces, query, options);
5249
5312
  }
5250
5313
  function searchCities(query, options = {}) {
5251
- var _a;
5252
- const q = normalize(query);
5253
- const all = getFlatCitiesInternal();
5254
- if (!q) return options.limit ? all.slice(0, options.limit) : all;
5255
- const locale = (_a = options.locale) != null ? _a : "both";
5256
- const results = all.filter((c) => {
5257
- const matchEn = locale !== "fa" && normalize(c.en).includes(q);
5258
- const matchFa = locale !== "en" && normalize(c.fa).includes(q);
5259
- return matchEn || matchFa;
5260
- });
5261
- return options.limit ? results.slice(0, options.limit) : results;
5314
+ return rankedSearch(searchableCities, query, options);
5262
5315
  }
5263
5316
 
5264
5317
  // src/react.ts