iamkun / iamkun/dayjs

Conditional thresholds produce weird behaviour with Infinity value

Open
#2,380 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
With the discribe plugin, the relativeTime plugin and the updateLocale plugin, I'm parsing an ISO 8601 string.
If I want to add Infinity as limiter for thresholds inside the relativeTime plugin with a ternary operation, for each Time Units, it will produce weird behaviours.

To Reproduce
```JS
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import relativeTime from "dayjs/plugin/relativeTime";

// Setup
const myFormats = ["seconds"];
dayjs.extend(duration);
dayjs.extend(updateLocale);
dayjs.updateLocale("en", {
relativeTime: {
...dayjs.Ls.en.relativeTime,
s: "%d second",
ss: "%d seconds",
m: "%d minute",
h: "%d hour",
d: "%d day",
M: "%d month",
y: "%d year",
yy: "%d years"
}
});
const thresholds = [
{ l: "s", r: 1 },
{ l: "ss", r: myFormats.includes("seconds") ? Infinity : 59, d: "second" },
{ l: "m", r: 1 },
{ l: "mm", r: Infinity, d: "minute" }
];
dayjs.extend(relativeTime, { thresholds });
// "thresholds" contains the right value "Infinity" for the "ss" limiter

// Tests
// Test 1
const sixtySecondsString = "PT60S";
const durationValue = dayjs.duration(sixtySecondsString);
durationValue.humanize(); // Will display "1 minute"

// Test 2
const seventySecondsString = "PT70S";
const durationValueB = dayjs.duration(seventySecondsString);
durationValueB.humanize(); // Will display "1 minute"
```

**Expected behavior**
In my example, I'm expecting "Test A" to display "60 seconds" instead of "1 minute", and "Test B" to display "61 seconds" instead of "1 minute".

My goal is to be able to display any ISO 8601 with strict unit times (i.e. : "PT400H" should display "400 hours" not "17 days")

**Information**
- Day.js Version 1.11.8
- OS: Windows 10
- Browser : None, I'm using Unit Tests for this example, Jest version 25.1.0
- Time zone: +01.00 Paris

Contributor guide

Open the contributing guide

Research direction

Start by running the provided reproduction with the relativeTime, duration, and updateLocale plugins in the project's Jest setup. Inspect how relativeTime thresholds are selected and how duration.humanize converts units. Done means Infinity thresholds preserve the requested seconds and other strict units, with tests covering the reported 60- and 70-second cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.