UNPKG

fuse.js-ie11

Version:

Lightweight fuzzy-search

9 lines 13 kB
/** * Fuse.js v6.0.0 - Lightweight fuzzy-search (http://fusejs.io) * * Copyright (c) 2020 Kiro Risk (http://kiro.me) * All Rights Reserved. Apache Software License 2.0 * * http://www.apache.org/licenses/LICENSE-2.0 */ var e,t;e=this,t=function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function n(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,t){if(e){if("string"==typeof e)return a(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?a(e,t):void 0}}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function c(e){return Array.isArray?Array.isArray(e):"[object Array]"===Object.prototype.toString.call(e)}function h(e){return"string"==typeof e}function u(e){return"number"==typeof e}function d(e){return null!=e}function l(e){return!e.trim().length}var f=function(e){return"Pattern length exceeds max of ".concat(e,".")},v=Object.prototype.hasOwnProperty,y=function(){function t(n){var r=this;e(this,t),this._keys={},this._keyNames=[];var i=0;n.forEach((function(e){var t,n=1;if(h(e))t=e;else{if(!v.call(e,"name"))throw new Error("Missing ".concat("name"," property in key"));if(t=e.name,v.call(e,"weight")&&(n=e.weight)<=0)throw new Error(function(e){return"Property 'weight' in key '".concat(e,"' must be a positive integer")}(t))}r._keyNames.push(t),r._keys[t]={weight:n},i+=n})),this._keyNames.forEach((function(e){r._keys[e].weight/=i}))}return n(t,[{key:"get",value:function(e,t){return this._keys[e]&&this._keys[e][t]}},{key:"keys",value:function(){return this._keyNames}},{key:"toJSON",value:function(){return JSON.stringify(this._keys)}}]),t}(),g=o({},{isCaseSensitive:!1,includeScore:!1,keys:[],shouldSort:!0,sortFn:function(e,t){return e.score===t.score?e.idx<t.idx?-1:1:e.score<t.score?-1:1}},{},{includeMatches:!1,findAllMatches:!1,minMatchCharLength:1},{},{location:0,threshold:.6,distance:100},{},{useExtendedSearch:!1,getFn:function(e,t){var n=[],r=!1;return function e(t,i){if(i){var o=i.indexOf("."),s=i,a=null;-1!==o&&(s=i.slice(0,o),a=i.slice(o+1));var l=t[s];if(!d(l))return;if(a||!h(l)&&!u(l))if(c(l)){r=!0;for(var f=0,v=l.length;f<v;f+=1)e(l[f],a)}else a&&e(l,a);else n.push(function(e){return null==e?"":function(e){if("string"==typeof e)return e;var t=e+"";return"0"==t&&1/e==-1/0?"-0":t}(e)}(l))}else n.push(t)}(e,t),r?n:n[0]}}),p=/[^ ]+/g;function m(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:3,t=new Map;return{get:function(n){var r=n.match(p).length;if(t.has(r))return t.get(r);var i=parseFloat((1/Math.sqrt(r)).toFixed(e));return t.set(r,i),i},clear:function(){t.clear()}}}var k=function(){function t(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=n.getFn,i=void 0===r?g.getFn:r;e(this,t),this.norm=m(3),this.getFn=i,this.isCreated=!1,this.setRecords()}return n(t,[{key:"setCollection",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.docs=e}},{key:"setRecords",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.records=e}},{key:"setKeys",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.keys=e}},{key:"create",value:function(){var e=this;!this.isCreated&&this.docs.length&&(this.isCreated=!0,h(this.docs[0])?this.docs.forEach((function(t,n){e._addString(t,n)})):this.docs.forEach((function(t,n){e._addObject(t,n)})),this.norm.clear())}},{key:"add",value:function(e){var t=this.size();h(e)?this._addString(e,t):this._addObject(e,t)}},{key:"removeAt",value:function(e){this.records.splice(e,1);for(var t=e,n=this.size();t<n;t+=1)this.records[t].i-=1}},{key:"size",value:function(){return this.records.length}},{key:"_addString",value:function(e,t){if(d(e)&&!l(e)){var n={v:e,i:t,n:this.norm.get(e)};this.records.push(n)}}},{key:"_addObject",value:function(e,t){var n=this,r={i:t,$:{}};this.keys.forEach((function(t,i){var o=n.getFn(e,t);if(d(o))if(c(o))!function(){for(var e=[],t=[{nestedArrIndex:-1,value:o}];t.length;){var s=t.pop(),a=s.nestedArrIndex,u=s.value;if(d(u))if(h(u)&&!l(u)){var f={v:u,i:a,n:n.norm.get(u)};e.push(f)}else c(u)&&u.forEach((function(e,n){t.push({nestedArrIndex:n,value:e})}))}r.$[i]=e}();else if(!l(o)){var s={v:o,n:n.norm.get(o)};r.$[i]=s}})),this.records.push(r)}},{key:"toJSON",value:function(){return{keys:this.keys,records:this.records}}}]),t}();function b(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r=n.getFn,i=void 0===r?g.getFn:r,o=new k({getFn:i});return o.setKeys(e),o.setCollection(t),o.create(),o}function M(e,t){var n=e.matches;t.matches=[],d(n)&&n.forEach((function(e){if(d(e.indices)&&e.indices.length){var n={indices:e.indices,value:e.value};e.key&&(n.key=e.key),e.idx>-1&&(n.refIndex=e.idx),t.matches.push(n)}}))}function x(e,t){t.score=e.score}function w(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.errors,r=void 0===n?0:n,i=t.currentLocation,o=void 0===i?0:i,s=t.expectedLocation,a=void 0===s?0:s,c=t.distance,h=void 0===c?g.distance:c,u=r/e.length,d=Math.abs(a-o);return h?u+d/h:d?1:u}function _(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:g.minMatchCharLength,n=[],r=-1,i=-1,o=0,s=e.length;o<s;o+=1){var a=e[o];a&&-1===r?r=o:a||-1===r||((i=o-1)-r+1>=t&&n.push([r,i]),r=-1)}return e[o-1]&&o-r>=t&&n.push([r,o-1]),n}function O(e){for(var t={},n=0,r=e.length;n<r;n+=1){var i=e.charAt(n);t[i]=(t[i]||0)|1<<r-n-1}return t}var S=function(){function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=r.location,o=void 0===i?g.location:i,s=r.threshold,a=void 0===s?g.threshold:s,c=r.distance,h=void 0===c?g.distance:c,u=r.includeMatches,d=void 0===u?g.includeMatches:u,l=r.findAllMatches,f=void 0===l?g.findAllMatches:l,v=r.minMatchCharLength,y=void 0===v?g.minMatchCharLength:v,p=r.isCaseSensitive,m=void 0===p?g.isCaseSensitive:p;e(this,t),this.options={location:o,threshold:a,distance:h,includeMatches:d,findAllMatches:f,minMatchCharLength:y,isCaseSensitive:m},this.pattern=m?n:n.toLowerCase(),this.chunks=[];for(var k=0;k<this.pattern.length;){var b=this.pattern.substring(k,k+32);this.chunks.push({pattern:b,alphabet:O(b)}),k+=32}}return n(t,[{key:"searchIn",value:function(e){var t=this.options,n=t.isCaseSensitive,r=t.includeMatches;if(n||(e=e.toLowerCase()),this.pattern===e){var i={isMatch:!0,score:0};return r&&(i.indices=[[0,e.length-1]]),i}var o=this.options,a=o.location,c=o.distance,h=o.threshold,u=o.findAllMatches,d=o.minMatchCharLength,l=[],v=0,y=!1;this.chunks.forEach((function(t,n){var i=t.pattern,o=t.alphabet,p=function(e,t,n){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},i=r.location,o=void 0===i?g.location:i,s=r.distance,a=void 0===s?g.distance:s,c=r.threshold,h=void 0===c?g.threshold:c,u=r.findAllMatches,d=void 0===u?g.findAllMatches:u,l=r.minMatchCharLength,v=void 0===l?g.minMatchCharLength:l,y=r.includeMatches,p=void 0===y?g.includeMatches:y;if(t.length>32)throw new Error(f(32));var m,k=t.length,b=e.length,M=Math.max(0,Math.min(o,b)),x=h,O=M,S=[];if(p)for(var A=0;A<b;A+=1)S[A]=0;for(;(m=e.indexOf(t,O))>-1;){var E=w(t,{currentLocation:m,expectedLocation:M,distance:a});if(x=Math.min(E,x),O=m+k,p)for(var L=0;L<k;)S[m+L]=1,L+=1}O=-1;for(var j=[],C=1,I=k+b,F=1<<(k<=31?k-1:30),P=0;P<k;P+=1){for(var N=0,$=I;N<$;){var D=w(t,{errors:P,currentLocation:M+$,expectedLocation:M,distance:a});D<=x?N=$:I=$,$=Math.floor((I-N)/2+N)}I=$;var z=Math.max(1,M-$+1),J=d?b:Math.min(M+$,b)+k,K=Array(J+2);K[J+1]=(1<<P)-1;for(var R=J;R>=z;R-=1){var T=R-1,q=n[e.charAt(T)];if(q&&p&&(S[T]=1),K[R]=(K[R+1]<<1|1)&q,0!==P&&(K[R]|=(j[R+1]|j[R])<<1|1|j[R+1]),K[R]&F&&(C=w(t,{errors:P,currentLocation:T,expectedLocation:M,distance:a}))<=x){if(x=C,(O=T)<=M)break;z=Math.max(1,2*M-O)}}var U=w(t,{errors:P+1,currentLocation:M,expectedLocation:M,distance:a});if(U>x)break;j=K}var B={isMatch:O>=0,score:Math.max(.001,C)};return p&&(B.indices=_(S,v)),B}(e,i,o,{location:a+32*n,distance:c,threshold:h,findAllMatches:u,minMatchCharLength:d,includeMatches:r}),m=p.isMatch,k=p.score,b=p.indices;m&&(y=!0),v+=k,m&&b&&(l=[].concat(s(l),s(b)))}));var p={isMatch:y,score:y?v/this.chunks.length:1};return y&&r&&(p.indices=l),p}}]),t}(),A=[];function E(e,t){for(var n=0,r=A.length;n<r;n+=1){var i=A[n];if(i.condition(e,t))return new i(e,t)}return new S(e,t)}var L=function(){function t(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=arguments.length>2?arguments[2]:void 0;if(e(this,t),this.options=o({},g,{},r),this.options.useExtendedSearch)throw new Error("Extended search is not available");this._keyStore=new y(this.options.keys),this.setCollection(n,i)}return n(t,[{key:"setCollection",value:function(e,t){if(this._docs=e,t&&!(t instanceof k))throw new Error("Incorrect 'index' type");this._myIndex=t||b(this._keyStore.keys(),this._docs,{getFn:this.options.getFn})}},{key:"add",value:function(e){d(e)&&(this._docs.push(e),this._myIndex.add(e))}},{key:"removeAt",value:function(e){this._docs.splice(e,1),this._myIndex.removeAt(e)}},{key:"getIndex",value:function(){return this._myIndex}},{key:"search",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.limit,r=void 0===n?-1:n,i=this.options,o=i.includeMatches,s=i.includeScore,a=i.shouldSort,c=i.sortFn,d=h(e)?h(this._docs[0])?this._searchStringList(e):this._searchObjectList(e):this._searchLogical(e);return j(d,this._keyStore),a&&d.sort(c),u(r)&&r>-1&&(d=d.slice(0,r)),C(d,this._docs,{includeMatches:o,includeScore:s})}},{key:"_searchStringList",value:function(e){var t=E(e,this.options),n=this._myIndex.records,r=[];return n.forEach((function(e){var n=e.v,i=e.i,o=e.n;if(d(n)){var s=t.searchIn(n),a=s.isMatch,c=s.score,h=s.indices;a&&r.push({item:n,idx:i,matches:[{score:c,value:n,norm:o,indices:h}]})}})),r}},{key:"_searchLogical",value:function(e){throw new Error("Logical search is not available")}},{key:"_searchObjectList",value:function(e){var t=this,n=E(e,this.options),r=this._myIndex,i=r.keys,o=r.records,a=[];return o.forEach((function(e){var r=e.$,o=e.i;if(d(r)){var c=[];i.forEach((function(e,i){c.push.apply(c,s(t._findMatches({key:e,value:r[i],searcher:n})))})),c.length&&a.push({idx:o,item:r,matches:c})}})),a}},{key:"_findMatches",value:function(e){var t=e.key,n=e.value,r=e.searcher;if(!d(n))return[];var i=[];if(c(n))n.forEach((function(e){var n=e.v,o=e.i,s=e.n;if(d(n)){var a=r.searchIn(n),c=a.isMatch,h=a.score,u=a.indices;c&&i.push({score:h,key:t,value:n,idx:o,norm:s,indices:u})}}));else{var o=n.v,s=n.n,a=r.searchIn(o),h=a.isMatch,u=a.score,l=a.indices;h&&i.push({score:u,key:t,value:o,norm:s,indices:l})}return i}}]),t}();function j(e,t){e.forEach((function(e){var n=1;e.matches.forEach((function(e){var r=e.key,i=e.norm,o=e.score,s=t.get(r,"weight");n*=Math.pow(0===o&&s?Number.EPSILON:o,(s||1)*i)})),e.score=n}))}function C(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r=n.includeMatches,i=void 0===r?g.includeMatches:r,o=n.includeScore,s=void 0===o?g.includeScore:o,a=[];return i&&a.push(M),s&&a.push(x),e.map((function(e){var n=e.idx,r={item:t[n],refIndex:n};return a.length&&a.forEach((function(t){t(e,r)})),r}))}return L.version="6.0.0",L.createIndex=b,L.parseIndex=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.getFn,r=void 0===n?g.getFn:n,i=e.keys,o=e.records,s=new k({getFn:r});return s.setKeys(i),s.setRecords(o),s},L.config=g,L},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Fuse=t();