UNPKG

fuzzball

Version:

Fuzzy string matching algorithms and utilities, port of the TheFuzz Python library.

2 lines (1 loc) 12.4 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).fuzzball=t()}(this,(function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},t={exports:{}};var r,o={_intersect:function _intersect(e,t){if(!e||!t||0===e.length||0===t.length)return[];if(e.length<100&&t.length<100)return e.filter((e=>t.includes(e)));const r=new Set(t);return e.filter((e=>r.has(e)))},_intersectWith:function _intersectWith(e,t,r){return e&&t&&0!==e.length&&0!==t.length?e.filter((e=>t.some((t=>r(e,t))))):[]},_difference:function _difference(e,t){if(!e)return[];if(!t||0===t.length)return e.slice();if(e.length<100&&t.length<100)return e.filter((e=>!t.includes(e)));const r=new Set(t);return e.filter((e=>!r.has(e)))},_differenceWith:function _differenceWith(e,t,r){return e?t&&0!==t.length?e.filter((e=>!t.some((t=>r(e,t))))):e.slice():[]},_uniq:function _uniq(e){return e&&0!==e.length?1===e.length?e.slice():[...new Set(e)]:[]},_uniqWith:function _uniqWith(e,t){if(!e||0===e.length)return[];if(1===e.length)return e.slice();const r=[];e:for(let o=0;o<e.length;o++){const n=e[o];for(let e=0;e<r.length;e++)if(t(n,r[e]))continue e;r.push(n)}return r},_partialRight:function _partialRight(e){const t=Array.prototype.slice.call(arguments,1);return function(){const r=Array.prototype.slice.call(arguments);return e.apply(this,r.concat(t))}},_forEach:function _forEach(e,t){if(e)if(Array.isArray(e))for(let r=0;r<e.length;r++)t(e[r],r);else{const r=Object.keys(e);for(let o=0;o<r.length;o++)t(e[r[o]],r[o])}},_isArray:Array.isArray};try{r="undefined"!=typeof Intl&&void 0!==Intl.Collator?Intl.Collator("generic",{sensitivity:"base"}):null}catch(e){void 0!==typeof console&&console.warn("Collator could not be initialized and wouldn't be used")}var n=function leven(e,t,o){var n=[],s=[],i=o&&r&&o.useCollator,c=1;if(o&&o.subcost&&"number"==typeof o.subcost&&(c=o.subcost),e===t)return 0;var a,l,f,u,p=e.length,d=t.length;if(0===p)return d;if(0===d)return p;for(var h=0,_=0;h<p;)s[h]=e.charCodeAt(h),n[h]=++h;if(i)for(;_<d;)for(a=t.charCodeAt(_),f=_++,l=_,h=0;h<p;h++)u=0===r.compare(String.fromCharCode(a),String.fromCharCode(s[h]))?f:f+c,f=n[h],l=n[h]=f>l?u>l?l+1:u:u>f?f+1:u;else for(;_<d;)for(a=t.charCodeAt(_),f=_++,l=_,h=0;h<p;h++)u=a===s[h]?f:f+c,f=n[h],l=n[h]=f>l?u>l?l+1:u:u>f?f+1:u;return l};return function(e,t){if(!e.setImmediate){var r,o=1,n={},s=!1,i=e.document,c=Object.getPrototypeOf&&Object.getPrototypeOf(e);c=c&&c.setTimeout?c:e,"[object process]"==={}.toString.call(e.process)?function installNextTickImplementation(){r=function(e){process.nextTick((function(){runIfPresent(e)}))}}():!function canUsePostMessage(){if(e.postMessage&&!e.importScripts){var t=!0,r=e.onmessage;return e.onmessage=function(){t=!1},e.postMessage("","*"),e.onmessage=r,t}}()?e.MessageChannel?function installMessageChannelImplementation(){var e=new MessageChannel;e.port1.onmessage=function(e){runIfPresent(e.data)},r=function(t){e.port2.postMessage(t)}}():i&&"onreadystatechange"in i.createElement("script")?function installReadyStateChangeImplementation(){var e=i.documentElement;r=function(t){var r=i.createElement("script");r.onreadystatechange=function(){runIfPresent(t),r.onreadystatechange=null,e.removeChild(r),r=null},e.appendChild(r)}}():function installSetTimeoutImplementation(){r=function(e){setTimeout(runIfPresent,0,e)}}():function installPostMessageImplementation(){var t="setImmediate$"+Math.random()+"$",onGlobalMessage=function(r){r.source===e&&"string"==typeof r.data&&0===r.data.indexOf(t)&&runIfPresent(+r.data.slice(t.length))};e.addEventListener?e.addEventListener("message",onGlobalMessage,!1):e.attachEvent("onmessage",onGlobalMessage),r=function(r){e.postMessage(t+r,"*")}}(),c.setImmediate=function setImmediate(e){"function"!=typeof e&&(e=new Function(""+e));for(var t=new Array(arguments.length-1),s=0;s<t.length;s++)t[s]=arguments[s+1];var i={callback:e,args:t};return n[o]=i,r(o),o++},c.clearImmediate=clearImmediate}function clearImmediate(e){delete n[e]}function runIfPresent(e){if(s)setTimeout(runIfPresent,0,e);else{var t=n[e];if(t){s=!0;try{!function run(e){var t=e.callback,r=e.args;switch(r.length){case 0:t();break;case 1:t(r[0]);break;case 2:t(r[0],r[1]);break;case 3:t(r[0],r[1],r[2]);break;default:t.apply(void 0,r)}}(t)}finally{clearImmediate(e),s=!1}}}}}("undefined"==typeof self?e:self),function(){var e,r,s,i,c=o._intersect,a=o._difference,l=n,f=function(e){var t={};function validate(e){return("string"==typeof e||e instanceof String)&&e.length>0}return t.validate=validate,t.process_and_sort=function process_and_sort(e){return validate(e)?e.match(/\S+/g).sort().join(" ").trim():""},t.tokenize=function unique_tokens(t,r){return e(t.match(/\S+/g))},t.full_process=function full_process(e,t){return e instanceof String||"string"==typeof e?(t&&(t.force_ascii||!0===t)&&(e=e.replace(/[^\x00-\x7F]/g,"")),r=e.replace(/\W|_/g," ").toLowerCase().trim(),t&&t.collapseWhitespace&&(r=r.replace(/\s+/g," ")),r):"";var r},t.clone_and_set_option_defaults=function(e){if(e&&e.isAClone)return e;var t={isAClone:!0};if(e){var r,o=Object.keys(e);for(r=0;r<o.length;r++)t[o[r]]=e[o[r]]}return void 0!==t.full_process&&!1===t.full_process||(t.full_process=!0),void 0!==t.force_ascii&&!0===t.force_ascii||(t.force_ascii=!1),void 0!==t.collapseWhitespace&&!1===t.collapseWhitespace||(t.collapseWhitespace=!0),t},t.isCustomFunc=function(e){return"function"!=typeof e||"token_set_ratio"!==e.name&&"partial_token_set_ratio"!==e.name&&"token_sort_ratio"!==e.name&&"partial_token_sort_ratio"!==e.name&&"QRatio"!==e.name&&"WRatio"!==e.name&&"distance"!==e.name&&"partial_ratio"!==e.name},t}(o._uniq),u=f.validate,p=f.process_and_sort,d=f.tokenize,h=f.full_process,_=f.clone_and_set_option_defaults,m=f.isCustomFunc;function QRatio(e,t,r){var o=_(r);return e=o.full_process?h(e,o):e,t=o.full_process?h(t,o):t,u(e)&&u(t)?_ratio(e,t,o):0}function extractAsync(e,t,r,o){var n,s,i=_(r);"object"==typeof r.abortController&&(n=r.abortController),"object"==typeof r.cancelToken&&(s=r.cancelToken);var c=256;"number"==typeof i.asyncLoopOffset&&(c=i.asyncLoopOffset<1?1:i.asyncLoopOffset);var a,l=!1;if(t&&t.length&&Array.isArray(t))a=t.length,l=!0;else{if(!(t instanceof Object))return void o(new Error("Invalid choices"));a=Object.keys(t).length}if(!t||0===a)return void 0!==typeof console&&console.warn("No choices"),void o(null,[]);if(i.processor&&"function"!=typeof i.processor)o(new Error("Invalid Processor"));else if(i.processor||(i.processor=function(e){return e}),i.scorer&&"function"!=typeof i.scorer)o(new Error("Invalid Scorer"));else{i.scorer||(i.scorer=QRatio);var f=m(i.scorer);i.cutoff&&"number"==typeof i.cutoff||(i.cutoff=-1);var pre_processor=function(e,t){return e};i.full_process&&(pre_processor=h,f||(i.processed=!0)),f||(e=pre_processor(e,i),i.full_process=!1,0===e.length&&void 0!==typeof console&&console.warn("Processed query is empty string"));var u,y,g,v,b=[],k=!1,w=!1,I=!1;if("token_sort_ratio"===i.scorer.name||"partial_token_sort_ratio"===i.scorer.name){var O=p(e);w=!0}else if("token_set_ratio"===i.scorer.name||"partial_token_set_ratio"===i.scorer.name){var j=d(e);I=!0}v=i.returnObjects?function(e,t){return t.score-e.score}:function(e,t){return t[1]-e[1]};var A=Object.keys(t);l?searchLoop(0):searchLoop(A[0],0)}function searchLoop(r,h){(l||t.hasOwnProperty(r))&&(i.tokens=void 0,i.proc_sorted=!1,w?(i.proc_sorted=!0,t[r].proc_sorted?y=t[r].proc_sorted:(y=pre_processor(i.processor(t[r]),i),y=p(y)),g=i.scorer(O,y,i)):I?(y="x",t[r].tokens?(i.tokens=[j,t[r].tokens],i.trySimple&&(y=pre_processor(i.processor(t[r]),i))):(y=pre_processor(i.processor(t[r]),i),i.tokens=[j,d(y)]),g=i.scorer(e,y,i)):f?(y=i.processor(t[r]),g=i.scorer(e,y,i)):("string"==typeof(y=pre_processor(i.processor(t[r]),i))&&0!==y.length||(k=!0),g=i.scorer(e,y,i)),u=l?parseInt(r):r,g>i.cutoff&&(i.returnObjects?b.push({choice:t[r],score:g,key:u}):b.push([t[r],g,u]))),n&&!0===n.signal.aborted?o(new Error("aborted")):s&&!0===s.canceled?o(new Error("canceled")):l&&r<t.length-1?r%c==0?setImmediate((function(){searchLoop(r+1)})):searchLoop(r+1):h<A.length-1?h%c==0?setImmediate((function(){searchLoop(A[h+1],h+1)})):searchLoop(A[h+1],h+1):(k&&void 0!==typeof console&&console.log("One or more choices were empty. (post-processing if applied)"),i.limit&&"number"==typeof i.limit&&i.limit>0&&i.limit<a&&!i.unsorted?b=b.sort(v).slice(0,i.limit):i.unsorted||(b=b.sort(v)),o(null,b))}}function _ratio(e,t,r){return u(e)&&u(t)?(void 0===r.subcost&&(r.subcost=2),o=l(e,t,r),n=e.length+t.length,Math.round((n-o)/n*100)):0;var o,n}void 0===Array.isArray&&(Array.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)}),Object.keys||(Object.keys=(e=Object.prototype.hasOwnProperty,r=!{toString:null}.propertyIsEnumerable("toString"),i=(s=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"]).length,function(t){if("object"!=typeof t&&("function"!=typeof t||null===t))throw new TypeError("Object.keys called on non-object");var o,n,c=[];for(o in t)e.call(t,o)&&c.push(o);if(r)for(n=0;n<i;n++)e.call(t,s[n])&&c.push(s[n]);return c}));var y=void 0;"undefined"!=typeof Promise&&(y=function(e,t,r){return new Promise((function(o,n){extractAsync(e,t,r,(function(e,t){e?n(e):o(t)}))}))});var g={distance:function distance(e,t,r){var o=_(r);return e=o.full_process?h(e,o):e,t=o.full_process?h(t,o):t,l(e,t,o)},ratio:QRatio,token_set_ratio:function token_set_ratio(e,t,r){var o=_(r);return e=o.full_process?h(e,o):e,t=o.full_process?h(t,o):t,u(e)&&u(t)?function _token_set(e,t,r){if(r.tokens)o=r.tokens[0],n=r.tokens[1];else var o=d(e),n=d(t);var s=c(o,n),i=a(o,n),l=a(n,o),f=s.sort().join(" "),u=i.sort().join(" "),p=l.sort().join(" "),h=f+" "+u,_=f+" "+p;f=f.trim(),h=h.trim(),_=_.trim();var m=_ratio,y=[m(f,h,r),m(f,_,r),m(h,_,r)];r.trySimple&&y.push(m(e,t,r));return Math.max.apply(null,y)}(e,t,o):0},token_sort_ratio:function token_sort_ratio(e,t,r){var o=_(r);return e=o.full_process?h(e,o):e,t=o.full_process?h(t,o):t,u(e)&&u(t)?(o.proc_sorted||(e=p(e),t=p(t)),_ratio(e,t,o)):0},full_process:h,extract:function extract(e,t,r){var o,n=_(r),s=!1;if(t&&t.length&&Array.isArray(t))o=t.length,s=!0;else{if(!(t instanceof Object))throw new Error("Invalid choices");o=Object.keys(t).length}if(!t||0===o)return void 0!==typeof console&&console.warn("No choices"),[];if(n.processor&&"function"!=typeof n.processor)throw new Error("Invalid Processor");if(n.processor||(n.processor=function(e){return e}),n.scorer&&"function"!=typeof n.scorer)throw new Error("Invalid Scorer");n.scorer||(n.scorer=QRatio);var i=m(n.scorer);n.cutoff&&"number"==typeof n.cutoff||(n.cutoff=-1);var pre_processor=function(e,t){return e};n.full_process&&(pre_processor=h,i||(n.processed=!0)),i||(e=pre_processor(e,n),n.full_process=!1,0===e.length&&void 0!==typeof console&&console.warn("Processed query is empty string"));var c,a,l,f,u=[],y=!1,g=!1,v=!1;if("token_sort_ratio"===n.scorer.name||"partial_token_sort_ratio"===n.scorer.name){var b=p(e);g=!0}else if("token_set_ratio"===n.scorer.name||"partial_token_set_ratio"===n.scorer.name){var k=d(e);v=!0}for(var w in f=n.returnObjects?function(e,t){return t.score-e.score}:function(e,t){return t[1]-e[1]},t)(s||t.hasOwnProperty(w))&&(n.tokens=void 0,n.proc_sorted=!1,g?(n.proc_sorted=!0,t[w]&&t[w].proc_sorted?a=t[w].proc_sorted:(a=pre_processor(n.processor(t[w]),n),a=p(a)),l=n.scorer(b,a,n)):v?(a="x",t[w]&&t[w].tokens?(n.tokens=[k,t[w].tokens],n.trySimple&&(a=pre_processor(n.processor(t[w]),n))):(a=pre_processor(n.processor(t[w]),n),n.tokens=[k,d(a)]),l=n.scorer(e,a,n)):i?(a=n.processor(t[w]),l=n.scorer(e,a,n)):("string"==typeof(a=pre_processor(n.processor(t[w]),n))&&0!==a.length||(y=!0),l=n.scorer(e,a,n)),c=s?parseInt(w):w,l>n.cutoff&&(n.returnObjects?u.push({choice:t[w],score:l,key:c}):u.push([t[w],l,c])));return y&&void 0!==typeof console&&console.log("One or more choices were empty. (post-processing if applied)"),n.limit&&"number"==typeof n.limit&&n.limit>0&&n.limit<o&&!n.unsorted?u=u.sort(f).slice(0,n.limit):n.unsorted||(u=u.sort(f)),u},extractAsync:extractAsync,extractAsPromised:y,process_and_sort:p,unique_tokens:d};t.exports=g}(),t.exports}));