@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.
26 lines (24 loc) • 1.04 kB
JavaScript
import { isoStringBody } from "../../internal/isoStringBody.js";
import { isValidUtc } from "../validate/isValidUtc.js";
// src/utc/chop/chopUtc.ts
/**
* Return the UTC datetime string with a trailing Z removed if present.
*
* - Removes the trailing "Z"; a lower-case "z" is not a GMT UTC string (`isValidUtc`), so it returns "".
* - Drops the RFC 9557 annotations `Temporal.Instant.from` ignores (`[foo=bar]`, `[Europe/Paris]`).
* - Returns "" for invalid input.
*
* @param value UTC datetime string (ISO 8601)
* @returns UTC datetime string without trailing Z or "" on invalid input
*
* @example chopUtc("2024-03-10T12:00:00Z") // "2024-03-10T12:00:00"
* @example chopUtc("2024-03-10T12:00:00") // "" (no Z: not a UTC instant)
* @example chopUtc("2024-03-10T12:00:00z") // "" (lower-case z)
* @example chopUtc("invalid") // ""
* @example chopUtc("2024-03-10T12:00:00Z[foo=bar]") // "2024-03-10T12:00:00"
*/
function chopUtc(value) {
if (!isValidUtc(value)) return "";
return isoStringBody(value).replace(/Z$/, "");
}
export { chopUtc };