UNPKG

recent-searches

Version:

A JavaScript module to help anyone easily build recent searches functionality into their search.

1 lines 4.74 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("RecentSearches",[],e):"object"==typeof exports?exports.RecentSearches=e():t.RecentSearches=e()}("undefined"!=typeof self?self:this,function(){return r={},i.m=n=[function(t,e,n){"use strict";function o(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}n.r(e);var r=function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),o(this,"DATA",{}),o(this,"KEY",void 0),o(this,"DEFAULT_VALUE",void 0),o(this,"getItem",function(){return n.DATA[n.KEY]||n.DEFAULT_VALUE}),o(this,"setItem",function(t){return n.DATA[n.KEY]=t,!0});var r=e.key,i=e.defaultValue;this.DATA={},this.KEY=r||a,this.DEFAULT_VALUE=i};function i(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function u(t){var e=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,u),i(this,"KEY",void 0),i(this,"DEFAULT_VALUE",void 0),i(this,"getItem",function(){return function(t,e){if(!t)return e;try{return JSON.parse(t)}catch(t){return e}}(localStorage.getItem(e.KEY),e.DEFAULT_VALUE)}),i(this,"setItem",function(t){try{return localStorage.setItem(e.KEY,JSON.stringify(t)),!0}catch(t){return!1}});var n=t.key,r=t.defaultValue;this.KEY=n||a,this.DEFAULT_VALUE=r}var a="__RECENT_SEARCHES__",s=function(t){return function(){var t="__TEST__KEY__";try{return localStorage.setItem(t,""),localStorage.removeItem(t),!0}catch(t){return!1}}()?new u(t):new r(t)},c=function(t,e,n,r){var i=String(e);switch(n){case"PROXIMITY":return t.query.indexOf(i);case"TIME":return Math.log10((new Date).getTime()-t.timestamp);default:var o=t.query.indexOf(i),u=Math.log2(((new Date).getTime()-t.timestamp)/r+1),a=Math.log2(o+1)||1;return-1===o?o:(.01+.49*a+.49*u)/1}},f=function(t){return"string"==typeof t&&!function(t){return/^\s*$/.test(0<arguments.length&&void 0!==t?t:"")}(t)||"number"==typeof t};function E(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}n.d(e,"RecentSearches",function(){return l});var l=function t(){var i=this,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),E(this,"TTL",void 0),E(this,"LIMIT",void 0),E(this,"STORAGE",void 0),E(this,"RANKING",void 0),E(this,"RECENT_SEARCHES",[]),E(this,"getRecentSearches",function(n){return f(n)?i.RECENT_SEARCHES.map(function(t){var e=c(t,n,i.RANKING,i.TTL);return{data:t.data,query:t.query,score:e,timestamp:t.timestamp}}).filter(i.filterScoredResults).sort(i.sortScoredResults).map(function(t){return{data:t.data,query:t.query,timestamp:t.timestamp}}):i.RECENT_SEARCHES}),E(this,"setRecentSearch",function(e,t){if(!f(e))return i.RECENT_SEARCHES;var n={data:t,query:String(e),timestamp:(new Date).getTime()},r=i.RECENT_SEARCHES.findIndex(function(t){return t.query===e});return-1<r&&i.RECENT_SEARCHES.splice(r,1),i.RECENT_SEARCHES.unshift(n),i.RECENT_SEARCHES=i.RECENT_SEARCHES.slice(0,i.LIMIT),i.STORAGE.setItem(i.RECENT_SEARCHES),i.RECENT_SEARCHES}),E(this,"filterScoredResults",function(t){return"TIME"===i.RANKING||-1<t.score}),E(this,"sortScoredResults",function(t,e){return t.score-e.score}),E(this,"initializeStorageData",function(){var e=(new Date).getTime(),t=i.STORAGE.getItem().filter(function(t){return t.timestamp+i.TTL>=e}).slice(0,i.LIMIT);return i.STORAGE.setItem(t),i.RECENT_SEARCHES=t}),this.TTL=e.ttl||864e5,this.LIMIT=e.limit||50,this.STORAGE=s({defaultValue:[],key:e.namespace}),this.RECENT_SEARCHES=this.initializeStorageData(),this.RANKING=e.ranking||"PROXIMITY_AND_TIME"};e.default=l}],i.c=r,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)i.d(n,r,function(t){return e[t]}.bind(null,r));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0);function i(t){if(r[t])return r[t].exports;var e=r[t]={i:t,l:!1,exports:{}};return n[t].call(e.exports,e,e.exports,i),e.l=!0,e.exports}var n,r});