From 87082885cc06a2cbc6b7c31895cc92112120c93b Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 17 Apr 2026 14:18:48 +0200 Subject: [PATCH] Refs #17 a logcal change to allow for testabillity of the new features separately --- src/contacts/vcard/addDefaultFields.ts | 13 ++++--------- src/main.ts | 6 ++++-- tests/vcard.spec.ts | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/contacts/vcard/addDefaultFields.ts b/src/contacts/vcard/addDefaultFields.ts index 24b5325..8984641 100644 --- a/src/contacts/vcard/addDefaultFields.ts +++ b/src/contacts/vcard/addDefaultFields.ts @@ -1,9 +1,10 @@ import { TFile } from "obsidian"; -import { updateFrontMatter } from "src/contacts"; import { getApp } from "src/context/sharedAppContext"; import { getSettings } from "src/context/sharedSettingsContext"; -export async function addDefaultFields(file: TFile) { +import { VCardForObsidianRecord } from "./shared/vcard"; + +export async function addDefaultFields(file: TFile):Promise { const defaultFieldKeys = getSettings().createFieldsKeys const { metadataCache } = getApp(); @@ -12,18 +13,12 @@ export async function addDefaultFields(file: TFile) { throw new Error('No frontmatter found.'); } const record: Record = { ...frontMatter }; - - let changed = false; - for (const key of defaultFieldKeys) { if (!(key in record)) { record[key] = ""; - changed = true; } } - if (!changed) return; - - await updateFrontMatter(file, record); + return record; } diff --git a/src/main.ts b/src/main.ts index 48909ba..3376587 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import myScrollTo from "src/util/myScrollTo"; import { ContactsPluginSettings } from './settings/settings.d'; import {vcard} from "./contacts/vcard"; +import {updateFrontMatter} from "./contacts"; export default class ContactsPlugin extends Plugin { settings: ContactsPluginSettings; @@ -48,13 +49,14 @@ export default class ContactsPlugin extends Plugin { this.addCommand({ id: "contacts-apply-default-fields", name: "Apply Default Fields to Current File", - callback: () => { + callback: async () => { const file = this.app.workspace.getActiveFile(); if (!file) { new Notice("No active file."); return; } - vcard.addDefaultFields(file) + const records = await vcard.addDefaultFields(file); + updateFrontMatter(file, records) } }); } diff --git a/tests/vcard.spec.ts b/tests/vcard.spec.ts index 15d74cb..82aaaf4 100644 --- a/tests/vcard.spec.ts +++ b/tests/vcard.spec.ts @@ -313,5 +313,19 @@ describe('vcard tostring', () => { describe('vcard addDefaultFields', () => { - // TODO: write some tests for this. + it('should add missing default fields while preserving existing values', async () => { + const result = await vcard.addDefaultFields({ basename: 'base.frontmatter' } as TFile); + + expect(result['N.GN']).toBe('Liam'); + expect(result[ 'URL[WORK]']).toBe('https://storycraft.ie/liam'); + expect(result['N.PREFIX']).toBe(''); + expect(result['N.MN']).toBe(''); + expect(result['N.SUFFIX']).toBe(''); + expect(result['TEL[HOME]']).toBe(''); + expect(result['TEL[WORK]']).toBe(''); + expect(result['EMAIL[HOME]']).toBe('liam.green@lucky.ie'); + expect(result['EMAIL[WORK]']).toBe('liam@storycraft.ie'); + expect(result['ROLE']).toBe(''); + expect(result['CATEGORIES']).toBe('Writing, Mythology'); + }); });