array-of-object-query
Version:
array-of-object-query is a simple JavaScript library that makes working with arrays of objects easier. It has some methods to help you filter, sort, group, and summarize data. Use this library to make handling complex data structures simple and efficient.
1 lines • 6.38 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["array-of-object-query"]=e():t["array-of-object-query"]=e()}(this,(()=>(()=>{"use strict";var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}function u(t){return u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u(t)}function y(t){return y="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},y(t)}function f(t){return f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},f(t)}function c(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}t.r(e),t.d(e,{arrayQueries:()=>a});var s={where:function(t,e){if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(n(t)));if(void 0===e)throw new TypeError("The value must be defined");return this.filter((function(r){return r[t]===e}))},whereIn:function(t,e){if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(o(t)));if(!Array.isArray(e))throw new TypeError("The values must be an array");return this.filter((function(r){return e.includes(r[t])}))},select:function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return this.map((function(t){var r={};return e.forEach((function(e){e in t&&(r[e]=t[e])})),r}))},pluck:function(){for(var t=this,e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return r.map((function(e){return t.map((function(t){return t[e]}))}))},orderBy:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"asc";if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(r(t)));if(!["asc","desc"].includes(e))throw new TypeError('The order must be "asc" or "desc"');return this.slice().sort((function(r,n){return r[t]<n[t]?"asc"===e?-1:1:r[t]>n[t]?"asc"===e?1:-1:0}))},first:function(){return this.length>0?this[0]:void 0},last:function(){return this.length>0?this[this.length-1]:void 0},min:function(t){if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(i(t)));if(0!==this.length){for(var e=this[0][t],r=1;r<this.length;r++)this[r][t]<e&&(e=this[r][t]);return e}},max:function(t){if("string"!=typeof t)throw new TypeError("The key must be a string");if(0!==this.length){for(var e=this[0][t],r=1;r<this.length;r++)this[r][t]>e&&(e=this[r][t]);return e}},take:function(t){if("number"!=typeof t||t<0)throw new TypeError("Parameter n must be a non-negative number");return this.slice(0,t)},sum:function(t){if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(u(t)));return 0===this.length?0:this.reduce((function(e,r){if(!(t in r)||"number"!=typeof r[t])throw new TypeError("Object does not have numeric property '".concat(t,"'"));return e+Number(r[t])}),0)},groupBy:function(t){if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(f(t)));return this.reduce((function(e,r){var n=r[t];return e[n]||(e[n]=[]),e[n].push(r),e}),{})},average:function(t){if("string"!=typeof t)throw new TypeError("The key must be a string. The given key is the ".concat(y(t)));return 0===this.length?NaN:this.reduce((function(e,r){if(!(t in r)||"number"!=typeof r[t])throw new TypeError("Object does not have numeric property '".concat(t,"'"));return e+r[t]}),0)/this.length},unique:function(t){var e=new Set;return this.filter((function(r){var n=r[t];return!e.has(n)&&(e.add(n),!0)}))},distinct:function(t){var e=new Set;return this.reduce((function(r,n){var o=n[t];return e.has(o)||(e.add(o),r.push(o)),r}),[])}};Object.entries(s).forEach((function(t){var e,r,n=(r=2,function(t){if(Array.isArray(t))return t}(e=t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,u,y=[],f=!0,c=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;f=!1}else for(;!(f=(n=i.call(r)).done)&&(y.push(n.value),y.length!==e);f=!0);}catch(t){c=!0,o=t}finally{try{if(!f&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(c)throw o}}return y}}(e,r)||function(t,e){if(t){if("string"==typeof t)return c(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?c(t,e):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),o=n[0],i=n[1];Array.prototype[o]||(Array.prototype[o]=i)}));const a=s;return e})()));