@future-widget-lab/ui-filters
Version:
A set of primitives for handling filters through search parameters in React applications.
3 lines (2 loc) • 11.1 kB
JavaScript
"use strict";var t=require("flatted"),r=function(r){var e,n=r.filters,o=r.name,i=r.value,a=r.onBeforeAdd,u=r.onAfterAdd,l=null!=(e=n[o])?e:[];if("object"==typeof i){var c=l.map((function(r){return t.stringify(r)})),f=t.stringify(i),s=c.includes(f)?l:[].concat(l,[i]);return null==a||a({name:o,value:i}),n[r.name]=s,null==u||u({name:o,value:i}),n}var h=l.includes(i)?l:[].concat(l,[i]);return null==a||a({name:o,value:i}),n[r.name]=h,null==u||u({name:o,value:i}),n};function e(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function n(t,r,e,n,o,i,a){try{var u=t[i](a),l=u.value}catch(t){return void e(t)}u.done?r(l):Promise.resolve(l).then(n,o)}function o(t){return function(){var r=this,e=arguments;return new Promise((function(o,i){var a=t.apply(r,e);function u(t){n(a,o,i,u,l,"next",t)}function l(t){n(a,o,i,u,l,"throw",t)}u(void 0)}))}}function i(t,r){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,r){if(t){if("string"==typeof t)return e(t,r);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){n&&(t=n);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function a(){return a=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)({}).hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t},a.apply(null,arguments)}function u(){u=function(){return r};var t,r={},e=Object.prototype,n=e.hasOwnProperty,o=Object.defineProperty||function(t,r,e){t[r]=e.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",l=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag";function f(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{f({},"")}catch(t){f=function(t,r,e){return t[r]=e}}function s(t,r,e,n){var i=Object.create((r&&r.prototype instanceof g?r:g).prototype),a=new P(n||[]);return o(i,"_invoke",{value:F(t,e,a)}),i}function h(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=s;var v="suspendedStart",p="suspendedYield",y="executing",d="completed",m={};function g(){}function w(){}function b(){}var x={};f(x,a,(function(){return this}));var A=Object.getPrototypeOf,L=A&&A(A(G([])));L&&L!==e&&n.call(L,a)&&(x=L);var E=b.prototype=g.prototype=Object.create(x);function j(t){["next","throw","return"].forEach((function(r){f(t,r,(function(t){return this._invoke(r,t)}))}))}function O(t,r){function e(o,i,a,u){var l=h(t[o],t,i);if("throw"!==l.type){var c=l.arg,f=c.value;return f&&"object"==typeof f&&n.call(f,"__await")?r.resolve(f.__await).then((function(t){e("next",t,a,u)}),(function(t){e("throw",t,a,u)})):r.resolve(f).then((function(t){c.value=t,a(c)}),(function(t){return e("throw",t,a,u)}))}u(l.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new r((function(r,o){e(t,n,r,o)}))}return i=i?i.then(o,o):o()}})}function F(r,e,n){var o=v;return function(i,a){if(o===y)throw Error("Generator is already running");if(o===d){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var l=_(u,n);if(l){if(l===m)continue;return l}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===v)throw o=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=y;var c=h(r,e,n);if("normal"===c.type){if(o=n.done?d:p,c.arg===m)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=d,n.method="throw",n.arg=c.arg)}}}function _(r,e){var n=e.method,o=r.iterator[n];if(o===t)return e.delegate=null,"throw"===n&&r.iterator.return&&(e.method="return",e.arg=t,_(r,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var i=h(o,r.iterator,e.arg);if("throw"===i.type)return e.method="throw",e.arg=i.arg,e.delegate=null,m;var a=i.arg;return a?a.done?(e[r.resultName]=a.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,m):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,m)}function S(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function k(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(S,this),this.reset(!0)}function G(r){if(r||""===r){var e=r[a];if(e)return e.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var o=-1,i=function e(){for(;++o<r.length;)if(n.call(r,o))return e.value=r[o],e.done=!1,e;return e.value=t,e.done=!0,e};return i.next=i}}throw new TypeError(typeof r+" is not iterable")}return w.prototype=b,o(E,"constructor",{value:b,configurable:!0}),o(b,"constructor",{value:w,configurable:!0}),w.displayName=f(b,c,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===w||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,f(t,c,"GeneratorFunction")),t.prototype=Object.create(E),t},r.awrap=function(t){return{__await:t}},j(O.prototype),f(O.prototype,l,(function(){return this})),r.AsyncIterator=O,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new O(s(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},j(E),f(E,c,"Generator"),f(E,a,(function(){return this})),f(E,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=G,P.prototype={constructor:P,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(k),!r)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var e=this;function o(n,o){return u.type="throw",u.arg=r,e.next=n,o&&(e.method="next",e.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],u=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var l=n.call(a,"catchLoc"),c=n.call(a,"finallyLoc");if(l&&c){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(l){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!c)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,m):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),m},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),k(e),m}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;k(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:G(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),m}},r}var l=function(t){var r=t.filters;return delete r[t.name],r},c=function(r){var e,n=r.filters,o=r.value,i=null!=(e=n[r.name])?e:[];if(0===i.length)return n;if("object"==typeof o){var a=i.filter((function(r){return"object"==typeof r?t.stringify(r)!==t.stringify(o):r!==t.stringify(o)}));return 0===a.length?l({filters:n,name:r.name}):(n[r.name]=a,n)}var u=i.filter((function(r){return"object"==typeof r?t.stringify(r)!==o:r!==o}));return 0===u.length?l({filters:n,name:r.name}):(n[r.name]=u,n)},f=function(t){var e=t.filters,n=t.name,o=t.value;if(!o||Array.isArray(o)&&0===o.length)return l({filters:e,name:n});if(e=l({filters:e,name:n}),Array.isArray(o))for(var a,u=i(o);!(a=u()).done;)e=r({filters:e,name:n,value:a.value});else e=r({filters:e,name:n,value:o});return e},s=function(){var r=o(u().mark((function r(e){var n,o,i,a,l,c,f;return u().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:n=e.filters,o=e.commit,a=void 0===(i=e.serializer)?t.stringify:i,c=e.onAfterCommit,null==(l=e.onBeforeCommit)||l(),f=0===Object.keys(n).length?void 0:a(n),o(f),null==c||c();case 5:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}();exports.addFilterValue=r,exports.addMultipleFilterValues=function(t){for(var e,n=t.filters,o=t.input,u=t.onBeforeAdd,l=t.onAfterAdd,c=a({},n),f=i(Array.isArray(o)?o:[o]);!(e=f()).done;){var s=e.value;if(Array.isArray(s.value))for(var h,v=i(s.value);!(h=v()).done;)c=r({filters:c,name:s.name,value:h.value,onBeforeAdd:u,onAfterAdd:l});else c=r({filters:n,name:s.name,value:s.value,onBeforeAdd:u,onAfterAdd:l})}return c},exports.deserializeFilters=function(r){var e=r.serializedFilters,n=r.deserializer,o=void 0===n?t.parse:n,i=r.onBeforeDeserializer,a=r.onAfterDeserializer,u=r.onDeserializerError;null==i||i();try{if(!e)return{};var l=o(e);return null==a||a(l),l}catch(t){return null==u||u(t),{}}},exports.peekFilter=function(t){var r;return null!=(r=t.filters[t.name])?r:[]},exports.removeFilter=l,exports.removeFilterValue=c,exports.removeMultipleFilterValues=function(t){for(var r,e=t.filters,n=t.input,o=a({},e),u=i(Array.isArray(n)?n:[n]);!(r=u()).done;){var l=r.value;if(Array.isArray(l.value))for(var f,s=i(l.value);!(f=s()).done;)o=c({filters:o,name:l.name,value:f.value});else o=c({filters:e,name:l.name,value:l.value})}return o},exports.removeMultipleFilters=function(t){for(var r,e=t.filters,n=t.input,o=a({},e),u=i(Array.isArray(n)?n:[n]);!(r=u()).done;)o=l({filters:e,name:r.value});return o},exports.setFilterValue=f,exports.setMultipleFilterValues=function(t){for(var r,e=t.filters,n=t.input,o=a({},e),u=i(Array.isArray(n)?n:[n]);!(r=u()).done;){var l=r.value;o=f({filters:e,name:l.name,value:l.value})}return o},exports.syncState=s;
//# sourceMappingURL=ui-filters.cjs.production.min.js.map