@tmlmobilidade/utils 20250628.1623.5 → 20250628.1632.4

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.
@@ -1,4 +1,4 @@
1
- import { OperationalDate } from '@tmlmobilidade/types';
1
+ import { type OperationalDate, type UnixTimestamp } from '@tmlmobilidade/types';
2
2
  /**
3
3
  * Returns an array of individual dates from a given range of operational dates.
4
4
  * @param start The start date of the range, in OperationalDate format.
@@ -6,3 +6,12 @@ import { OperationalDate } from '@tmlmobilidade/types';
6
6
  * @returns An array of OperationalDate objects representing each date in the range.
7
7
  */
8
8
  export declare function getOperationalDatesFromRange(start: OperationalDate, end: OperationalDate): OperationalDate[];
9
+ /**
10
+ * Sorts an array of objects by a Unix timestamp key in ascending or descending order.
11
+ * The key must be of type UnixTimestamp, which is a number representing the timestamp in milliseconds.
12
+ * @param data The array of objects to be sorted.
13
+ * @param key The key of the objects to sort by, which must be a UnixTimestamp.
14
+ * @param direction The direction to sort the data, either 'asc' for ascending or 'desc' for descending. Defaults to 'asc'.
15
+ * @returns A new array of objects sorted by the specified Unix timestamp key.
16
+ */
17
+ export declare function sortByUnixTimestamp<T, K extends keyof T>(data: T[], key: K extends keyof T ? (T[K] extends UnixTimestamp ? K : never) : never, direction?: 'asc' | 'desc'): T[];
@@ -29,3 +29,25 @@ export function getOperationalDatesFromRange(start, end) {
29
29
  }
30
30
  return dates;
31
31
  }
32
+ /**
33
+ * Sorts an array of objects by a Unix timestamp key in ascending or descending order.
34
+ * The key must be of type UnixTimestamp, which is a number representing the timestamp in milliseconds.
35
+ * @param data The array of objects to be sorted.
36
+ * @param key The key of the objects to sort by, which must be a UnixTimestamp.
37
+ * @param direction The direction to sort the data, either 'asc' for ascending or 'desc' for descending. Defaults to 'asc'.
38
+ * @returns A new array of objects sorted by the specified Unix timestamp key.
39
+ */
40
+ export function sortByUnixTimestamp(data, key, direction = 'asc') {
41
+ //
42
+ if (direction === 'desc') {
43
+ return [...data].sort((a, b) => {
44
+ return b[key] - a[key];
45
+ });
46
+ }
47
+ //
48
+ // If direction is 'asc'
49
+ return [...data].sort((a, b) => {
50
+ return a[key] - b[key];
51
+ });
52
+ //
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20250628.1623.5",
3
+ "version": "20250628.1632.4",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",