@dephub/sort-date 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +58 -2
- package/dist/core/sort-date.d.ts +16 -0
- package/dist/core/sort-date.js +21 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +8 -0
- package/package.json +64 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Estarlin R
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dephub/sort-date 📅
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Sort arrays of objects in place in descending order by a date property.
|
|
4
|
+
|
|
5
|
+
[](https://npmjs.org/package/@dephub/sort-date)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
|
|
8
|
+
## Features ✨
|
|
9
|
+
|
|
10
|
+
- 📅 **Descending Chronological Sort**: Orders items from newest to oldest using a specified date property.
|
|
11
|
+
- 🔢 **Flexible Date Inputs**: Supports both ISO/date strings and numeric timestamps.
|
|
12
|
+
- 🛡️ **Runtime Validation**: Throws descriptive errors when date values have invalid types or cannot be parsed.
|
|
13
|
+
- 🎯 **Type-Safe Keys**: Restricts target properties to valid keys of your object type (`keyof T`).
|
|
14
|
+
- ⚡ **Zero Dependencies**: Lightweight, ESM-only TypeScript implementation.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation 📦
|
|
19
|
+
|
|
20
|
+
- npm: `npm install @dephub/sort-date`
|
|
21
|
+
- pnpm: `pnpm add @dephub/sort-date`
|
|
22
|
+
- yarn: `yarn add @dephub/sort-date`
|
|
23
|
+
- bun: `bun add @dephub/sort-date`
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Usage 🎯
|
|
28
|
+
|
|
29
|
+
### API 🧩
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { sortByDate } from '@dephub/sort-date';
|
|
33
|
+
|
|
34
|
+
const events = [
|
|
35
|
+
{ name: 'Event 1', date: '2023-05-01' },
|
|
36
|
+
{ name: 'Event 2', date: '2024-03-01' },
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
// Sorts in place from newest to oldest
|
|
40
|
+
const sorted = sortByDate(events, 'date');
|
|
41
|
+
// [
|
|
42
|
+
// { name: 'Event 2', date: '2024-03-01' },
|
|
43
|
+
// { name: 'Event 1', date: '2023-05-01' },
|
|
44
|
+
// ]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Available exports
|
|
48
|
+
|
|
49
|
+
| Category | Functions |
|
|
50
|
+
| -------- | ------------ |
|
|
51
|
+
| **Date** | `sortByDate` |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## License 📄
|
|
56
|
+
|
|
57
|
+
MIT License – see [LICENSE](https://www.google.com/search?q=LICENSE) for details.
|
|
58
|
+
|
|
59
|
+
**Author:** Estarlin R ([estarlincito.com](https://estarlincito.com))
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sorts an array of objects in place in descending order by a date property.
|
|
3
|
+
* @template T - The type of the objects in the array.
|
|
4
|
+
*
|
|
5
|
+
* @param {T[]} data - Array of objects to sort in place.
|
|
6
|
+
* @param {keyof T} key - Property key holding a date string or numeric timestamp.
|
|
7
|
+
* @throws {Error} When a property value is not a string or number, or cannot be parsed into a valid date.
|
|
8
|
+
* @returns {T[]} The same array sorted from newest to oldest.
|
|
9
|
+
* @example
|
|
10
|
+
* const data = [
|
|
11
|
+
* { name: 'Event 1', date: '2023-05-01' },
|
|
12
|
+
* { name: 'Event 2', date: '2024-03-01' },
|
|
13
|
+
* ];
|
|
14
|
+
* const sortedData = sortByDate(data, 'date');
|
|
15
|
+
*/
|
|
16
|
+
export declare function sortByDate<T extends object>(data: T[], key: keyof T): T[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { parseDate } from "../utils.js";
|
|
2
|
+
// src/core/sort-date.ts
|
|
3
|
+
/**
|
|
4
|
+
* Sorts an array of objects in place in descending order by a date property.
|
|
5
|
+
* @template T - The type of the objects in the array.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} data - Array of objects to sort in place.
|
|
8
|
+
* @param {keyof T} key - Property key holding a date string or numeric timestamp.
|
|
9
|
+
* @throws {Error} When a property value is not a string or number, or cannot be parsed into a valid date.
|
|
10
|
+
* @returns {T[]} The same array sorted from newest to oldest.
|
|
11
|
+
* @example
|
|
12
|
+
* const data = [
|
|
13
|
+
* { name: 'Event 1', date: '2023-05-01' },
|
|
14
|
+
* { name: 'Event 2', date: '2024-03-01' },
|
|
15
|
+
* ];
|
|
16
|
+
* const sortedData = sortByDate(data, 'date');
|
|
17
|
+
*/
|
|
18
|
+
function sortByDate(data, key) {
|
|
19
|
+
return data.sort((a, b) => parseDate(b[key], key) - parseDate(a[key], key));
|
|
20
|
+
}
|
|
21
|
+
export { sortByDate };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sortByDate } from './core/sort-date.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sortByDate } from "./core/sort-date.js";
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseDate(value: unknown, key: PropertyKey): number;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function parseDate(value, key) {
|
|
3
|
+
if (typeof value !== "string" && typeof value !== "number") throw new Error(`Invalid date value for key "${String(key)}".`);
|
|
4
|
+
const timestamp = new Date(value).valueOf();
|
|
5
|
+
if (Number.isNaN(timestamp)) throw new Error(`Unparseable date value for key "${String(key)}".`);
|
|
6
|
+
return timestamp;
|
|
7
|
+
}
|
|
8
|
+
export { parseDate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,67 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dephub/sort-date",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Sort arrays of objects in place in descending order by a date property.",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"date",
|
|
10
|
+
"sort-date",
|
|
11
|
+
"typescript",
|
|
12
|
+
"nodejs",
|
|
13
|
+
"dephub"
|
|
14
|
+
],
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Estarlin R",
|
|
17
|
+
"email": "dev@estarlincito.com",
|
|
18
|
+
"url": "https://estarlincito.com"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.md",
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"homepage": "https://github.com/dephub-js/sort-date#readme",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/dephub-js/sort-date.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/dephub-js/sort-date/issues"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@dephub/eslint-ts": "^1.0.6",
|
|
39
|
+
"@dephub/glob": "^1.0.3",
|
|
40
|
+
"@dephub/path": "^2.0.1",
|
|
41
|
+
"@types/node": "^26.6.10",
|
|
42
|
+
"eslint": "^10.15.0",
|
|
43
|
+
"prettier": "^3.10.1",
|
|
44
|
+
"typescript": "^6.0.3",
|
|
45
|
+
"vite": "^8.3.8",
|
|
46
|
+
"vite-plugin-dts": "^5.1.2"
|
|
47
|
+
},
|
|
48
|
+
"imports": {
|
|
49
|
+
"#/*": "./src/*"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=24.0.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"release": "pnpm publish",
|
|
56
|
+
"docs": "lineo md 'src/**/*.ts' --outFile code.md --outDir temp",
|
|
57
|
+
"test": "node --test",
|
|
58
|
+
"check": "pnpm fmt && pnpm lint && pnpm check-types",
|
|
59
|
+
"check-types": "tsc --noEmit --skipLibCheck",
|
|
60
|
+
"lint": "eslint . --fix --max-warnings 0",
|
|
61
|
+
"fmt": "prettier --write .",
|
|
62
|
+
"clean": "rm -rf dist",
|
|
63
|
+
"pack": "pnpm pack",
|
|
64
|
+
"build": "vite build",
|
|
65
|
+
"dev": "vite build -w --mode development"
|
|
66
|
+
}
|
|
6
67
|
}
|