Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update date parsing to use shared library (#771)
Leendert de Borst7917270aee5d
diff --git a/apps/browser-extension/src/entrypoints/popup/components/CredentialDetails/AliasBlock.tsx b/apps/browser-extension/src/entrypoints/popup/components/CredentialDetails/AliasBlock.tsxindex 2b65c2365..9319e6f4a 100644--- a/apps/browser-extension/src/entrypoints/popup/components/CredentialDetails/AliasBlock.tsx+++ b/apps/browser-extension/src/entrypoints/popup/components/CredentialDetails/AliasBlock.tsx@@ -1,20 +1,20 @@import React from 'react';-import { Credential } from '../../../../utils/types/Credential';-import { FormInputCopyToClipboard } from '../../components/FormInputCopyToClipboard';+import { Credential } from '@/utils/types/Credential';+import { FormInputCopyToClipboard } from '@/entrypoints/popup/components/FormInputCopyToClipboard';+import { IdentityHelperUtils } from '@/utils/shared/identity-generator';type AliasBlockProps = {credential: Credential;- isValidDate: (date: string | null | undefined) => boolean;}/*** Render the alias block.*/-const AliasBlock: React.FC<AliasBlockProps> = ({ credential, isValidDate }) => {+const AliasBlock: React.FC<AliasBlockProps> = ({ credential }) => {const hasFirstName = Boolean(credential.Alias?.FirstName?.trim());const hasLastName = Boolean(credential.Alias?.LastName?.trim());const hasNickName = Boolean(credential.Alias?.NickName?.trim());- const hasBirthDate = isValidDate(credential.Alias?.BirthDate);+ const hasBirthDate = IdentityHelperUtils.isValidBirthDate(credential.Alias?.BirthDate);if (!hasFirstName && !hasLastName && !hasNickName && !hasBirthDate) {return null;@@ -48,7 +48,7 @@ const AliasBlock: React.FC<AliasBlockProps> = ({ credential, isValidDate }) => {<FormInputCopyToClipboardid="birthDate"label="Birth Date"- value={new Date(credential.Alias?.BirthDate).toISOString().split('T')[0]}+ value={IdentityHelperUtils.normalizeBirthDateForDisplay(credential.Alias?.BirthDate)}/>)}{hasNickName && (diff --git a/apps/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx b/apps/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsxindex 4d2d6ac6a..258fc0914 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/CredentialDetails.tsx@@ -65,17 +65,6 @@ const CredentialDetails: React.FC = () => {);};- /**- * Check if a date is valid.- */- const isValidDate = useCallback((date: string | null | undefined): boolean => {- if (!date || date === '0001-01-01 00:00:00') {- return false;- }- const dateObj = new Date(date);- return !isNaN(dateObj.getTime());- }, []);-useEffect(() => {if (isPopup()) {window.history.replaceState({}, '', `popup.html#/credentials`);@@ -116,10 +105,7 @@ const CredentialDetails: React.FC = () => {<NotesBlock notes={credential.Notes} /><TotpBlock credentialId={credential.Id} /><LoginCredentialsBlock credential={credential} />- <AliasBlock- credential={credential}- isValidDate={isValidDate}- />+ <AliasBlock credential={credential} /></div>);};diff --git a/apps/browser-extension/src/utils/formDetector/FormFiller.ts b/apps/browser-extension/src/utils/formDetector/FormFiller.tsindex 99dc8ea74..790f8b980 100644--- a/apps/browser-extension/src/utils/formDetector/FormFiller.ts+++ b/apps/browser-extension/src/utils/formDetector/FormFiller.ts@@ -118,7 +118,7 @@ export class FormFiller {*/private fillBirthdateFields(credential: Credential): void {// TODO: when birth date is made optional in datamodel, we can remove this mindate check here.- if (!credential.Alias.BirthDate || credential.Alias.BirthDate === '0001-01-01 00:00:00') {+ if (!credential.Alias.BirthDate || credential.Alias.BirthDate.startsWith('0001-01-01')) {return;}diff --git a/apps/browser-extension/src/utils/shared/identity-generator/index.js b/apps/browser-extension/src/utils/shared/identity-generator/index.jsindex 467cad8c2..e1c3fa169 100644--- a/apps/browser-extension/src/utils/shared/identity-generator/index.js+++ b/apps/browser-extension/src/utils/shared/identity-generator/index.js@@ -1673,7 +1673,7 @@ var IdentityHelperUtils = class {if (!birthDate || birthDate.startsWith("0001-01-01")) {return "";}- return birthDate.split("T")[0];+ return birthDate.split(/[T ]/)[0];}/*** Normalize a birth date for database.@@ -1682,15 +1682,17 @@ var IdentityHelperUtils = class {if (!input || input.trim() === "") {return "0001-01-01T00:00:00.000Z";}- const trimmed = input.trim();- const parsedDate = new Date(trimmed);- if (isNaN(parsedDate.getTime())) {- return "0001-01-01T00:00:00.000Z";+ const trimmed = input.trim().replace(" ", "T");+ const match = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})[T ]?(\d{2}):?(\d{2}):?(\d{2})?$/);+ if (match) {+ const [_, y, m, d, h = "00", mi = "00", s = "00"] = match;+ return `${y}-${m}-${d}T${h}:${mi}:${s}.000Z`;}- if (trimmed.includes("T")) {- return trimmed;+ const parsedDate = new Date(trimmed);+ if (!isNaN(parsedDate.getTime())) {+ return parsedDate.toISOString();}- return `${trimmed}T00:00:00.000Z`;+ return "0001-01-01T00:00:00.000Z";}/*** Check if a birth date is valid.diff --git a/apps/browser-extension/src/utils/shared/identity-generator/index.mjs b/apps/browser-extension/src/utils/shared/identity-generator/index.mjsindex 3cd7ec7be..2a6d2dd3b 100644--- a/apps/browser-extension/src/utils/shared/identity-generator/index.mjs+++ b/apps/browser-extension/src/utils/shared/identity-generator/index.mjs@@ -1642,7 +1642,7 @@ var IdentityHelperUtils = class {if (!birthDate || birthDate.startsWith("0001-01-01")) {return "";}- return birthDate.split("T")[0];+ return birthDate.split(/[T ]/)[0];}/*** Normalize a birth date for database.@@ -1651,15 +1651,17 @@ var IdentityHelperUtils = class {if (!input || input.trim() === "") {return "0001-01-01T00:00:00.000Z";}- const trimmed = input.trim();- const parsedDate = new Date(trimmed);- if (isNaN(parsedDate.getTime())) {- return "0001-01-01T00:00:00.000Z";+ const trimmed = input.trim().replace(" ", "T");+ const match = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})[T ]?(\d{2}):?(\d{2}):?(\d{2})?$/);+ if (match) {+ const [_, y, m, d, h = "00", mi = "00", s = "00"] = match;+ return `${y}-${m}-${d}T${h}:${mi}:${s}.000Z`;}- if (trimmed.includes("T")) {- return trimmed;+ const parsedDate = new Date(trimmed);+ if (!isNaN(parsedDate.getTime())) {+ return parsedDate.toISOString();}- return `${trimmed}T00:00:00.000Z`;+ return "0001-01-01T00:00:00.000Z";}/*** Check if a birth date is valid.diff --git a/apps/mobile-app/utils/shared/identity-generator/index.js b/apps/mobile-app/utils/shared/identity-generator/index.jsindex 467cad8c2..e1c3fa169 100644--- a/apps/mobile-app/utils/shared/identity-generator/index.js+++ b/apps/mobile-app/utils/shared/identity-generator/index.js@@ -1673,7 +1673,7 @@ var IdentityHelperUtils = class {if (!birthDate || birthDate.startsWith("0001-01-01")) {return "";}- return birthDate.split("T")[0];+ return birthDate.split(/[T ]/)[0];}/*** Normalize a birth date for database.@@ -1682,15 +1682,17 @@ var IdentityHelperUtils = class {if (!input || input.trim() === "") {return "0001-01-01T00:00:00.000Z";}- const trimmed = input.trim();- const parsedDate = new Date(trimmed);- if (isNaN(parsedDate.getTime())) {- return "0001-01-01T00:00:00.000Z";+ const trimmed = input.trim().replace(" ", "T");+ const match = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})[T ]?(\d{2}):?(\d{2}):?(\d{2})?$/);+ if (match) {+ const [_, y, m, d, h = "00", mi = "00", s = "00"] = match;+ return `${y}-${m}-${d}T${h}:${mi}:${s}.000Z`;}- if (trimmed.includes("T")) {- return trimmed;+ const parsedDate = new Date(trimmed);+ if (!isNaN(parsedDate.getTime())) {+ return parsedDate.toISOString();}- return `${trimmed}T00:00:00.000Z`;+ return "0001-01-01T00:00:00.000Z";}/*** Check if a birth date is valid.diff --git a/apps/mobile-app/utils/shared/identity-generator/index.mjs b/apps/mobile-app/utils/shared/identity-generator/index.mjsindex 3cd7ec7be..2a6d2dd3b 100644--- a/apps/mobile-app/utils/shared/identity-generator/index.mjs+++ b/apps/mobile-app/utils/shared/identity-generator/index.mjs@@ -1642,7 +1642,7 @@ var IdentityHelperUtils = class {if (!birthDate || birthDate.startsWith("0001-01-01")) {return "";}- return birthDate.split("T")[0];+ return birthDate.split(/[T ]/)[0];}/*** Normalize a birth date for database.@@ -1651,15 +1651,17 @@ var IdentityHelperUtils = class {if (!input || input.trim() === "") {return "0001-01-01T00:00:00.000Z";}- const trimmed = input.trim();- const parsedDate = new Date(trimmed);- if (isNaN(parsedDate.getTime())) {- return "0001-01-01T00:00:00.000Z";+ const trimmed = input.trim().replace(" ", "T");+ const match = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})[T ]?(\d{2}):?(\d{2}):?(\d{2})?$/);+ if (match) {+ const [_, y, m, d, h = "00", mi = "00", s = "00"] = match;+ return `${y}-${m}-${d}T${h}:${mi}:${s}.000Z`;}- if (trimmed.includes("T")) {- return trimmed;+ const parsedDate = new Date(trimmed);+ if (!isNaN(parsedDate.getTime())) {+ return parsedDate.toISOString();}- return `${trimmed}T00:00:00.000Z`;+ return "0001-01-01T00:00:00.000Z";}/*** Check if a birth date is valid.diff --git a/shared/identity-generator/src/__tests__/IdentityHelperUtils.test.ts b/shared/identity-generator/src/__tests__/IdentityHelperUtils.test.tsnew file mode 100644index 000000000..49535ea0d--- /dev/null+++ b/shared/identity-generator/src/__tests__/IdentityHelperUtils.test.ts@@ -0,0 +1,53 @@+import { describe, it, expect } from 'vitest';+import { IdentityHelperUtils } from '../utils/IdentityHelperUtils';++describe('IdentityHelperUtils', () => {+ describe('normalizeBirthDateForDisplay', () => {+ it('should return empty string for undefined input', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDisplay(undefined)).toBe('');+ });++ it('should return empty string for default date', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDisplay('0001-01-01')).toBe('');+ });++ it('should strip time from ISO format date', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDisplay('1976-05-18T08:24:15.000Z')).toBe('1976-05-18');+ });++ it('should handle date with space separator', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDisplay('1976-05-18 08:24:15')).toBe('1976-05-18');+ });++ it('should handle date without time', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDisplay('1976-05-18')).toBe('1976-05-18');+ });+ });++ describe('normalizeBirthDateForDb', () => {+ it('should return default date for undefined input', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDb(undefined)).toBe('0001-01-01T00:00:00.000Z');+ });++ it('should return default date for empty string', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDb('')).toBe('0001-01-01T00:00:00.000Z');+ });++ it('should return default date for invalid date', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDb('invalid-date')).toBe('0001-01-01T00:00:00.000Z');+ });++ it('should preserve ISO format date with time', () => {+ const input = '1976-05-18T08:24:15.000Z';+ expect(IdentityHelperUtils.normalizeBirthDateForDb(input)).toBe(input);+ });++ it('should add default time to date with space separator', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDb('1976-05-18 08:24:15')).toBe('1976-05-18T08:24:15.000Z');+ });++ it('should add default time to date without time', () => {+ expect(IdentityHelperUtils.normalizeBirthDateForDb('1976-05-18')).toBe('1976-05-18T00:00:00.000Z');+ });+ });+});\ No newline at end of filediff --git a/shared/identity-generator/src/utils/IdentityHelperUtils.ts b/shared/identity-generator/src/utils/IdentityHelperUtils.tsindex 989872114..d43d588b8 100644--- a/shared/identity-generator/src/utils/IdentityHelperUtils.ts+++ b/shared/identity-generator/src/utils/IdentityHelperUtils.ts@@ -10,7 +10,8 @@ export class IdentityHelperUtils {return '';}- return birthDate.split('T')[0]; // Strip time+ // Handle both space and T separators+ return birthDate.split(/[T ]/)[0]; // Strip time}/**@@ -21,21 +22,22 @@ export class IdentityHelperUtils {return '0001-01-01T00:00:00.000Z';}- const trimmed = input.trim();+ const trimmed = input.trim().replace(' ', 'T');- // Try to parse the date- const parsedDate = new Date(trimmed);- if (isNaN(parsedDate.getTime())) {- return '0001-01-01T00:00:00.000Z';+ // Check if the format is valid ISO-like string manually, to support pre-1970 dates+ const match = trimmed.match(/^(\d{4})-(\d{2})-(\d{2})[T ]?(\d{2}):?(\d{2}):?(\d{2})?$/);+ if (match) {+ const [_, y, m, d, h = '00', mi = '00', s = '00'] = match;+ return `${y}-${m}-${d}T${h}:${mi}:${s}.000Z`;}- // If input already includes time, return as is (assume it's valid ISO format)- if (trimmed.includes('T')) {- return trimmed;+ // Fall back to native Date parsing only if ISO conversion not needed+ const parsedDate = new Date(trimmed);+ if (!isNaN(parsedDate.getTime())) {+ return parsedDate.toISOString();}- // If only date was provided (e.g., "1983-08-12"), add default time- return `${trimmed}T00:00:00.000Z`;+ return '0001-01-01T00:00:00.000Z';}/**