@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.
35 lines (33 loc) • 1.31 kB
JavaScript
import { durationOf } from "../../internal/inputChecks.js";
// src/duration/calculate/getDurationSign.ts
/**
* Report whether an ISO 8601 duration is negative, zero-length, or positive.
*
* - Uses Temporal.Duration.from and reads .sign: -1, 0, or 1.
* - Zero is its own result, not folded into the positive case — "PT0S" is 0, and so is
* "-PT0S", since a negated zero is still zero.
* - Needs no `relativeTo`: a duration's sign is carried on its components, not derived from
* how long they measure, so calendar-unit durations answer fine.
* - Returns null when `value` is not a valid ISO 8601 duration string — distinct from the 0
* a valid zero-length duration returns.
*
* @param value ISO 8601 duration string
* @returns -1 for a negative duration, 0 for zero-length, 1 for positive, or null on invalid input
*
* @example getDurationSign("P1DT2H") // 1
* @example getDurationSign("-P1DT2H") // -1
* @example getDurationSign("PT0S") // 0
* @example getDurationSign("-PT0S") // 0
* @example getDurationSign("-P1Y") // -1 (no relativeTo needed)
* @example getDurationSign("not a duration") // null
*/
function getDurationSign(value) {
const duration = durationOf(value);
if (duration === null) return null;
try {
return duration.sign;
} catch {
return null;
}
}
export { getDurationSign };