diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eded70..f4db7a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.156.2 (2026-11-27) + +- **Fixed: `calendar.format_duration` no longer raises on a `step_name` that is not a string.** Such a value reads as absent and the duration is worded in the grain, the calendar profile's rule for an optional key of the wrong kind (a `step_name` of `5` raised `TypeError`). +- **Fixed: `calendar.format_duration` words a whole count exactly up to the safe integers.** The count was rounded to two decimals through `n * 100`, which is not exact past 2^53 / 100, so `9007199254740990` steps were worded as `9007199254740989 days`. A whole count is now shown as it is; a fractional one is rounded as before. The npm SDK's `formatDuration` gives the same answers. + ## 0.156.1 (2026-11-27) - **Fixed: the depth check before a parse took time growing with the square of a text's length.** Since 0.155.0 every reader of a file this package did not write (`read_folder`, `read_world_file`, the import doors) first counts the text's nesting, and that scan cut the strings out with a pattern that, on a string left open, retried from every quote to the end of the text: a 16 KB file of escaped quotes took about a second, and a 1 MB one would take about an hour. Valid JSON heavy with quotes read several times slower than before the check. The scan is now one pass over the brackets, quotes and backslashes, linear in the length, with the same answers: held to the old reading on 20,000 random texts, and a 40 KB text of that shape is timed (the old scan took 6.7 s on it). diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index e994588..a3effe5 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.156.1" +__version__ = "0.156.2" diff --git a/src/onlyworlds/calendar.py b/src/onlyworlds/calendar.py index aec3ddb..6fb19f9 100644 --- a/src/onlyworlds/calendar.py +++ b/src/onlyworlds/calendar.py @@ -1579,7 +1579,8 @@ def format_duration(cfg: Mapping[str, Any], steps: float) -> str: ``12 days`` in the calendar's word for a step.""" n = abs(_in(steps)) word = _prop(cfg, "step_name") - if _nullish(word): + # a step_name that is not a string reads as absent (the profile's rule for an optional key of the wrong kind) + if not isinstance(word, str): word = _prop(cfg, "grain") if _nullish(word): word = "step" @@ -1599,7 +1600,8 @@ def plural(w: str, v: float) -> str: years = _div(n, _to_number(year_len)) rounded = _round(years) if years >= 10 else _div(_round(years * 10), 10) return f"{_js_num_str(rounded)} {plural('year', rounded)}" - shown = _div(_round(n * 100), 100) + # a whole count is shown as it is: n * 100 is not exact past 2^53 / 100, and 9007199254740990 came back a day short + shown = n if n.is_integer() else _div(_round(n * 100), 100) return f"{_js_num_str(shown)} {plural(unit, shown)}" diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 46ee732..d6b77dc 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -392,6 +392,17 @@ def test_surface_helpers() -> None: assert format_duration({"version": 1, "grain": "day", "step_name": "sol"}, 14) == "14 sols" +def test_duration_wrong_kind_step_name_and_whole_counts() -> None: + """A step_name that is not a string reads as absent; a whole count is exact up to the safe integers.""" + day = {"version": 2, "grain": "day"} + assert format_duration({**day, "step_name": 5}, 3) == "3 days" + assert format_duration({**day, "step_name": None}, 1) == "1 day" + assert format_duration({**day, "step_name": ["sol"]}, 2) == "2 days" + assert format_duration(day, 9007199254740990) == "9007199254740990 days" + assert format_duration(day, 9007199254740991) == "9007199254740991 days" + assert format_duration(day, 0.333) == "0.33 days" + + def test_mid_labels_and_month_starts() -> None: """Not in the SDK's tests; the comparison against its built package covers them too.""" assert format_mid_label(KIRROWMOSS, 7603) == "14 Thistlewane 22"