@vuex-orm/plugin-search
Version:
Vuex ORM plugin for adding fuzzy search feature through model entities.
1 lines • 12.6 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).VuexORMSearch=t()}(this,function(){"use strict";var r=function(){return(r=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},n={distance:100,location:0,maxPatternLength:32,minMatchCharLength:1,searchPrimaryKey:!1,shouldSort:!1,threshold:.3,tokenize:!1,keys:[],verbose:!1};var e,t,o=(function(e,t){e.exports=function(r){var n={};function o(e){if(n[e])return n[e].exports;var t=n[e]={i:e,l:!1,exports:{}};return r[e].call(t.exports,t,t.exports,o),t.l=!0,t.exports}return o.m=r,o.c=n,o.d=function(e,t,r){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(r,n,function(e){return t[e]}.bind(null,n));return r},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=1)}([function(e,t){e.exports=function(e){return Array.isArray?Array.isArray(e):"[object Array]"===Object.prototype.toString.call(e)}},function(e,t,r){function l(e){return(l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var i=r(2),K=r(8),Q=r(0),n=function(){function q(e,t){var r=t.location,n=void 0===r?0:r,o=t.distance,i=void 0===o?100:o,a=t.threshold,s=void 0===a?.6:a,c=t.maxPatternLength,h=void 0===c?32:c,l=t.caseSensitive,u=void 0!==l&&l,f=t.tokenSeparator,p=void 0===f?/ +/g:f,d=t.findAllMatches,v=void 0!==d&&d,y=t.minMatchCharLength,g=void 0===y?1:y,m=t.id,k=void 0===m?null:m,x=t.keys,S=void 0===x?[]:x,b=t.shouldSort,M=void 0===b||b,_=t.getFn,O=void 0===_?K:_,w=t.sortFn,L=void 0===w?function(e,t){return e.score-t.score}:w,A=t.tokenize,C=void 0!==A&&A,I=t.matchAllTokens,P=void 0!==I&&I,j=t.includeMatches,T=void 0!==j&&j,z=t.includeScore,F=void 0!==z&&z,Q=t.verbose,E=void 0!==Q&&Q;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,q),this.options={location:n,distance:i,threshold:s,maxPatternLength:h,isCaseSensitive:u,tokenSeparator:p,findAllMatches:v,minMatchCharLength:g,id:k,keys:S,includeMatches:T,includeScore:F,shouldSort:M,getFn:O,sortFn:L,verbose:E,tokenize:C,matchAllTokens:P},this.setCollection(e)}return function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(q.prototype,[{key:"setCollection",value:function(e){return this.list=e}},{key:"search",value:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{limit:!1};this._log('---------\nSearch pattern: "'.concat(e,'"'));var r=this._prepareSearchers(e),n=r.tokenSearchers,o=r.fullSearcher,i=this._search(n,o),a=i.weights,s=i.results;return this._computeScore(a,s),this.options.shouldSort&&this._sort(s),t.limit&&"number"==typeof t.limit&&(s=s.slice(0,t.limit)),this._format(s)}},{key:"_prepareSearchers",value:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",t=[];if(this.options.tokenize)for(var r=e.split(this.options.tokenSeparator),n=0,o=r.length;n<o;n+=1)t.push(new i(r[n],this.options));return{tokenSearchers:t,fullSearcher:new i(e,this.options)}}},{key:"_search",value:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:[],t=1<arguments.length?arguments[1]:void 0,r=this.list,n={},o=[];if("string"==typeof r[0]){for(var i=0,a=r.length;i<a;i+=1)this._analyze({key:"",value:r[i],record:i,index:i},{resultMap:n,results:o,tokenSearchers:e,fullSearcher:t});return{weights:null,results:o}}for(var s={},c=0,h=r.length;c<h;c+=1)for(var l=r[c],u=0,f=this.options.keys.length;u<f;u+=1){var p=this.options.keys[u];if("string"!=typeof p){if(s[p.name]={weight:1-p.weight||1},p.weight<=0||1<p.weight)throw new Error("Key weight has to be > 0 and <= 1");p=p.name}else s[p]={weight:1};this._analyze({key:p,value:this.options.getFn(l,p),record:l,index:c},{resultMap:n,results:o,tokenSearchers:e,fullSearcher:t})}return{weights:s,results:o}}},{key:"_analyze",value:function(e,t){var r=e.key,n=e.arrayIndex,o=void 0===n?-1:n,i=e.value,a=e.record,s=e.index,c=t.tokenSearchers,h=void 0===c?[]:c,l=t.fullSearcher,u=void 0===l?[]:l,f=t.resultMap,p=void 0===f?{}:f,d=t.results,v=void 0===d?[]:d;if(null!=i){var y=!1,g=-1,m=0;if("string"==typeof i){this._log("\nKey: ".concat(""===r?"-":r));var k=u.search(i);if(this._log('Full text: "'.concat(i,'", score: ').concat(k.score)),this.options.tokenize){for(var x=i.split(this.options.tokenSeparator),S=[],b=0;b<h.length;b+=1){var M=h[b];this._log('\nPattern: "'.concat(M.pattern,'"'));for(var _=!1,O=0;O<x.length;O+=1){var w=x[O],L=M.search(w),A={};L.isMatch?(A[w]=L.score,_=y=!0,S.push(L.score)):(A[w]=1,this.options.matchAllTokens||S.push(1)),this._log('Token: "'.concat(w,'", score: ').concat(A[w]))}_&&(m+=1)}g=S[0];for(var C=S.length,I=1;I<C;I+=1)g+=S[I];g/=C,this._log("Token score average:",g)}var P=k.score;-1<g&&(P=(P+g)/2),this._log("Score average:",P);var j=!this.options.tokenize||!this.options.matchAllTokens||m>=h.length;if(this._log("\nCheck Matches: ".concat(j)),(y||k.isMatch)&&j){var T=p[s];T?T.output.push({key:r,arrayIndex:o,value:i,score:P,matchedIndices:k.matchedIndices}):(p[s]={item:a,output:[{key:r,arrayIndex:o,value:i,score:P,matchedIndices:k.matchedIndices}]},v.push(p[s]))}}else if(Q(i))for(var z=0,F=i.length;z<F;z+=1)this._analyze({key:r,arrayIndex:z,value:i[z],record:a,index:s},{resultMap:p,results:v,tokenSearchers:h,fullSearcher:u})}}},{key:"_computeScore",value:function(e,t){this._log("\n\nComputing score:\n");for(var r=0,n=t.length;r<n;r+=1){for(var o=t[r].output,i=o.length,a=1,s=1,c=0;c<i;c+=1){var h=e?e[o[c].key].weight:1,l=(1===h?o[c].score:o[c].score||.001)*h;1!==h?s=Math.min(s,l):(o[c].nScore=l,a*=l)}t[r].score=1===s?a:s,this._log(t[r])}}},{key:"_sort",value:function(e){this._log("\n\nSorting...."),e.sort(this.options.sortFn)}},{key:"_format",value:function(e){var t=[];if(this.options.verbose){var r=[];this._log("\n\nOutput:\n\n",JSON.stringify(e,function(e,t){if("object"===l(t)&&null!==t){if(-1!==r.indexOf(t))return;r.push(t)}return t})),r=null}var n=[];this.options.includeMatches&&n.push(function(e,t){var r=e.output;t.matches=[];for(var n=0,o=r.length;n<o;n+=1){var i=r[n];if(0!==i.matchedIndices.length){var a={indices:i.matchedIndices,value:i.value};i.key&&(a.key=i.key),i.hasOwnProperty("arrayIndex")&&-1<i.arrayIndex&&(a.arrayIndex=i.arrayIndex),t.matches.push(a)}}}),this.options.includeScore&&n.push(function(e,t){t.score=e.score});for(var o=0,i=e.length;o<i;o+=1){var a=e[o];if(this.options.id&&(a.item=this.options.getFn(a.item,this.options.id)[0]),n.length){for(var s={item:a.item},c=0,h=n.length;c<h;c+=1)n[c](a,s);t.push(s)}else t.push(a.item)}return t}},{key:"_log",value:function(){var e;this.options.verbose&&(e=console).log.apply(e,arguments)}}]),q}();e.exports=n},function(e,t,r){var l=r(3),u=r(4),k=r(7),n=function(){function m(e,t){var r=t.location,n=void 0===r?0:r,o=t.distance,i=void 0===o?100:o,a=t.threshold,s=void 0===a?.6:a,c=t.maxPatternLength,h=void 0===c?32:c,l=t.isCaseSensitive,u=void 0!==l&&l,f=t.tokenSeparator,p=void 0===f?/ +/g:f,d=t.findAllMatches,v=void 0!==d&&d,y=t.minMatchCharLength,g=void 0===y?1:y;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,m),this.options={location:n,distance:i,threshold:s,maxPatternLength:h,isCaseSensitive:u,tokenSeparator:p,findAllMatches:v,minMatchCharLength:g},this.pattern=this.options.isCaseSensitive?e:e.toLowerCase(),this.pattern.length<=h&&(this.patternAlphabet=k(this.pattern))}return function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(m.prototype,[{key:"search",value:function(e){if(this.options.isCaseSensitive||(e=e.toLowerCase()),this.pattern===e)return{isMatch:!0,score:0,matchedIndices:[[0,e.length-1]]};var t=this.options,r=t.maxPatternLength,n=t.tokenSeparator;if(this.pattern.length>r)return l(e,this.pattern,n);var o=this.options,i=o.location,a=o.distance,s=o.threshold,c=o.findAllMatches,h=o.minMatchCharLength;return u(e,this.pattern,this.patternAlphabet,{location:i,distance:a,threshold:s,findAllMatches:c,minMatchCharLength:h})}}]),m}();e.exports=n},function(e,t){var l=/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;e.exports=function(e,t){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:/ +/g,n=new RegExp(t.replace(l,"\\$&").replace(r,"|")),o=e.match(n),i=!!o,a=[];if(i)for(var s=0,c=o.length;s<c;s+=1){var h=o[s];a.push([e.indexOf(h),h.length-1])}return{score:i?.5:1,isMatch:i,matchedIndices:a}}},function(e,t,r){var Q=r(5),E=r(6);e.exports=function(e,t,r,n){for(var o=n.location,i=void 0===o?0:o,a=n.distance,s=void 0===a?100:a,c=n.threshold,h=void 0===c?.6:c,l=n.findAllMatches,u=void 0!==l&&l,f=n.minMatchCharLength,p=void 0===f?1:f,d=i,v=e.length,y=h,g=e.indexOf(t,d),m=t.length,k=[],x=0;x<v;x+=1)k[x]=0;if(-1!==g){var S=Q(t,{errors:0,currentLocation:g,expectedLocation:d,distance:s});if(y=Math.min(S,y),-1!==(g=e.lastIndexOf(t,d+m))){var b=Q(t,{errors:0,currentLocation:g,expectedLocation:d,distance:s});y=Math.min(b,y)}}g=-1;for(var M=[],_=1,O=m+v,w=1<<(m<=31?m-1:30),L=0;L<m;L+=1){for(var A=0,C=O;A<C;)Q(t,{errors:L,currentLocation:d+C,expectedLocation:d,distance:s})<=y?A=C:O=C,C=Math.floor((O-A)/2+A);O=C;var I=Math.max(1,d-C+1),P=u?v:Math.min(d+C,v)+m,j=Array(P+2);j[P+1]=(1<<L)-1;for(var T=P;I<=T;T-=1){var z=T-1,F=r[e.charAt(z)];if(F&&(k[z]=1),j[T]=(j[T+1]<<1|1)&F,0!==L&&(j[T]|=(M[T+1]|M[T])<<1|1|M[T+1]),j[T]&w&&(_=Q(t,{errors:L,currentLocation:z,expectedLocation:d,distance:s}))<=y){if(y=_,(g=z)<=d)break;I=Math.max(1,2*d-g)}}if(Q(t,{errors:L+1,currentLocation:d,expectedLocation:d,distance:s})>y)break;M=j}return{isMatch:0<=g,score:0===_?.001:_,matchedIndices:E(k,p)}}},function(e,t){e.exports=function(e,t){var r=t.errors,n=void 0===r?0:r,o=t.currentLocation,i=void 0===o?0:o,a=t.expectedLocation,s=void 0===a?0:a,c=t.distance,h=void 0===c?100:c,l=n/e.length,u=Math.abs(s-i);return h?l+u/h:u?1:l}},function(e,t){e.exports=function(){for(var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:[],t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:1,r=[],n=-1,o=-1,i=0,a=e.length;i<a;i+=1){var s=e[i];s&&-1===n?n=i:s||-1===n||((o=i-1)-n+1>=t&&r.push([n,o]),n=-1)}return e[i-1]&&t<=i-n&&r.push([n,i-1]),r}},function(e,t){e.exports=function(e){for(var t={},r=e.length,n=0;n<r;n+=1)t[e.charAt(n)]=0;for(var o=0;o<r;o+=1)t[e.charAt(o)]|=1<<r-o-1;return t}},function(e,t,r){var l=r(0);e.exports=function(e,t){return function e(t,r,n){if(r){var o=r.indexOf("."),i=r,a=null;-1!==o&&(i=r.slice(0,o),a=r.slice(o+1));var s=t[i];if(null!=s)if(a||"string"!=typeof s&&"number"!=typeof s)if(l(s))for(var c=0,h=s.length;c<h;c+=1)e(s[c],a,n);else a&&e(s,a,n);else n.push(s.toString())}else n.push(t);return n}(e,t,[])}}])}(e={exports:{}},e.exports),e.exports),i=(t=o)&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t;o.Fuse;var a=(s.prototype.plugin=function(){this.mixQuery(),this.registerQueryHook()},s.prototype.mixQuery=function(){!function(e,t){e.prototype.searchTerms=null,e.prototype.searchOptions=t,e.prototype.search=function(e,t){return void 0===t&&(t={}),this.searchTerms=Array.isArray(e)?e:[e],0===this.searchOptions.keys.length&&(this.searchOptions.keys=Object.keys(this.model.cachedFields[this.model.entity])),this.searchOptions=r(r({},this.searchOptions),t),this},e.prototype.filterSearch=function(e){if(null===this.searchTerms)return e;var r=new i(e,this.searchOptions);return this.searchTerms.reduce(function(e,t){return e.push.apply(e,r.search(t)),e},[])}}(this.query,this.options)},s.prototype.registerQueryHook=function(){this.query.on("afterWhere",function(e){return this.filterSearch(e)})},s);function s(e,t){this.query=e.Query,this.options=r(r({},n),t)}return{install:function(e,t){new a(e,t).plugin()}}});