@hackthedev/datetools 1.0.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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
@@ -0,0 +1,43 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ permissions:
9
+ contents: write
10
+
11
+ jobs:
12
+ publish:
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ with:
18
+ persist-credentials: true
19
+
20
+ - name: Skip version bump commits
21
+ run: |
22
+ if git log -1 --pretty=%B | grep -q "chore: bump version"; then
23
+ echo "Version bump commit detected, skipping."
24
+ exit 0
25
+ fi
26
+
27
+ - uses: actions/setup-node@v4
28
+ with:
29
+ node-version: 20
30
+ registry-url: https://registry.npmjs.org/
31
+
32
+ - run: npm ci
33
+
34
+ - run: |
35
+ git config user.name "github-actions"
36
+ git config user.email "actions@github.com"
37
+ npm version patch -m "chore: bump version %s"
38
+ git push
39
+
40
+ - run: npm publish --access public
41
+ env:
42
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
43
+
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # date-tools
2
+
package/index.mjs ADDED
@@ -0,0 +1,78 @@
1
+ export default class DateTools {
2
+ static formatDate(date) {
3
+ if (!(date instanceof Date)) {
4
+ throw new Error("Invalid date: Please pass a valid Date object.");
5
+ }
6
+
7
+ const formatter = new Intl.DateTimeFormat("en-US", {
8
+ month: "long", // Full month name
9
+ day: "numeric", // Numeric day
10
+ year: "numeric", // Full year
11
+ hour: "numeric", // Hour
12
+ minute: "numeric", // Minute
13
+ hour12: false // Use 12-hour format
14
+ });
15
+
16
+ let formattedDate = formatter.format(date);
17
+
18
+ // Replace "at" with a comma, if present
19
+ return formattedDate.replace(" at ", ", ");
20
+ }
21
+
22
+ static getReadableDuration(date) {
23
+ let untilTimestamp = date.getTime();
24
+
25
+ const remainingTime = untilTimestamp - Date.now();
26
+ if (remainingTime <= 0) return "Expired";
27
+
28
+ const seconds = Math.floor(remainingTime / 1000) % 60;
29
+ const minutes = Math.floor(remainingTime / (1000 * 60)) % 60;
30
+ const hours = Math.floor(remainingTime / (1000 * 60 * 60)) % 24;
31
+ const days = Math.floor(remainingTime / (1000 * 60 * 60 * 24));
32
+
33
+ return `${days}d ${hours}h ${minutes}m ${seconds}s`;
34
+ }
35
+
36
+ static getDateFromOffset(offset) {
37
+ if(!offset) throw new Error("No offset provided. Examples: -1 day, +2hours, ...");
38
+
39
+ const units = {
40
+ seconds: 1000,
41
+ second: 1000,
42
+ minutes: 1000 * 60,
43
+ minute: 1000 * 60,
44
+ hours: 1000 * 60 * 60,
45
+ hour: 1000 * 60 * 60,
46
+ days: 1000 * 60 * 60 * 24,
47
+ day: 1000 * 60 * 60 * 24,
48
+ months: "months",
49
+ month: "months",
50
+ years: "years",
51
+ year: "years",
52
+ perma: "perma"
53
+ };
54
+
55
+ const [amountStr, unit] = offset.split(" ");
56
+ const amount = parseInt(amountStr, 10);
57
+
58
+ if (unit === "perma") {
59
+ return new Date("9999-12-31T23:59:59Z");
60
+ }
61
+
62
+ if (isNaN(amount) || !units[unit]) {
63
+ throw new Error("Invalid offset format. Use '<number> <unit>' (e.g., '-1 day', '+2 hours').");
64
+ }
65
+
66
+ const now = new Date();
67
+
68
+ if (units[unit] === "months") {
69
+ now.setMonth(now.getMonth() + amount);
70
+ } else if (units[unit] === "years") {
71
+ now.setFullYear(now.getFullYear() + amount);
72
+ } else {
73
+ now.setTime(now.getTime() + amount * units[unit]);
74
+ }
75
+
76
+ return now;
77
+ }
78
+ }
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@hackthedev/datetools",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "homepage": "https://github.com/NETWORK-Z-Dev/date-tools#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/NETWORK-Z-Dev/date-tools/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/NETWORK-Z-Dev/date-tools.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "",
15
+ "main": "index.mjs",
16
+ "scripts": {
17
+ "test": "echo \"Error: no test specified\" && exit 1"
18
+ }
19
+ }