Guarantees

This commit is contained in:
Manav Rathi
2024-08-23 20:27:28 +05:30
parent 039055ccc0
commit 177d3dcccf
3 changed files with 29 additions and 24 deletions

View File

@@ -71,8 +71,8 @@ const isDateComponentsMatch = (
{ year, month, day, weekday }: SearchDateComponents,
date: Date,
) => {
// Components are guaranteed to have at least one component, so start by
// assuming true.
// Components are guaranteed to have at least one attribute present, so
// start by assuming true.
let match = true;
if (year) match = date.getFullYear() == year;

View File

@@ -30,34 +30,39 @@ export const parseDateComponents = (s: string): DateSearchResult[] => {
};
export const parseChrono = (s: string): DateSearchResult[] =>
chrono.parse(s).map((result) => {
const p = result.start;
const component = (s: Component) =>
p.isCertain(s) ? nullToUndefined(p.get(s)) : undefined;
chrono
.parse(s)
.map((result) => {
const p = result.start;
const component = (s: Component) =>
p.isCertain(s) ? nullToUndefined(p.get(s)) : undefined;
const year = component("year");
const month = component("month");
const day = component("day");
const weekday = component("weekday");
const components = { year, month, day, weekday };
const year = component("year");
const month = component("month");
const day = component("day");
const weekday = component("weekday");
const format: Intl.DateTimeFormatOptions = {};
if (year) format.year = "numeric";
if (month) format.month = "long";
if (day) format.day = "numeric";
if (weekday) format.weekday = "long";
if (!year && !month && !day && !weekday) return undefined;
const components = { year, month, day, weekday };
const formatter = new Intl.DateTimeFormat(undefined, format);
const formattedDate = formatter.format(p.date());
return { components, formattedDate };
});
const format: Intl.DateTimeFormatOptions = {};
if (year) format.year = "numeric";
if (month) format.month = "long";
if (day) format.day = "numeric";
if (weekday) format.weekday = "long";
const formatter = new Intl.DateTimeFormat(undefined, format);
const formattedDate = formatter.format(p.date());
return { components, formattedDate };
})
.filter((x) => x !== undefined);
/** Parse a string like "2024" into a date search result. */
const parseYearComponents = (s: string): DateSearchResult[] => {
// s is already trimmed
// s is already trimmed.
if (s.length == 4) {
const year = parseInt(s);
if (year > 0 && year <= 9999) {
if (year && year <= 9999) {
const components = { year };
return [{ components, formattedDate: s }];
}

View File

@@ -8,8 +8,8 @@ import type { EnteFile } from "../../types/file";
/**
* A parsed version of a potential natural language date time string.
*
* The components which were parsed will be set. The type doesn't enforce this,
* but at least one component will be present.
* All attributes which were parsed will be set. The type doesn't enforce this,
* but it is guaranteed that at least one attribute will be present.
*/
export interface SearchDateComponents {
/**