(value: string)
| 1680 | * Returns the original string if it's not a recognized natural language pattern |
| 1681 | */ |
| 1682 | export function resolveNaturalLanguageDate(value: string): string { |
| 1683 | if (!value || typeof value !== "string") { |
| 1684 | return value; |
| 1685 | } |
| 1686 | |
| 1687 | const normalized = value.toLowerCase().trim().replace(/\s+/g, " "); |
| 1688 | |
| 1689 | // Check exact matches first |
| 1690 | if (normalized in NATURAL_LANGUAGE_DATE_PATTERNS) { |
| 1691 | try { |
| 1692 | return NATURAL_LANGUAGE_DATE_PATTERNS[ |
| 1693 | normalized as keyof typeof NATURAL_LANGUAGE_DATE_PATTERNS |
| 1694 | ](); |
| 1695 | } catch (error) { |
| 1696 | tasknotesLogger.error("Error resolving natural language date:", { |
| 1697 | category: "validation", |
| 1698 | operation: "resolving-natural-language-date", |
| 1699 | details: { value }, |
| 1700 | error: error, |
| 1701 | }); |
| 1702 | return value; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | // Handle relative patterns like "in 3 days", "2 weeks ago" |
| 1707 | try { |
| 1708 | // "in X days" pattern (handle flexible whitespace) |
| 1709 | let match = normalized.match(/^in\s+(\d+)\s+(days?)$/); |
| 1710 | if (match) { |
| 1711 | const days = parseInt(match[1], 10); |
| 1712 | return addDaysToDateString(getTodayString(), days); |
| 1713 | } |
| 1714 | |
| 1715 | // "X days ago" pattern (handle flexible whitespace) |
| 1716 | match = normalized.match(/^(\d+)\s+(days?)\s+ago$/); |
| 1717 | if (match) { |
| 1718 | const days = parseInt(match[1], 10); |
| 1719 | return addDaysToDateString(getTodayString(), -days); |
| 1720 | } |
| 1721 | |
| 1722 | // "in X weeks" pattern (handle flexible whitespace) |
| 1723 | match = normalized.match(/^in\s+(\d+)\s+(weeks?)$/); |
| 1724 | if (match) { |
| 1725 | const weeks = parseInt(match[1], 10); |
| 1726 | return addWeeksToDateString(getTodayString(), weeks); |
| 1727 | } |
| 1728 | |
| 1729 | // "X weeks ago" pattern (handle flexible whitespace) |
| 1730 | match = normalized.match(/^(\d+)\s+(weeks?)\s+ago$/); |
| 1731 | if (match) { |
| 1732 | const weeks = parseInt(match[1], 10); |
| 1733 | return addWeeksToDateString(getTodayString(), -weeks); |
| 1734 | } |
| 1735 | } catch (error) { |
| 1736 | tasknotesLogger.error("Error parsing relative natural language date:", { |
| 1737 | category: "validation", |
| 1738 | operation: "parsing-relative-natural-language-date", |
| 1739 | details: { value }, |
no test coverage detected