Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Handle years

Manav Rathi00865e3ee21f
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index 4a2309e6bd..00ee4d4b77 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -3,6 +3,11 @@ import type { Component } from "chrono-node";
import * as chrono from "chrono-node";
import type { SearchDateComponents } from "./types";
+interface DateSearchResult {
+ components: SearchDateComponents;
+ formattedDate: string;
+}
+
/**
* Try to parse an arbitrary search string into sets of date components.
*
@@ -17,9 +22,14 @@ import type { SearchDateComponents } from "./types";
* In addition, also return a formatted representation of the "best" guess at
* the date that was intended by the search string.
*/
-export const parseDateComponents = (
- s: string,
-): { components: SearchDateComponents; formattedDate: string }[] =>
+export const parseDateComponents = (s: string): DateSearchResult[] => {
+ const result = parseChrono(s);
+ if (result.length) return result;
+ // chrono does not parse years like "2024", so do it manually.
+ return parseYearComponents(s);
+};
+
+export const parseChrono = (s: string): DateSearchResult[] =>
chrono.parse(s).map((result) => {
const p = result.start;
const component = (s: Component) =>
@@ -41,3 +51,16 @@ export const parseDateComponents = (
const formattedDate = formatter.format(p.date());
return { components, formattedDate };
});
+
+/** Parse a string like "2024" into a date search result. */
+const parseYearComponents = (s: string): DateSearchResult[] => {
+ // s is already trimmed
+ if (s.length == 4) {
+ const year = parseInt(s);
+ if (year > 0 && year <= 9999) {
+ const components = { year };
+ return [{ components, formattedDate: s }];
+ }
+ }
+ return [];
+};