querier-ts 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,50 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.5.0
4
+
5
+ ### Summary
6
+
7
+ - [ ] Bug fixes
8
+ - [x] Code refactoring
9
+ - [x] New features
10
+ - [x] Build and packaging updates
11
+ - [ ] Breaking changes
12
+
13
+ ### New features
14
+
15
+ - Added `distinct()` method to `Query`.
16
+
17
+ ### Code refactoring
18
+
19
+ - Improve conditions order in `column()` method.
20
+ - Added a new unit test for the `column()` method, covering no rows case.
21
+
22
+ ### Build and packaging updates
23
+
24
+ - Updated development dependencies.
25
+
26
+ ## v2.4.1
27
+
28
+ ### Summary
29
+
30
+ - [ ] Bug fixes
31
+ - [x] Code refactoring
32
+ - [ ] New features
33
+ - [x] Compatibility improvements
34
+ - [ ] Breaking changes
35
+
36
+ ### Code refactoring
37
+
38
+ - Removed redundant conditions and unecessary code.
39
+ - Optimized array iteration performance:
40
+ - Pre-allocate arrays to avoid dynamic resizing.
41
+ - Replace `Array.map()` and `for...of` with indexed `for` loops.
42
+ - Updated JSDoc for `Query`.
43
+
44
+ ### Compatibility improvements
45
+
46
+ - Updated development dependencies.
47
+
3
48
  ## v2.4.0
4
49
 
5
50
  ### Changes
package/README.md CHANGED
@@ -10,7 +10,7 @@ A lightweight, type-safe in-memory query engine for JavaScript and TypeScript.
10
10
 
11
11
  ## `Query`
12
12
 
13
- You can create a `Query` instance like this:
13
+ Usage:
14
14
 
15
15
  ```ts
16
16
  const usersQuery = Query.from(users);
@@ -79,6 +79,23 @@ const filteredUsers = Query.from(users)
79
79
 
80
80
  ---
81
81
 
82
+ #### `distinct(key | callback)`
83
+
84
+ Removes duplicate rows based on a key or a callback function.
85
+
86
+ - `key`: The key to be used for comparison.
87
+ - `callback`: A function that maps each row to a value.
88
+
89
+ ```ts
90
+ const uniqueCountries = Query.from(addresses)
91
+ .distinct('country')
92
+ .column('country');
93
+
94
+ const uniqueEmailProviders = Query.from(users)
95
+ .distinct((user) => user.email.split('@')[1])
96
+ .column('email');
97
+ ```
98
+
82
99
  ### Selecting data
83
100
 
84
101
  #### `select(columns)`
@@ -1 +1 @@
1
- 'use strict';var S=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var F=n=>{throw TypeError(n)};var x=(n,e,t,r)=>{for(var o=$(e,t),i=n.length-1,s;i>=0;i--)(s=n[i])&&(o=(s(e,t,o))||o);return o&&S(e,t,o),o},h=(n,e)=>(t,r)=>e(t,r,n);var N=(n,e,t)=>e.has(n)||F("Cannot "+t);var l=(n,e,t)=>(N(n,e,"read from private field"),t?t.call(n):e.get(n)),y=(n,e,t)=>e.has(n)?F("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(n):e.set(n,t),p=(n,e,t,r)=>(N(n,e,"write to private field"),e.set(n,t),t);var O=n=>typeof n=="number";var b=class extends Error{constructor(e){super(e),this.name="InvalidArgumentError";}};var v=new WeakMap;function V(n,e){let t=v.get(n);t||(t=new Map,v.set(n,t));let r=t.get(e);return r||(r=[],t.set(e,r)),r}function k(n){return function(e,t,r){let o=V(e,t),i=o.find(s=>s.index===r);i?i.min=n:o.push({index:r,min:n});}}function P(n,e,t){let r=V(n,e),o=r.find(i=>i.index===t);o?o.integer=true:r.push({index:t,integer:true});}function C(n,e,t){let r=t.value;t.value=function(...o){let i=v.get(n)?.get(e)??[];for(let s of i){let u=o[s.index];if(s.min!==void 0&&(!O(u)||u<s.min))throw new b(`${String(u)} is not a valid argument to param ${s.index} on ${e}(). Expected value to be equal or greater than ${s.min}.`);if(s.max!==void 0&&(!O(u)||u>s.max))throw new b(`${String(u)} is not a valid argument to param ${s.index} on ${e}(). Expected value to be equal or less than ${s.max}.`);if(s.integer&&!Number.isSafeInteger(u))throw new b(`${String(u)} is not a valid argument to param ${s.index} on ${e}(). Expected value to be an integer.`)}return r.apply(this,o)};}var d=n=>typeof n=="object"&&n!==null;var j=(n,e)=>{if(n===e)return true;if(!d(n)||!d(e)||Array.isArray(n)!==Array.isArray(e))return false;let t=Object.keys(n),r=Object.keys(e);return t.length!==r.length?false:t.every(o=>Object.prototype.hasOwnProperty.call(e,o)&&j(n[o],e[o]))};var A=(n,e)=>n.length===e.length&&n.every((t,r)=>j(t,e[r]));function G(n){return Object.entries(n)}var m=n=>typeof n=="function";var w,g=class{static validate(e,t,r){for(let[o,i]of G(t))if(!this.validateColumnCondition(e,o,i,r??l(this,w)))return false;return true}static validateColumnCondition(e,t,r,o){if(o.ignoreNullValues&&r==null)return true;let i=e[t];return m(r)?r(i):Array.isArray(r)?Array.isArray(i)?A(i,r):false:d(r)?d(i)?this.validate(i,r):false:i===r}};w=new WeakMap,y(g,w,{ignoreNullValues:false});function B(n){return Object.keys(n).filter(e=>typeof n[e]!="function")}function L(n){let e=1,t=n;t.startsWith("-")&&(e=-1,t=t.slice(1));let r=t;return (o,i)=>{let s=o[r],u=i[r];return s==null&&u==null?0:s==null?1*e:u==null||s<u?-1*e:s>u?1*e:0}}function M(...n){return (e,t)=>{if(n.length===0)return 0;for(let r of n){let o=L(r)(e,t);if(o!==0)return o}return 0}}var a,f,T,c=class c{constructor(e){y(this,a,[]);y(this,f,0);y(this,T,null);p(this,a,[...e]);}static from(e){return new c(e)}select(...e){let t=[];for(let o of l(this,a)){let i={};for(let s of e)i[s]=o[s];t.push(i);}let r=new c(t);return this.cloneStateInto(r),r}map(e){let t=[];for(let o of l(this,a))t.push(e(o));let r=new c(t);return this.cloneStateInto(r),r}where(e){return this.filterRows(e),this}filterWhere(e){return this.filterRows(e,{ignoreNullValues:true}),this}orderBy(...e){return p(this,a,l(this,a).sort(M(...e))),this}skip(e){return p(this,f,e),this}limit(e){return p(this,T,e),this}all(){return this.getLimitedRows()}first(){let e=this.getLimitedRows();return e.length>0?e[0]:null}last(){let e=this.getLimitedRows();return e[e.length-1]??null}count(){return this.getLimitedRows().length}exists(){return this.count()>0}scalar(){let e=this.first(),t=this.getFirstColumn();return e&&t?e[t]:false}column(e){if(e===void 0){let t=this.getFirstColumn();if(!t)return [];e=t;}return this.getLimitedRows().map(t=>t[e])}values(){let e=[];for(let t of this.getLimitedRows())e.push(Object.values(t));return e}groupBy(e,t){let r=this.getLimitedRows(),o=new Map,i=m(e);for(let s of r){let u=i?e(s):s[e],K=t?t(s):s;o.has(u)?o.get(u).push(K):o.set(u,[K]);}return o}min(e){let t=this.getLimitedRows();if(t.length===0)return null;let r=m(e)?t.map(o=>e(o)):t.map(o=>o[e]);return r.length===0?null:Math.min(...r)}max(e){let t=this.getLimitedRows();if(t.length===0)return null;let r=m(e)?t.map(o=>e(o)):t.map(o=>o[e]);return r.length===0?null:Math.max(...r)}sum(e){let t=this.getLimitedRows();return t.length===0?0:(m(e)?t.map(o=>e(o)):t.map(o=>o[e])).reduce((o,i)=>o+i,0)}average(e){let t=this.count();return t===0?null:(m(e)?this.sum(e):this.sum(e))/t}filterRows(e,t){let r=l(this,a),o=[];if(m(e))for(let i=0;i<r.length;i++){let s=r[i];e(s)&&o.push(s);}else for(let i=0;i<r.length;i++){let s=r[i];g.validate(s,e,t)&&o.push(s);}p(this,a,o);}getFirstColumn(){let e=this.first();if(!e)return null;let t=B(e);return t.length>0?t[0]:null}getLimitedRows(){let e=[...l(this,a)];return e.slice(l(this,f),l(this,f)+(l(this,T)??e.length))}cloneStateInto(e){p(e,f,l(this,f)),p(e,T,l(this,T));}};a=new WeakMap,f=new WeakMap,T=new WeakMap,x([C,h(0,P),h(0,k(0))],c.prototype,"skip"),x([C,h(0,P),h(0,k(0))],c.prototype,"limit");var R=c;exports.Query=R;
1
+ 'use strict';var $=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var V=s=>{throw TypeError(s)};var O=(s,e,t,n)=>{for(var o=E(e,t),r=s.length-1,i;r>=0;r--)(i=s[r])&&(o=(i(e,t,o))||o);return o&&$(e,t,o),o},g=(s,e)=>(t,n)=>e(t,n,s);var F=(s,e,t)=>e.has(s)||V("Cannot "+t);var l=(s,e,t)=>(F(s,e,"read from private field"),e.get(s)),d=(s,e,t)=>e.has(s)?V("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(s):e.set(s,t),f=(s,e,t,n)=>(F(s,e,"write to private field"),e.set(s,t),t);var v=s=>typeof s=="number";var b=class extends Error{constructor(e){super(e),this.name="InvalidArgumentError";}};var k=new WeakMap;function N(s,e){let t=k.get(s);t||(t=new Map,k.set(s,t));let n=t.get(e);return n||(n=[],t.set(e,n)),n}function P(s){return function(e,t,n){let o=N(e,t),r=o.find(i=>i.index===n);r?r.min=s:o.push({index:n,min:s});}}function C(s,e,t){let n=N(s,e),o=n.find(r=>r.index===t);o?o.integer=true:n.push({index:t,integer:true});}function j(s,e,t){let n=t.value;t.value=function(...o){let r=k.get(s)?.get(e)??[];for(let i of r){let u=o[i.index];if(i.min!==void 0&&(!v(u)||u<i.min))throw new b(`${String(u)} is not a valid argument to param ${i.index} on ${e}(). Expected value to be equal or greater than ${i.min}.`);if(i.max!==void 0&&(!v(u)||u>i.max))throw new b(`${String(u)} is not a valid argument to param ${i.index} on ${e}(). Expected value to be equal or less than ${i.max}.`);if(i.integer&&!Number.isSafeInteger(u))throw new b(`${String(u)} is not a valid argument to param ${i.index} on ${e}(). Expected value to be an integer.`)}return n.apply(this,o)};}var h=s=>typeof s=="object"&&s!==null;var R=(s,e)=>{if(s===e)return true;if(!h(s)||!h(e)||Array.isArray(s)!==Array.isArray(e))return false;let t=Object.keys(s),n=Object.keys(e);return t.length!==n.length?false:t.every(o=>Object.prototype.hasOwnProperty.call(e,o)&&R(s[o],e[o]))};var G=(s,e)=>s.length===e.length&&s.every((t,n)=>R(t,e[n]));function B(s){return Object.entries(s)}var m=s=>typeof s=="function";var x,w=class{static validate(e,t,n){for(let[o,r]of B(t))if(!this.validateColumnCondition(e,o,r,n??l(this,x)))return false;return true}static validateColumnCondition(e,t,n,o){if(o.ignoreNullValues&&n==null)return true;let r=e[t];return m(n)?n(r):Array.isArray(n)?Array.isArray(r)?G(r,n):false:h(n)?h(r)?this.validate(r,n):false:r===n}};x=new WeakMap,d(w,x,{ignoreNullValues:false});function S(s){return Object.keys(s).filter(e=>typeof s[e]!="function")}function L(s){let e=1,t=s;t.startsWith("-")&&(e=-1,t=t.slice(1));let n=t;return (o,r)=>{let i=o[n],u=r[n];return i==null&&u==null?0:i==null?1*e:u==null||i<u?-1*e:i>u?1*e:0}}function M(...s){return (e,t)=>{if(s.length===0)return 0;for(let n of s){let o=L(n)(e,t);if(o!==0)return o}return 0}}var a,c,y,T=class T{constructor(e){d(this,a,[]);d(this,c,0);d(this,y,null);f(this,a,[...e]);}static from(e){return new T(e)}select(...e){let t=l(this,a),n=t.length,o=new Array(n);for(let i=0;i<n;i++){let u={};for(let p of e)u[p]=t[i][p];o[i]=u;}let r=new T(o);return this.cloneStateInto(r),r}map(e){let t=l(this,a),n=t.length,o=new Array(n);for(let i=0;i<n;i++)o[i]=e(t[i]);let r=new T(o);return this.cloneStateInto(r),r}distinct(e){let t=new Set,n=[],o=l(this,a),r=m(e);for(let i=0;i<o.length;i++){let u=o[i],p=r?e(u):u[e];t.has(p)||(t.add(p),n.push(u));}return f(this,a,n),this}where(e){return this.filterRows(e),this}filterWhere(e){return this.filterRows(e,{ignoreNullValues:true}),this}orderBy(...e){return f(this,a,l(this,a).sort(M(...e))),this}skip(e){return f(this,c,e),this}limit(e){return f(this,y,e),this}all(){return this.getLimitedRows()}first(){let e=this.getLimitedRows();return e.length>0?e[0]:null}last(){let e=this.getLimitedRows();return e[e.length-1]??null}count(){return this.getLimitedRows().length}exists(){return this.count()>0}scalar(){let e=this.first(),t=this.getFirstColumn();return e&&t?e[t]:false}column(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return [];if(e===void 0){let r=this.getFirstColumn();if(!r)return [];e=r;}let o=new Array(n);for(let r=0;r<n;r++)o[r]=t[r][e];return o}values(){let e=this.getLimitedRows(),t=e.length,n=new Array(t);for(let o=0;o<t;o++)n[o]=Object.values(e[o]);return n}groupBy(e,t){let n=this.getLimitedRows(),o=new Map,r=m(e);for(let i=0;i<n.length;i++){let u=n[i],p=r?e(u):u[e],A=t?t(u):u;o.has(p)?o.get(p).push(A):o.set(p,[A]);}return o}min(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return null;let o=new Array(n);if(m(e))for(let r=0;r<n;r++)o[r]=e(t[r]);else for(let r=0;r<n;r++)o[r]=t[r][e];return Math.min(...o)}max(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return null;let o=new Array(n);if(m(e))for(let r=0;r<n;r++)o[r]=e(t[r]);else for(let r=0;r<n;r++)o[r]=t[r][e];return Math.max(...o)}sum(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return 0;let o=new Array(n);if(m(e))for(let r=0;r<n;r++)o[r]=e(t[r]);else for(let r=0;r<n;r++)o[r]=t[r][e];return o.reduce((r,i)=>r+i,0)}average(e){let t=this.count();return t===0?null:(m(e)?this.sum(e):this.sum(e))/t}filterRows(e,t){let n=l(this,a),o=[];if(m(e))for(let r=0;r<n.length;r++){let i=n[r];e(i)&&o.push(i);}else for(let r=0;r<n.length;r++){let i=n[r];w.validate(i,e,t)&&o.push(i);}f(this,a,o);}getFirstColumn(){let e=this.first();if(!e)return null;let t=S(e);return t.length>0?t[0]:null}getLimitedRows(){return l(this,a).slice(l(this,c),l(this,c)+(l(this,y)??l(this,a).length))}cloneStateInto(e){f(e,c,l(this,c)),f(e,y,l(this,y));}};a=new WeakMap,c=new WeakMap,y=new WeakMap,O([j,g(0,C),g(0,P(0))],T.prototype,"skip"),O([j,g(0,C),g(0,P(0))],T.prototype,"limit");var K=T;exports.Query=K;
@@ -34,50 +34,49 @@ type KeysOfType<T, TValue> = {
34
34
  }[keyof T];
35
35
 
36
36
  /**
37
- * Allows filtering data from an array of objects.
37
+ * A fluent, immutable-like query builder for in-memory collections.
38
38
  *
39
- * @example
40
- * ```ts
41
- * interface User {
42
- * id: string;
43
- * email: string;
44
- * isActive: boolean;
45
- * createdAt: string;
46
- * updatedAt: string;
47
- * }
39
+ * Provides a set of methods to filter, transform, group, aggregate and
40
+ * retrieve data from an array of objects, inspired by database query APIs.
48
41
  *
49
- * const users: User[] = [];
42
+ * All transformation methods (e.g. `select`, `map`) return a new `Query`
43
+ * instance, preserving the original query state. Filtering and stateful
44
+ * operations (e.g. `where`, `orderBy`, `skip`, `limit`) mutate the current instance.
50
45
  *
51
- * // Filtering objects
46
+ * @typeParam T - The shape of the objects being queried.
52
47
  *
53
- * const activeGmailUsers = Query.from(users)
48
+ * @example
49
+ * ```ts
50
+ * const results = Query.from(users)
54
51
  * .where({
55
- * isActive: true,
56
- * email: (email) => email.endsWith('@gmail.com'),
57
- * })
58
- * .all();
59
- *
60
- * // Selecting specific data
61
- *
62
- * const userEmails = Query.from(users)
63
- * .select('email')
64
- * .column();
65
- *
66
- * const lastUserId = Query.from(users)
67
- * .select('id')
52
+ * active: true,
53
+ * email: (email) => email.endsWith('@example.com'),
54
+ * })
68
55
  * .orderBy('-createdAt')
69
- * .scalar();
70
- *
71
- * // Checking information
56
+ * .select('id', 'name')
57
+ * .skip(10)
58
+ * .limit(5)
59
+ * .all();
60
+ * ```
72
61
  *
73
- * const userExists = Query.from(users)
74
- * .where({
75
- * id: 'some-id',
76
- * })
77
- * .exists();
62
+ * @example
63
+ * ```ts
64
+ * const total = Query.from(products)
65
+ * .where(p => p.category === 'books')
66
+ * .sum('price');
67
+ * ```
78
68
  *
79
- * const numberOfUsers = Query.from(users).count();
69
+ * @example
70
+ * ```ts
71
+ * const grouped = Query.from(users)
72
+ * .groupBy('role');
73
+ * // Map<Role, User[]>
80
74
  * ```
75
+ *
76
+ * @remarks
77
+ * - All operations are performed in memory.
78
+ * - Ordering is applied to the current dataset at the moment `orderBy` is called.
79
+ * - `skip` and `limit` are only applied when retrieving results (`all`, `first`, etc.).
81
80
  */
82
81
  declare class Query<T extends object> {
83
82
  #private;
@@ -112,6 +111,20 @@ declare class Query<T extends object> {
112
111
  * @returns New query with the mapped rows.
113
112
  */
114
113
  map<TReturn extends object>(callback: (obj: T) => TReturn): Query<TReturn>;
114
+ /**
115
+ * Removes duplicate rows based on a key.
116
+ *
117
+ * @param key Key to be used for comparison.
118
+ * @returns Current query.
119
+ */
120
+ distinct<K extends PropOf<T>>(key: K): this;
121
+ /**
122
+ * Removes duplicate rows based on a function.
123
+ *
124
+ * @param fn Function to be used for comparison.
125
+ * @returns Current query.
126
+ */
127
+ distinct<TValue>(fn: (row: T) => TValue): this;
115
128
  /**
116
129
  * Applies conditions to the query.
117
130
  *
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- var S=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var F=n=>{throw TypeError(n)};var x=(n,e,t,r)=>{for(var o=$(e,t),i=n.length-1,s;i>=0;i--)(s=n[i])&&(o=(s(e,t,o))||o);return o&&S(e,t,o),o},h=(n,e)=>(t,r)=>e(t,r,n);var N=(n,e,t)=>e.has(n)||F("Cannot "+t);var l=(n,e,t)=>(N(n,e,"read from private field"),t?t.call(n):e.get(n)),y=(n,e,t)=>e.has(n)?F("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(n):e.set(n,t),p=(n,e,t,r)=>(N(n,e,"write to private field"),e.set(n,t),t);var O=n=>typeof n=="number";var b=class extends Error{constructor(e){super(e),this.name="InvalidArgumentError";}};var v=new WeakMap;function V(n,e){let t=v.get(n);t||(t=new Map,v.set(n,t));let r=t.get(e);return r||(r=[],t.set(e,r)),r}function k(n){return function(e,t,r){let o=V(e,t),i=o.find(s=>s.index===r);i?i.min=n:o.push({index:r,min:n});}}function P(n,e,t){let r=V(n,e),o=r.find(i=>i.index===t);o?o.integer=true:r.push({index:t,integer:true});}function C(n,e,t){let r=t.value;t.value=function(...o){let i=v.get(n)?.get(e)??[];for(let s of i){let u=o[s.index];if(s.min!==void 0&&(!O(u)||u<s.min))throw new b(`${String(u)} is not a valid argument to param ${s.index} on ${e}(). Expected value to be equal or greater than ${s.min}.`);if(s.max!==void 0&&(!O(u)||u>s.max))throw new b(`${String(u)} is not a valid argument to param ${s.index} on ${e}(). Expected value to be equal or less than ${s.max}.`);if(s.integer&&!Number.isSafeInteger(u))throw new b(`${String(u)} is not a valid argument to param ${s.index} on ${e}(). Expected value to be an integer.`)}return r.apply(this,o)};}var d=n=>typeof n=="object"&&n!==null;var j=(n,e)=>{if(n===e)return true;if(!d(n)||!d(e)||Array.isArray(n)!==Array.isArray(e))return false;let t=Object.keys(n),r=Object.keys(e);return t.length!==r.length?false:t.every(o=>Object.prototype.hasOwnProperty.call(e,o)&&j(n[o],e[o]))};var A=(n,e)=>n.length===e.length&&n.every((t,r)=>j(t,e[r]));function G(n){return Object.entries(n)}var m=n=>typeof n=="function";var w,g=class{static validate(e,t,r){for(let[o,i]of G(t))if(!this.validateColumnCondition(e,o,i,r??l(this,w)))return false;return true}static validateColumnCondition(e,t,r,o){if(o.ignoreNullValues&&r==null)return true;let i=e[t];return m(r)?r(i):Array.isArray(r)?Array.isArray(i)?A(i,r):false:d(r)?d(i)?this.validate(i,r):false:i===r}};w=new WeakMap,y(g,w,{ignoreNullValues:false});function B(n){return Object.keys(n).filter(e=>typeof n[e]!="function")}function L(n){let e=1,t=n;t.startsWith("-")&&(e=-1,t=t.slice(1));let r=t;return (o,i)=>{let s=o[r],u=i[r];return s==null&&u==null?0:s==null?1*e:u==null||s<u?-1*e:s>u?1*e:0}}function M(...n){return (e,t)=>{if(n.length===0)return 0;for(let r of n){let o=L(r)(e,t);if(o!==0)return o}return 0}}var a,f,T,c=class c{constructor(e){y(this,a,[]);y(this,f,0);y(this,T,null);p(this,a,[...e]);}static from(e){return new c(e)}select(...e){let t=[];for(let o of l(this,a)){let i={};for(let s of e)i[s]=o[s];t.push(i);}let r=new c(t);return this.cloneStateInto(r),r}map(e){let t=[];for(let o of l(this,a))t.push(e(o));let r=new c(t);return this.cloneStateInto(r),r}where(e){return this.filterRows(e),this}filterWhere(e){return this.filterRows(e,{ignoreNullValues:true}),this}orderBy(...e){return p(this,a,l(this,a).sort(M(...e))),this}skip(e){return p(this,f,e),this}limit(e){return p(this,T,e),this}all(){return this.getLimitedRows()}first(){let e=this.getLimitedRows();return e.length>0?e[0]:null}last(){let e=this.getLimitedRows();return e[e.length-1]??null}count(){return this.getLimitedRows().length}exists(){return this.count()>0}scalar(){let e=this.first(),t=this.getFirstColumn();return e&&t?e[t]:false}column(e){if(e===void 0){let t=this.getFirstColumn();if(!t)return [];e=t;}return this.getLimitedRows().map(t=>t[e])}values(){let e=[];for(let t of this.getLimitedRows())e.push(Object.values(t));return e}groupBy(e,t){let r=this.getLimitedRows(),o=new Map,i=m(e);for(let s of r){let u=i?e(s):s[e],K=t?t(s):s;o.has(u)?o.get(u).push(K):o.set(u,[K]);}return o}min(e){let t=this.getLimitedRows();if(t.length===0)return null;let r=m(e)?t.map(o=>e(o)):t.map(o=>o[e]);return r.length===0?null:Math.min(...r)}max(e){let t=this.getLimitedRows();if(t.length===0)return null;let r=m(e)?t.map(o=>e(o)):t.map(o=>o[e]);return r.length===0?null:Math.max(...r)}sum(e){let t=this.getLimitedRows();return t.length===0?0:(m(e)?t.map(o=>e(o)):t.map(o=>o[e])).reduce((o,i)=>o+i,0)}average(e){let t=this.count();return t===0?null:(m(e)?this.sum(e):this.sum(e))/t}filterRows(e,t){let r=l(this,a),o=[];if(m(e))for(let i=0;i<r.length;i++){let s=r[i];e(s)&&o.push(s);}else for(let i=0;i<r.length;i++){let s=r[i];g.validate(s,e,t)&&o.push(s);}p(this,a,o);}getFirstColumn(){let e=this.first();if(!e)return null;let t=B(e);return t.length>0?t[0]:null}getLimitedRows(){let e=[...l(this,a)];return e.slice(l(this,f),l(this,f)+(l(this,T)??e.length))}cloneStateInto(e){p(e,f,l(this,f)),p(e,T,l(this,T));}};a=new WeakMap,f=new WeakMap,T=new WeakMap,x([C,h(0,P),h(0,k(0))],c.prototype,"skip"),x([C,h(0,P),h(0,k(0))],c.prototype,"limit");var R=c;export{R as Query};
1
+ var $=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var V=s=>{throw TypeError(s)};var O=(s,e,t,n)=>{for(var o=E(e,t),r=s.length-1,i;r>=0;r--)(i=s[r])&&(o=(i(e,t,o))||o);return o&&$(e,t,o),o},g=(s,e)=>(t,n)=>e(t,n,s);var F=(s,e,t)=>e.has(s)||V("Cannot "+t);var l=(s,e,t)=>(F(s,e,"read from private field"),e.get(s)),d=(s,e,t)=>e.has(s)?V("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(s):e.set(s,t),f=(s,e,t,n)=>(F(s,e,"write to private field"),e.set(s,t),t);var v=s=>typeof s=="number";var b=class extends Error{constructor(e){super(e),this.name="InvalidArgumentError";}};var k=new WeakMap;function N(s,e){let t=k.get(s);t||(t=new Map,k.set(s,t));let n=t.get(e);return n||(n=[],t.set(e,n)),n}function P(s){return function(e,t,n){let o=N(e,t),r=o.find(i=>i.index===n);r?r.min=s:o.push({index:n,min:s});}}function C(s,e,t){let n=N(s,e),o=n.find(r=>r.index===t);o?o.integer=true:n.push({index:t,integer:true});}function j(s,e,t){let n=t.value;t.value=function(...o){let r=k.get(s)?.get(e)??[];for(let i of r){let u=o[i.index];if(i.min!==void 0&&(!v(u)||u<i.min))throw new b(`${String(u)} is not a valid argument to param ${i.index} on ${e}(). Expected value to be equal or greater than ${i.min}.`);if(i.max!==void 0&&(!v(u)||u>i.max))throw new b(`${String(u)} is not a valid argument to param ${i.index} on ${e}(). Expected value to be equal or less than ${i.max}.`);if(i.integer&&!Number.isSafeInteger(u))throw new b(`${String(u)} is not a valid argument to param ${i.index} on ${e}(). Expected value to be an integer.`)}return n.apply(this,o)};}var h=s=>typeof s=="object"&&s!==null;var R=(s,e)=>{if(s===e)return true;if(!h(s)||!h(e)||Array.isArray(s)!==Array.isArray(e))return false;let t=Object.keys(s),n=Object.keys(e);return t.length!==n.length?false:t.every(o=>Object.prototype.hasOwnProperty.call(e,o)&&R(s[o],e[o]))};var G=(s,e)=>s.length===e.length&&s.every((t,n)=>R(t,e[n]));function B(s){return Object.entries(s)}var m=s=>typeof s=="function";var x,w=class{static validate(e,t,n){for(let[o,r]of B(t))if(!this.validateColumnCondition(e,o,r,n??l(this,x)))return false;return true}static validateColumnCondition(e,t,n,o){if(o.ignoreNullValues&&n==null)return true;let r=e[t];return m(n)?n(r):Array.isArray(n)?Array.isArray(r)?G(r,n):false:h(n)?h(r)?this.validate(r,n):false:r===n}};x=new WeakMap,d(w,x,{ignoreNullValues:false});function S(s){return Object.keys(s).filter(e=>typeof s[e]!="function")}function L(s){let e=1,t=s;t.startsWith("-")&&(e=-1,t=t.slice(1));let n=t;return (o,r)=>{let i=o[n],u=r[n];return i==null&&u==null?0:i==null?1*e:u==null||i<u?-1*e:i>u?1*e:0}}function M(...s){return (e,t)=>{if(s.length===0)return 0;for(let n of s){let o=L(n)(e,t);if(o!==0)return o}return 0}}var a,c,y,T=class T{constructor(e){d(this,a,[]);d(this,c,0);d(this,y,null);f(this,a,[...e]);}static from(e){return new T(e)}select(...e){let t=l(this,a),n=t.length,o=new Array(n);for(let i=0;i<n;i++){let u={};for(let p of e)u[p]=t[i][p];o[i]=u;}let r=new T(o);return this.cloneStateInto(r),r}map(e){let t=l(this,a),n=t.length,o=new Array(n);for(let i=0;i<n;i++)o[i]=e(t[i]);let r=new T(o);return this.cloneStateInto(r),r}distinct(e){let t=new Set,n=[],o=l(this,a),r=m(e);for(let i=0;i<o.length;i++){let u=o[i],p=r?e(u):u[e];t.has(p)||(t.add(p),n.push(u));}return f(this,a,n),this}where(e){return this.filterRows(e),this}filterWhere(e){return this.filterRows(e,{ignoreNullValues:true}),this}orderBy(...e){return f(this,a,l(this,a).sort(M(...e))),this}skip(e){return f(this,c,e),this}limit(e){return f(this,y,e),this}all(){return this.getLimitedRows()}first(){let e=this.getLimitedRows();return e.length>0?e[0]:null}last(){let e=this.getLimitedRows();return e[e.length-1]??null}count(){return this.getLimitedRows().length}exists(){return this.count()>0}scalar(){let e=this.first(),t=this.getFirstColumn();return e&&t?e[t]:false}column(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return [];if(e===void 0){let r=this.getFirstColumn();if(!r)return [];e=r;}let o=new Array(n);for(let r=0;r<n;r++)o[r]=t[r][e];return o}values(){let e=this.getLimitedRows(),t=e.length,n=new Array(t);for(let o=0;o<t;o++)n[o]=Object.values(e[o]);return n}groupBy(e,t){let n=this.getLimitedRows(),o=new Map,r=m(e);for(let i=0;i<n.length;i++){let u=n[i],p=r?e(u):u[e],A=t?t(u):u;o.has(p)?o.get(p).push(A):o.set(p,[A]);}return o}min(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return null;let o=new Array(n);if(m(e))for(let r=0;r<n;r++)o[r]=e(t[r]);else for(let r=0;r<n;r++)o[r]=t[r][e];return Math.min(...o)}max(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return null;let o=new Array(n);if(m(e))for(let r=0;r<n;r++)o[r]=e(t[r]);else for(let r=0;r<n;r++)o[r]=t[r][e];return Math.max(...o)}sum(e){let t=this.getLimitedRows(),n=t.length;if(n===0)return 0;let o=new Array(n);if(m(e))for(let r=0;r<n;r++)o[r]=e(t[r]);else for(let r=0;r<n;r++)o[r]=t[r][e];return o.reduce((r,i)=>r+i,0)}average(e){let t=this.count();return t===0?null:(m(e)?this.sum(e):this.sum(e))/t}filterRows(e,t){let n=l(this,a),o=[];if(m(e))for(let r=0;r<n.length;r++){let i=n[r];e(i)&&o.push(i);}else for(let r=0;r<n.length;r++){let i=n[r];w.validate(i,e,t)&&o.push(i);}f(this,a,o);}getFirstColumn(){let e=this.first();if(!e)return null;let t=S(e);return t.length>0?t[0]:null}getLimitedRows(){return l(this,a).slice(l(this,c),l(this,c)+(l(this,y)??l(this,a).length))}cloneStateInto(e){f(e,c,l(this,c)),f(e,y,l(this,y));}};a=new WeakMap,c=new WeakMap,y=new WeakMap,O([j,g(0,C),g(0,P(0))],T.prototype,"skip"),O([j,g(0,C),g(0,P(0))],T.prototype,"limit");var K=T;export{K as Query};
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "querier-ts",
3
3
  "type": "module",
4
- "version": "2.4.0",
4
+ "version": "2.5.0",
5
5
  "description": "A lightweight, type-safe in-memory query engine for JavaScript and TypeScript",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://github.com/luizfilipezs/query-ts.git"
8
+ "url": "git+https://github.com/luizfilipezs/querier-ts.git"
9
9
  },
10
10
  "keywords": [
11
11
  "query",
@@ -19,9 +19,9 @@
19
19
  },
20
20
  "license": "MIT",
21
21
  "bugs": {
22
- "url": "https://github.com/luizfilipezs/query-ts/issues"
22
+ "url": "https://github.com/luizfilipezs/querier-ts/issues"
23
23
  },
24
- "homepage": "https://github.com/luizfilipezs/query-ts#readme",
24
+ "homepage": "https://github.com/luizfilipezs/querier-ts#readme",
25
25
  "files": [
26
26
  "dist",
27
27
  "README.md",
@@ -61,7 +61,7 @@
61
61
  "devDependencies": {
62
62
  "@eslint/js": "^10.0.1",
63
63
  "@vitest/coverage-v8": "^4.1.2",
64
- "eslint": "^10.0.3",
64
+ "eslint": "^10.1.0",
65
65
  "eslint-config-prettier": "^10.1.8",
66
66
  "eslint-plugin-prettier": "^5.5.5",
67
67
  "globals": "^17.4.0",
@@ -72,7 +72,8 @@
72
72
  "prettier-plugin-organize-imports": "^4.3.0",
73
73
  "tsup": "^8.5.1",
74
74
  "typescript": "~5.9.3",
75
- "typescript-eslint": "^8.57.1",
75
+ "typescript-eslint": "^8.58.0",
76
76
  "vitest": "^4.1.2"
77
- }
77
+ },
78
+ "packageManager": "pnpm@10.33.0"
78
79
  }