Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): add humanize time format "past", and support future time for "relative" #26

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions shared/datetime/src/humanizeTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,43 @@ describe("humanizeTime", () => {
expect(humanizeTime(m)).toBe(m.format("LL ah:mm"));
});

it("show relative past time", function () {
const pastMoment = moment().add(-5, "days");
expect(humanizeTime(pastMoment, HumanizeTimeFormat.relative)).toBe(
"5 天前"
);
});

it("show relative future time", function () {
const futureMoment = moment().add(5, "days");
expect(humanizeTime(futureMoment, HumanizeTimeFormat.relative)).toBe(
"5 天后"
);
});

it("show past time", function () {
const pastMoment = moment().add(-5, "days");
expect(humanizeTime(pastMoment, HumanizeTimeFormat.past)).toBe("5 天前");
});
it("show past time even if it is future time", function () {
const pastMoment = moment().add(5, "days");
expect(humanizeTime(pastMoment, HumanizeTimeFormat.past)).toBe("几秒前");
});

it("show future time", function () {
const futureMoment = moment().add(5, "days");
expect(humanizeTime(futureMoment, HumanizeTimeFormat.future)).toBe(
"5 天后"
);
});

it("show future time even if it is past time", function () {
const futureMoment = moment().add(-5, "days");
expect(humanizeTime(futureMoment, HumanizeTimeFormat.future)).toBe(
"几秒后"
);
});

it("return null when given a nil", function () {
expect(humanizeTime(undefined)).toBe(null);
expect(humanizeTime(null)).toBe(null);
Expand Down
13 changes: 12 additions & 1 deletion shared/datetime/src/humanizeTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum HumanizeTimeFormat {
full = "full",
default = "default",
relative = "relative",
past = "past",
future = "future",
accurate = "accurate",
auto = "auto",
Expand Down Expand Up @@ -95,7 +96,17 @@ export const humanizeTime = (
case HumanizeTimeFormat.default:
text = m.format(fDefault);
break;
case HumanizeTimeFormat.relative:
case HumanizeTimeFormat.relative: {
const diff = +m - +now;
text =
diff <= 0
? moment.duration(diff).humanize(true)
: i18n.t(`${NS_LIBS_DATETIME}:${K.FUTURE_AFTER}`, {
time: moment.duration(diff).humanize(),
});
break;
}
case HumanizeTimeFormat.past:
text = moment.duration(Math.min(+m - +now, 0)).humanize(true);
break;
case HumanizeTimeFormat.future:
Expand Down
Loading