kenya-locations
Version:
A comprehensive and intuitive TypeScript package for working with Kenyan administrative divisions (counties, sub-counties, constituencies, and wards)
2 lines (1 loc) • 1.45 kB
JavaScript
;const n=require("fuse.js"),B=require("./counties-D9ZfM1Ad.cjs"),L=require("./constituencies-HrhCXHyg.cjs"),M=require("./wards-BHJ3nhLv.cjs"),O=require("./sub-counties-BGJlbhdB.cjs"),y=require("./area-zfFmEagr.cjs"),c={keys:["name"],threshold:.2,includeScore:!0,ignoreLocation:!0,findAllMatches:!1,minMatchCharLength:2};let d=null,p=null,f=null,h=null,m=null,w=null;function T(){d??(d=new n(B.counties,c)),p??(p=new n(L.constituencies,c)),f??(f=new n(M.wards,c)),h??(h=new n(O.subCounties,c)),m??(m=new n(y.localities,c)),w??(w=new n(y.areas,c))}function b(u,i={}){const{limit:r=10,types:e}=i;if(!u||u.trim().length<2)return[];T();const F=u.trim(),o=[],q=[{type:"county",fuse:d,enabled:!e||e.includes("county")},{type:"constituency",fuse:p,enabled:!e||e.includes("constituency")},{type:"ward",fuse:f,enabled:!e||e.includes("ward")},{type:"sub-county",fuse:h,enabled:!e||e.includes("sub-county")},{type:"locality",fuse:m,enabled:!e||e.includes("locality")},{type:"area",fuse:w,enabled:!e||e.includes("area")}];for(const{type:s,fuse:t,enabled:C}of q)if(C){const g=t.search(F,{limit:r*2});o.push(...g.map(a=>({type:s,item:a.item,score:a.score})))}const l={county:1,constituency:2,ward:3,"sub-county":4,locality:5,area:6};return o.sort((s,t)=>s.score!==t.score?(s.score||0)-(t.score||0):l[s.type]-l[t.type]).slice(0,r).map(({score:s,...t})=>t)}function k(u,i,r=10){return b(u,{limit:r,types:[i]})}exports.search=b;exports.searchByType=k;