UNPKG

@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 (25 loc) • 1.22 kB
// src/internal/normalizeDateTime.ts /** * Normalize formatted date/time strings for stable comparisons. * * - Removes Unicode format and bidi control characters (LRM/RLM/LRE/RLE/etc.) * - Removes zero-width space and BOM * - Normalizes various non-breaking and narrow spaces to plain space * - Collapses repeated whitespace to a single space and trims * - Normalizes common dash/minus characters to ASCII hyphen-minus * - Optionally lowercases the result (disabled by default), in * `opts.locale` (default `"en"`: a language, not one country) */ function normalizeDateTime(input, opts) { if (typeof input !== "string") return ""; const { lower = false, locale = "en" } = opts ?? {}; let s = typeof input.normalize === "function" ? input.normalize("NFC") : input; s = s.replace(/[\p{Cf}\u200B\uFEFF]/gu, ""); s = s.replace(/[\u00A0\u202F\u2009\u2007\u2002\u2003\u2004\u2005\u2006\u2008\u200A]/g, " "); s = s.replace(/(?<=\d)[\u2010\u2011\u2012\u2013\u2014\u2015\u2212](?=\d)/g, "-"); s = s.replace(/[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/g, " - "); s = s.replace(/\s+/g, " ").trim(); s = s.replace(/[\u2044]/g, "/"); return lower ? s.toLocaleLowerCase(locale) : s; } export { normalizeDateTime };