querier-ts 2.6.0 → 2.7.1

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,33 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.7.1
4
+
5
+ ### Summary
6
+
7
+ - [x] Documentation updates
8
+ - [ ] Bug fixes
9
+ - [ ] Code refactoring
10
+ - [ ] New features
11
+ - [ ] Breaking changes
12
+
13
+ ### New features
14
+
15
+ - Update `README.md` documentation for `orderBy()` method with new overload and examples.
16
+
17
+ ## v2.7.0
18
+
19
+ ### Summary
20
+
21
+ - [ ] Bug fixes
22
+ - [ ] Code refactoring
23
+ - [x] New features
24
+ - [ ] Build and packaging updates
25
+ - [ ] Breaking changes
26
+
27
+ ### New features
28
+
29
+ - Added new overload to `orderBy()` method, allowing to specify a function that returns the values to be sorted and the order to be applied.
30
+
3
31
  ## v2.6.0
4
32
 
5
33
  ### Summary
package/README.md CHANGED
@@ -144,23 +144,30 @@ const query = Query.from()
144
144
 
145
145
  ### Ordering results
146
146
 
147
- #### `orderBy(...columns)`
147
+ #### `orderBy(...columns | (callback, order?))`
148
148
 
149
- Sorts the results. You can pass multiple columns.
149
+ Sorts the results.
150
150
 
151
- ```ts
152
- .orderBy('name', 'id')
153
- ```
151
+ - `columns`: the columns to order by.
152
+ - `callback`: A function that maps each row to a value.
153
+ - `order`: The order to sort in.
154
154
 
155
- In this example, `name` has higher priority than `id`.
155
+ You can specify column names or a callback function to map each row to a value.
156
156
 
157
- You can also sort in descending order by prefixing the column with `-`:
157
+ Examples:
158
158
 
159
159
  ```ts
160
- const lastId = Query.from(users)
161
- .select('id')
162
- .orderBy('-id')
163
- .scalar();
160
+ // Column names with ASC and DESC (-) order
161
+ .orderBy('name', '-createdAt')
162
+
163
+ // callback with ASC order by default
164
+ .orderBy((user) => user.address.country)
165
+
166
+ // callback with ASC order set explicitly
167
+ .orderBy((user) => user.address.country, 'asc')
168
+
169
+ // callback with DESC order
170
+ .orderBy((user) => user.address.country, 'desc')
164
171
  ```
165
172
 
166
173
  ---
@@ -227,6 +234,8 @@ const countries = Query.from(addresses)
227
234
  .column('country');
228
235
  ```
229
236
 
237
+ > Passing a non-integer or a negative number to `limit` will throw an `InvalidArgumentError`.
238
+
230
239
  ---
231
240
 
232
241
  #### `top(limit, options?)`
@@ -273,6 +282,7 @@ const countries = Query.from(addresses)
273
282
  .column('country'); // ['Brazil', 'Chile', 'Argentina']
274
283
  ```
275
284
 
285
+ > Passing a non-integer or a negative number to `limit` will throw an `InvalidArgumentError`.
276
286
 
277
287
  ---
278
288
 
@@ -185,6 +185,21 @@ function getObjectPropertyNames(obj) {
185
185
  );
186
186
  }
187
187
 
188
+ // src/utils/functions/sort/sort-values.ts
189
+ function sortValues(a, b, order) {
190
+ if (a == null && b == null) return 0;
191
+ if (a == null) return 1 * order;
192
+ if (b == null) return -1 * order;
193
+ if (a < b) return -1 * order;
194
+ if (a > b) return 1 * order;
195
+ return 0;
196
+ }
197
+
198
+ // src/utils/functions/sort/sort-by-callback.ts
199
+ function sortByCallback(callback, sortOrder) {
200
+ return (a, b) => sortValues(callback(a), callback(b), sortOrder);
201
+ }
202
+
188
203
  // src/utils/functions/sort/sort-by-property.ts
189
204
  function sortByProperty(property) {
190
205
  let sortOrder = 1;
@@ -197,12 +212,7 @@ function sortByProperty(property) {
197
212
  return (a, b) => {
198
213
  const valueA = a[key];
199
214
  const valueB = b[key];
200
- if (valueA == null && valueB == null) return 0;
201
- if (valueA == null) return 1 * sortOrder;
202
- if (valueB == null) return -1 * sortOrder;
203
- if (valueA < valueB) return -1 * sortOrder;
204
- if (valueA > valueB) return 1 * sortOrder;
205
- return 0;
215
+ return sortValues(valueA, valueB, sortOrder);
206
216
  };
207
217
  }
208
218
 
@@ -339,32 +349,19 @@ var _Query = class _Query {
339
349
  this.filterRows(condition, { ignoreNullValues: true });
340
350
  return this;
341
351
  }
342
- /**
343
- * Adds ordering to the results.
344
- *
345
- * This method should be called after `select()`; otherwise, the ordering will
346
- * be applied only to the selected columns.
347
- *
348
- * @example
349
- * ```ts
350
- * // ❌ "age" will not be ordered, because it is not part of the selected columns
351
- * const query = Query.from(users)
352
- * .orderBy('-age')
353
- * .select('name');
354
- *
355
- * // ✅ "age" will be ordered
356
- * const query = Query.from(users)
357
- * .select('name', 'age')
358
- * .orderBy('-age');
359
- * ```
360
- *
361
- * @param columns Ascending or descending columns. To mark a field as
362
- * descending, prefix it with `-`.
363
- *
364
- * @returns Current query.
365
- */
366
- orderBy(...columns) {
367
- this.#rows = this.#rows.sort(sortByProperties(...columns));
352
+ orderBy(...arg) {
353
+ if (arg.length === 0) {
354
+ return this;
355
+ }
356
+ if (isFunction(arg[0])) {
357
+ this.#rows = this.#rows.sort(
358
+ sortByCallback(arg[0], arg[1] === "desc" ? -1 : 1)
359
+ );
360
+ } else {
361
+ this.#rows = this.#rows.sort(
362
+ sortByProperties(...arg)
363
+ );
364
+ }
368
365
  return this;
369
366
  }
370
367
  skip(numberOfRows) {
@@ -154,28 +154,21 @@ declare class Query<T extends object> {
154
154
  /**
155
155
  * Adds ordering to the results.
156
156
  *
157
- * This method should be called after `select()`; otherwise, the ordering will
158
- * be applied only to the selected columns.
159
- *
160
- * @example
161
- * ```ts
162
- * // ❌ "age" will not be ordered, because it is not part of the selected columns
163
- * const query = Query.from(users)
164
- * .orderBy('-age')
165
- * .select('name');
166
- *
167
- * // ✅ "age" will be ordered
168
- * const query = Query.from(users)
169
- * .select('name', 'age')
170
- * .orderBy('-age');
171
- * ```
172
- *
173
157
  * @param columns Ascending or descending columns. To mark a field as
174
158
  * descending, prefix it with `-`.
175
159
  *
176
160
  * @returns Current query.
177
161
  */
178
162
  orderBy(...columns: OrderingColumn<T>[]): this;
163
+ /**
164
+ * Adds ordering to the results.
165
+ *
166
+ * @param fn Function to map each row to a value.
167
+ * @param order Sort order. Defaults to `asc`.
168
+ *
169
+ * @returns Current query.
170
+ */
171
+ orderBy<TReturn>(fn: (row: T) => TReturn, order?: 'asc' | 'desc'): this;
179
172
  /**
180
173
  * Defines the number of rows to skip.
181
174
  *
package/dist/esm/index.js CHANGED
@@ -183,6 +183,21 @@ function getObjectPropertyNames(obj) {
183
183
  );
184
184
  }
185
185
 
186
+ // src/utils/functions/sort/sort-values.ts
187
+ function sortValues(a, b, order) {
188
+ if (a == null && b == null) return 0;
189
+ if (a == null) return 1 * order;
190
+ if (b == null) return -1 * order;
191
+ if (a < b) return -1 * order;
192
+ if (a > b) return 1 * order;
193
+ return 0;
194
+ }
195
+
196
+ // src/utils/functions/sort/sort-by-callback.ts
197
+ function sortByCallback(callback, sortOrder) {
198
+ return (a, b) => sortValues(callback(a), callback(b), sortOrder);
199
+ }
200
+
186
201
  // src/utils/functions/sort/sort-by-property.ts
187
202
  function sortByProperty(property) {
188
203
  let sortOrder = 1;
@@ -195,12 +210,7 @@ function sortByProperty(property) {
195
210
  return (a, b) => {
196
211
  const valueA = a[key];
197
212
  const valueB = b[key];
198
- if (valueA == null && valueB == null) return 0;
199
- if (valueA == null) return 1 * sortOrder;
200
- if (valueB == null) return -1 * sortOrder;
201
- if (valueA < valueB) return -1 * sortOrder;
202
- if (valueA > valueB) return 1 * sortOrder;
203
- return 0;
213
+ return sortValues(valueA, valueB, sortOrder);
204
214
  };
205
215
  }
206
216
 
@@ -337,32 +347,19 @@ var _Query = class _Query {
337
347
  this.filterRows(condition, { ignoreNullValues: true });
338
348
  return this;
339
349
  }
340
- /**
341
- * Adds ordering to the results.
342
- *
343
- * This method should be called after `select()`; otherwise, the ordering will
344
- * be applied only to the selected columns.
345
- *
346
- * @example
347
- * ```ts
348
- * // ❌ "age" will not be ordered, because it is not part of the selected columns
349
- * const query = Query.from(users)
350
- * .orderBy('-age')
351
- * .select('name');
352
- *
353
- * // ✅ "age" will be ordered
354
- * const query = Query.from(users)
355
- * .select('name', 'age')
356
- * .orderBy('-age');
357
- * ```
358
- *
359
- * @param columns Ascending or descending columns. To mark a field as
360
- * descending, prefix it with `-`.
361
- *
362
- * @returns Current query.
363
- */
364
- orderBy(...columns) {
365
- this.#rows = this.#rows.sort(sortByProperties(...columns));
350
+ orderBy(...arg) {
351
+ if (arg.length === 0) {
352
+ return this;
353
+ }
354
+ if (isFunction(arg[0])) {
355
+ this.#rows = this.#rows.sort(
356
+ sortByCallback(arg[0], arg[1] === "desc" ? -1 : 1)
357
+ );
358
+ } else {
359
+ this.#rows = this.#rows.sort(
360
+ sortByProperties(...arg)
361
+ );
362
+ }
366
363
  return this;
367
364
  }
368
365
  skip(numberOfRows) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "querier-ts",
3
3
  "type": "module",
4
- "version": "2.6.0",
4
+ "version": "2.7.1",
5
5
  "description": "A lightweight, type-safe in-memory query engine for JavaScript and TypeScript",
6
6
  "repository": {
7
7
  "type": "git",
@@ -68,7 +68,7 @@
68
68
  "husky": "^9.1.7",
69
69
  "jiti": "^2.6.1",
70
70
  "lint-staged": "^16.4.0",
71
- "prettier": "^3.8.1",
71
+ "prettier": "^3.8.2",
72
72
  "prettier-plugin-organize-imports": "^4.3.0",
73
73
  "tsup": "^8.5.1",
74
74
  "typescript": "~5.9.3",