UNPKG

9.13 kBJavaScriptView Raw
1(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):(a="undefined"==typeof globalThis?a||self:globalThis,b(a.quickScore={}))})(this,function(a){"use strict";var b=Math.min;function c(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function d(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function e(a,b,c){return b&&d(a.prototype,b),c&&d(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function f(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),Object.defineProperty(a,"prototype",{writable:!1}),b&&h(a,b)}function g(a){return g=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(a){return a.__proto__||Object.getPrototypeOf(a)},g(a)}function h(a,b){return h=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(a,b){return a.__proto__=b,a},h(a,b)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(a){return!1}}function j(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function k(a,b){if(b&&("object"==typeof b||"function"==typeof b))return b;if(void 0!==b)throw new TypeError("Derived constructors may only return object or undefined");return j(a)}function l(a){var b=i();return function(){var c,d=g(a);if(b){var e=g(this).constructor;c=Reflect.construct(d,arguments,e)}else c=d.apply(this,arguments);return k(this,c)}}function m(a,b){return n(a)||o(a,b)||p(a,b)||r()}function n(a){if(Array.isArray(a))return a}function o(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f=[],g=!0,h=!1;try{for(c=c.call(a);!(g=(d=c.next()).done)&&(f.push(d.value),!(b&&f.length===b));g=!0);}catch(a){h=!0,e=a}finally{try{g||null==c["return"]||c["return"]()}finally{if(h)throw e}}return f}}function p(a,b){if(a){if("string"==typeof a)return q(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?q(a,b):void 0}}function q(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function r(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function s(a){return a instanceof z?a:new A(a)}function t(a,c,d){function e(l,m,n){if(!m.length)return h.ignoredScore;if(m.length>l.length)return 0;for(var o=d&&d.length,p=m.length;0<p;p--){if(k>h.maxIterations)return 0;k++;var q=g.substring(m.location,m.location+p),r=u(f,q,new w(l.location,l.length-m.length+p));if(r.isValid()){n.location=n.isValid()?b(n.location,r.location):r.location,n.max(r.max()),d&&d.push(r.toArray());var s=new w(r.max(),l.max()-r.max()),t=new w(m.location+p,m.length-p),v=e(s,t,n);if(v){var x=s.location-l.location,y=!0,z=h.useSkipReduction(a,c,v,s,l,s,r,n);if(r.location>l.location)if(z&&-1<h.wordSeparators.indexOf(a[r.location-1]))for(var A=r.location-2;A>=l.location;A--)-1<h.wordSeparators.indexOf(a[A])?x--:x-=h.skippedScore;else if(z&&-1<h.uppercaseLetters.indexOf(a[r.location]))for(var B=r.location-1;B>=l.location;B--)-1<h.uppercaseLetters.indexOf(a[B])?x--:x-=h.skippedScore;else x-=r.location-l.location,y=!1;return x+=h.adjustRemainingScore(a,c,v,y,l,s,r,n),x/=l.length,x}d&&(d.length=o)}}return 0}var f=3<arguments.length&&arguments[3]!==void 0?arguments[3]:a.toLocaleLowerCase(),g=4<arguments.length&&arguments[4]!==void 0?arguments[4]:c.toLocaleLowerCase(),h=5<arguments.length&&arguments[5]!==void 0?arguments[5]:B,i=6<arguments.length&&arguments[6]!==void 0?arguments[6]:new w(0,a.length),k=0;return c?e(i,new w(0,c.length),new w):h.emptyQueryScore}function u(a,b,c){var d=a.indexOf(b,c.location),e=new w;return-1<d&&d<c.max()&&(e.location=d,e.length=b.length),e}function v(a){return a.toLocaleLowerCase()}var w=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:-1,d=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0;c(this,a),this.location=b,this.length=d}return e(a,[{key:"max",value:function(a){return"number"==typeof a&&(this.length=a-this.location),this.location+this.length}},{key:"isValid",value:function(){return-1<this.location}},{key:"toArray",value:function(){return[this.location,this.max()]}},{key:"toString",value:function(){return-1==this.location?"invalid range":"["+this.location+","+this.max()+")"}}]),a}(),x={wordSeparators:"-/\\:()<>%._=&[]+ \t\n\r",uppercaseLetters:function(){for(var a=[],b=0;26>b;b++)a.push(String.fromCharCode(65+b));return a.join("")}(),ignoredScore:.9,skippedScore:.15,emptyQueryScore:0,maxIterations:Math.pow(2,16)},y={longStringLength:150,maxMatchStartPct:.15,minMatchDensityPct:.75,maxMatchDensityPct:.95,beginningOfStringPct:.1},z=function(){function a(b){c(this,a),Object.assign(this,x,b)}return e(a,[{key:"useSkipReduction",value:function(){return!0}},{key:"adjustRemainingScore",value:function(a,b,c,d,e,f){return c*f.length}}]),a}(),A=function(a){function d(a){return c(this,d),g.call(this,Object.assign({},y,a))}f(d,a);var g=l(d);return e(d,[{key:"useSkipReduction",value:function(a,b,c,d,e,f,g){var h=a.length,i=h<=this.longStringLength,j=g.location/h;return i||j<this.maxMatchStartPct}},{key:"adjustRemainingScore",value:function(a,c,d,e,f,g,h,i){var j=a.length<=this.longStringLength,k=i.location/a.length,l=1,m=1-k;return e||(l=c.length/i.length,l=j&&k<=this.beginningOfStringPct&&l>=this.minMatchDensityPct?1:l,m=l>=this.maxMatchDensityPct?1:m),d*b(g.length,this.longStringLength)*l*m}}]),d}(z),B=s(),C=new z,D=new z({emptyQueryScore:.9,adjustRemainingScore:function(a,b,c,d,e,f,g){var h=c*f.length;return d||(h+=(g.location-e.location)/2),h}});t.createConfig=s;var E=function(){function a(){var b=0<arguments.length&&void 0!==arguments[0]?arguments[0]:[],d=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};c(this,a);var e=Array.isArray(d)?{keys:d}:d,f=e.scorer,g=void 0===f?t:f,h=e.transformString,i=void 0===h?v:h,j=e.keys,k=void 0===j?[]:j,l=e.sortKey,m=void 0===l?"":l,n=e.minimumScore,o=void 0===n?0:n,p=e.config;this.scorer=g,this.minimumScore=o,this.config=p,this.transformStringFunc=i,"function"==typeof g.createConfig&&(this.config=g.createConfig(p)),this.setKeys(k,m),this.setItems(b),this.compareScoredStrings=this.compareScoredStrings.bind(this)}return e(a,[{key:"search",value:function(a){var b=[],c=this.items,d=this.transformedItems,e=this.keys,f=this.config,g=a?this.minimumScore:-1,h=this.transformString(a),k=c.length,l=e.length;if("string"==typeof c[0])for(var m=0;m<k;m++){var n=c[m],o=d[m],p=[],q=this.scorer(n,a,p,o,h,f);q>g&&b.push({item:n,score:q,matches:p,_:o})}else for(var r=0;r<k;r++){for(var s=c[r],t=d[r],u={item:s,score:0,scoreKey:"",scoreValue:"",scores:{},matches:{},_:t},v=l?e:Object.keys(t),w=v.length,x=0,y="",z="",A=0;A<w;A++){var B=v[A],C=B.name,D=void 0===C?B:C,E=B.scorer,F=void 0===E?this.scorer:E,G=t[D];if(G){var H=this.getItemString(s,B),I=[],J=F(H,a,I,G,h,f);u.scores[D]=J,u.matches[D]=I,J>x&&(x=J,y=D,z=H)}}x>g&&(u.score=x,u.scoreKey=y,u.scoreValue=z,b.push(u))}return b.sort(this.compareScoredStrings),b}},{key:"setKeys",value:function(a,b){if(this.keys=a.slice(),this.sortKey=b,this.keys.length){var c=this.scorer;this.keys=this.keys.map(function(a){var b=a.length?{name:a,scorer:c}:a;if(!Array.isArray(b.name))-1<b.name.indexOf(".")&&(b.path=b.name.split("."));else if(1<b.name.length)b.path=b.name,b.name=b.path.join(".");else{var d=m(b.name,1);b.name=d[0]}return b}),this.sortKey=this.sortKey||this.keys[0].name}}},{key:"setItems",value:function(a){var b=a.slice(),c=b.length,d=[],e=this.keys,f=e.length;if("string"==typeof b[0])for(var g=0;g<c;g++)d.push(this.transformString(b[g]));else for(var h=0;h<c;h++){for(var k=b[h],l={},m=f?e:Object.keys(k),n=m.length,o=0;o<n;o++){var p=m[o],q=this.getItemString(k,p);q&&"string"==typeof q&&(l[p.name||p]=this.transformString(q))}d.push(l)}this.items=b,this.transformedItems=d}},{key:"getItemString",value:function(a,b){var c=b.name,d=b.path;return d?d.reduce(function(a,b){return a&&a[b]},a):a[c||b]}},{key:"transformString",value:function(a){return this.transformStringFunc(a)}},{key:"compareScoredStrings",value:function(c,a){var b=c._,d=a._,e="string"==typeof b?b:b[this.sortKey],f="string"==typeof d?d:d[this.sortKey];return c.score===a.score?void 0===e||void 0===f?void 0===e&&void 0===f?0:void 0===e?1:-1:e===f?0:e<f?-1:1:a.score-c.score}}]),a}();a.BaseConfig=C,a.DefaultConfig=B,a.QuickScore=E,a.QuicksilverConfig=D,a.Range=w,a.createConfig=s,a.quickScore=t,Object.defineProperty(a,"__esModule",{value:!0})});