@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
33 lines (31 loc) • 1.26 kB
JavaScript
import { durationOf } from "../../internal/inputChecks.js";
// src/duration/calculate/negateDuration.ts
/**
* Flip the sign of an ISO 8601 duration string.
*
* - Uses Temporal.Duration.from and .negated, then .toString() to re-emit.
* - Negating is a pure sign flip on every component, so it never needs `relativeTo` —
* calendar-unit durations round-trip through it fine, unlike `addDuration`/`totalDuration`.
* - A zero-length duration has no sign to flip and comes back as "PT0S", the canonical
* spelling Temporal emits for every zero duration (so "P0D" also renders "PT0S").
* - Returns "" when `value` is not a valid ISO 8601 duration string.
*
* @param value ISO 8601 duration string
* @returns ISO 8601 duration string with the opposite sign, or "" on invalid input
*
* @example negateDuration("P1DT2H") // "-P1DT2H"
* @example negateDuration("-P1DT2H") // "P1DT2H"
* @example negateDuration("P1Y2M") // "-P1Y2M" (no relativeTo needed)
* @example negateDuration("PT0S") // "PT0S"
* @example negateDuration("not a duration") // ""
*/
function negateDuration(value) {
const duration = durationOf(value);
if (duration === null) return "";
try {
return duration.negated().toString();
} catch {
return "";
}
}
export { negateDuration };