Skip to content

Commit

Permalink
Merge pull request #4 from marcreichel/include-seconds
Browse files Browse the repository at this point in the history
✨ Add include seconds option
  • Loading branch information
marcreichel authored Jun 22, 2022
2 parents bd904bc + 2703c19 commit a724845
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<img src="https://img.shields.io/github/v/tag/marcreichel/alpine-timeago?label=version" alt="version">
</a>
<a href="https://www.npmjs.com/package/@marcreichel/alpine-timeago">
<img src="https://img.badgesize.io/marcreichel/alpine-timeago/main/dist/alpine-timeago.js.svg?compression=gzip&color=green" alt="Build size">
<img src="https://img.badgesize.io/marcreichel/alpine-timeago/main/dist/alpine-timeago.min.js.svg?compression=gzip&color=green" alt="Build size">
</a>
<a href="https://www.npmjs.com/package/@marcreichel/alpine-timeago">
<img src="https://img.shields.io/npm/dt/@marcreichel/alpine-timeago" alt="downloads">
Expand Down Expand Up @@ -74,6 +74,14 @@ If you do not want the "[diff] ago" suffix or "in [diff]" prefix, you can use th
<span x-data="{ date: new Date() }" x-timeago.pure="date"></span>
```

### Include seconds

Distances less than a minute are more detailed.

```html
<span x-data="{ date: new Date() }" x-timeago.seconds="date"></span>
```

### Other locales

If you are using the `npm` installation method for this package or the ESM distribution, you can use the
Expand Down
22 changes: 15 additions & 7 deletions dist/alpine-timeago.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/alpine-timeago.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/alpine-timeago.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/alpine-timeago.min.js.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Alpine Timeago Examples</title>

<script defer src="../dist/alpine-timeago.min.js"></script>
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
<div x-data="{ date: new Date() }" x-timeago="date"></div>
<div x-data="{ date: new Date() }" x-timeago.pure="date"></div>
<div x-data="{ date: new Date() }" x-timeago.seconds="date"></div>
<div x-data="{ date: new Date() }" x-timeago.pure.seconds="date"></div>
</body>
</html>
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatDistanceToNow, parseISO } from 'date-fns';
import { differenceInSeconds, formatDistanceToNow, parseISO } from 'date-fns';

let locale = null;

Expand All @@ -8,11 +8,9 @@ function TimeAgo(Alpine) {

const render = (date) => {
try {
if (typeof date === 'string') {
date = parseISO(date);
}
el.textContent = formatDistanceToNow(date, {
addSuffix: !modifiers.includes('pure'),
includeSeconds: modifiers.includes('seconds'),
locale,
});
} catch (e) {
Expand All @@ -28,11 +26,20 @@ function TimeAgo(Alpine) {
clearInterval(interval);
}

if (typeof date === 'string') {
date = parseISO(date);
}

render(date);

let intervalDuration = 30000;
if (modifiers.includes('seconds') && differenceInSeconds(new Date(), date) < 90) {
intervalDuration = 5000;
}

interval = setInterval(() => {
render(date);
}, 30000);
}, intervalDuration);
});
});

Expand Down

0 comments on commit a724845

Please sign in to comment.