blob: 95e1d8009a387d4f9397f1a46ed9d02bfba65c14 [file] [log] [blame]
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r,t,s=e(require("leven")),n=e(require("lodash.deburr"));(r=exports.ReturnTypeEnums||(exports.ReturnTypeEnums={})).ALL_CLOSEST_MATCHES="all-closest-matches",r.ALL_MATCHES="all-matches",r.ALL_SORTED_MATCHES="all-sorted-matches",r.FIRST_CLOSEST_MATCH="first-closest-match",r.FIRST_MATCH="first-match",(t=exports.ThresholdTypeEnums||(exports.ThresholdTypeEnums={})).EDIT_DISTANCE="edit-distance",t.SIMILARITY="similarity";const o=new Error("unknown returnType"),T=new Error("unknown thresholdType"),u=(e,r)=>{let t=e;return r.trimSpaces&&(t=t.trim().replace(/\s+/g," ")),r.deburr&&(t=n(t)),r.caseSensitive||(t=t.toLowerCase()),t},h=(e,r)=>{const{matchPath:t}=r,s=((e,r)=>{const t=Array.isArray(r)&&r.length?r.reduce((e,r)=>null==e?void 0:e[r],e):e;return"string"!=typeof t?"":t})(e,t);return u(s,r)};exports.default=function(e,r,t){const n=(e=>{const r={caseSensitive:!1,deburr:!0,matchPath:[],returnType:exports.ReturnTypeEnums.FIRST_CLOSEST_MATCH,thresholdType:exports.ThresholdTypeEnums.SIMILARITY,trimSpaces:!0,...e};switch(r.thresholdType){case exports.ThresholdTypeEnums.EDIT_DISTANCE:return{threshold:20,...r};case exports.ThresholdTypeEnums.SIMILARITY:return{threshold:.4,...r};default:throw T}})(t),{returnType:p,threshold:a,thresholdType:c}=n,l=u(e,n);let E,S;switch(c){case exports.ThresholdTypeEnums.EDIT_DISTANCE:E=e=>e<=a,S=e=>s(l,h(e,n));break;case exports.ThresholdTypeEnums.SIMILARITY:E=e=>e>=a,S=e=>((e,r)=>{if(!e||!r)return 0;if(e===r)return 1;const t=s(e,r),n=Math.max(e.length,r.length);return(n-t)/n})(l,h(e,n));break;default:throw T}const d=[],A=r.length;switch(p){case exports.ReturnTypeEnums.ALL_CLOSEST_MATCHES:case exports.ReturnTypeEnums.FIRST_CLOSEST_MATCH:{const e=[];let t;switch(c){case exports.ThresholdTypeEnums.EDIT_DISTANCE:t=1/0;for(let s=0;s<A;s+=1){const n=S(r[s]);t>n&&(t=n),e.push(n)}break;case exports.ThresholdTypeEnums.SIMILARITY:t=0;for(let s=0;s<A;s+=1){const n=S(r[s]);t<n&&(t=n),e.push(n)}break;default:throw T}const s=e.length;for(let r=0;r<s;r+=1){const s=e[r];E(s)&&s===t&&d.push(r)}break}case exports.ReturnTypeEnums.ALL_MATCHES:for(let e=0;e<A;e+=1){E(S(r[e]))&&d.push(e)}break;case exports.ReturnTypeEnums.ALL_SORTED_MATCHES:{const e=[];for(let t=0;t<A;t+=1){const s=S(r[t]);E(s)&&e.push({score:s,index:t})}switch(c){case exports.ThresholdTypeEnums.EDIT_DISTANCE:e.sort((e,r)=>e.score-r.score);break;case exports.ThresholdTypeEnums.SIMILARITY:e.sort((e,r)=>r.score-e.score);break;default:throw T}for(const r of e)d.push(r.index);break}case exports.ReturnTypeEnums.FIRST_MATCH:for(let e=0;e<A;e+=1){if(E(S(r[e]))){d.push(e);break}}break;default:throw o}return((e,r,t)=>{switch(t){case exports.ReturnTypeEnums.ALL_CLOSEST_MATCHES:case exports.ReturnTypeEnums.ALL_MATCHES:case exports.ReturnTypeEnums.ALL_SORTED_MATCHES:return r.map(r=>e[r]);case exports.ReturnTypeEnums.FIRST_CLOSEST_MATCH:case exports.ReturnTypeEnums.FIRST_MATCH:return r.length?e[r[0]]:null;default:return null}})(r,d,p)};
//# sourceMappingURL=index.cjs.js.map