time-ago-dk 1.1.0 → 1.1.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/README.md CHANGED
@@ -7,7 +7,7 @@ A tiny JavaScript utility to convert dates into human readable "time ago" format
7
7
  - Lightweight and fast
8
8
  - Supports years, months, days, hours, minutes, and seconds
9
9
  - Returns "just now" for very recent times
10
- - ES module compatible
10
+ - ES module and CommonJS compatible
11
11
 
12
12
  ## Installation
13
13
 
@@ -17,6 +17,8 @@ npm install time-ago-dk
17
17
 
18
18
  ## Usage
19
19
 
20
+ ### ES Modules
21
+
20
22
  ```javascript
21
23
  import { timeAgo } from "time-ago-dk";
22
24
 
@@ -28,6 +30,19 @@ console.log(timeAgo(new Date(Date.now() - 2592000000))); // "1 month ago"
28
30
  console.log(timeAgo(new Date(Date.now() - 31536000000))); // "1 year ago"
29
31
  ```
30
32
 
33
+ ### CommonJS
34
+
35
+ ```javascript
36
+ const { timeAgo } = require("time-ago-dk");
37
+
38
+ console.log(timeAgo(new Date(Date.now() - 60000))); // "1 minute ago"
39
+ console.log(timeAgo(new Date(Date.now() - 3600000))); // "1 hour ago"
40
+ console.log(timeAgo(new Date(Date.now() - 1000))); // "just now"
41
+ console.log(timeAgo(new Date(Date.now() - 86400000))); // "1 day ago"
42
+ console.log(timeAgo(new Date(Date.now() - 2592000000))); // "1 month ago"
43
+ console.log(timeAgo(new Date(Date.now() - 31536000000))); // "1 year ago"
44
+ ```
45
+
31
46
  ## API
32
47
 
33
48
  ### `timeAgo(inputDate)`
package/package.json CHANGED
@@ -1,9 +1,16 @@
1
1
  {
2
2
  "name": "time-ago-dk",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A tiny JavaScript utility to convert dates into human readable 'time ago' format.",
5
- "main": "src/index.js",
5
+ "main": "src/index.cjs",
6
+ "module": "src/index.js",
6
7
  "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./src/index.js",
11
+ "require": "./src/index.cjs"
12
+ }
13
+ },
7
14
  "repository": {
8
15
  "type": "git",
9
16
  "url": "https://github.com/Darshit1101/npm-time-ago.git"
package/src/index.cjs ADDED
@@ -0,0 +1,27 @@
1
+ function timeAgo(inputDate) {
2
+ const now = new Date();
3
+ const date = new Date(inputDate);
4
+
5
+ const seconds = Math.floor((now - date) / 1000);
6
+
7
+ if (seconds < 5) return "just now";
8
+
9
+ const intervals = [
10
+ { label: "year", seconds: 31536000 },
11
+ { label: "month", seconds: 2592000 },
12
+ { label: "day", seconds: 86400 },
13
+ { label: "hour", seconds: 3600 },
14
+ { label: "minute", seconds: 60 },
15
+ ];
16
+
17
+ for (const interval of intervals) {
18
+ const count = Math.floor(seconds / interval.seconds);
19
+ if (count >= 1) {
20
+ return `${count} ${interval.label}${count > 1 ? "s" : ""} ago`;
21
+ }
22
+ }
23
+
24
+ return `${seconds} seconds ago`;
25
+ }
26
+
27
+ module.exports = { timeAgo };
@@ -0,0 +1,9 @@
1
+ const { timeAgo } = require("../src/index.cjs");
2
+
3
+ // Test cases
4
+ console.log(timeAgo(new Date(Date.now() - 31536000000))); // 1 year ago
5
+ console.log(timeAgo(new Date(Date.now() - 2592000000))); // 1 month ago
6
+ console.log(timeAgo(new Date(Date.now() - 86400000))); // 1 day ago
7
+ console.log(timeAgo(new Date(Date.now() - 60000))); // 1 minute ago
8
+ console.log(timeAgo(new Date(Date.now() - 3600000))); // 1 hour ago
9
+ console.log(timeAgo(new Date(Date.now() - 1000))); // just now