From 464cab8380a15bb8647296c88c1a405a79e55738 Mon Sep 17 00:00:00 2001 From: fraxken Date: Sat, 14 Jun 2025 18:17:21 +0200 Subject: [PATCH] refactor(contact)!: Add new EnforcedContact type that allow either name or email to be highlighted --- .changeset/cold-kids-flash.md | 5 +++++ package-lock.json | 16 ++++++++-------- workspaces/contact/package.json | 9 +++++++-- workspaces/contact/src/ContactExtractor.class.ts | 10 +++++++--- workspaces/contact/src/UnlitContact.class.ts | 12 +++++++++--- workspaces/contact/src/utils/compareContact.ts | 4 ++-- .../test/types/ContactExtractor.test-d.ts | 16 ++++++++++++++++ workspaces/mama/package.json | 3 +-- 8 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 .changeset/cold-kids-flash.md create mode 100644 workspaces/contact/test/types/ContactExtractor.test-d.ts diff --git a/.changeset/cold-kids-flash.md b/.changeset/cold-kids-flash.md new file mode 100644 index 00000000..d2b918e8 --- /dev/null +++ b/.changeset/cold-kids-flash.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/contact": major +--- + +Add new EnforcedContact type that allow either name or email to be highlighted diff --git a/package-lock.json b/package-lock.json index 862b9875..1da76a10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11270,10 +11270,11 @@ }, "workspaces/contact": { "name": "@nodesecure/contact", - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "dependencies": { - "@nodesecure/npm-types": "^1.2.0" + "@nodesecure/npm-types": "^1.2.0", + "type-fest": "^4.41.0" }, "devDependencies": { "@faker-js/faker": "^9.7.0" @@ -11301,15 +11302,14 @@ }, "workspaces/mama": { "name": "@nodesecure/mama", - "version": "1.2.0", + "version": "1.3.0", "license": "MIT", "dependencies": { "@nodesecure/npm-types": "^1.2.0", "object-hash": "^3.0.0" }, "devDependencies": { - "@types/object-hash": "^3.0.6", - "tsd": "^0.32.0" + "@types/object-hash": "^3.0.6" } }, "workspaces/npm-types": { @@ -11365,16 +11365,16 @@ }, "workspaces/scanner": { "name": "@nodesecure/scanner", - "version": "6.5.0", + "version": "6.6.0", "license": "MIT", "dependencies": { "@fastify/deepmerge": "^3.1.0", "@nodesecure/conformance": "^1.0.0", - "@nodesecure/contact": "^1.0.1", + "@nodesecure/contact": "^1.0.2", "@nodesecure/flags": "^3.0.3", "@nodesecure/i18n": "^4.0.1", "@nodesecure/js-x-ray": "^8.1.0", - "@nodesecure/mama": "^1.2.0", + "@nodesecure/mama": "^1.3.0", "@nodesecure/npm-registry-sdk": "^3.0.0", "@nodesecure/npm-types": "^1.2.0", "@nodesecure/rc": "^4.1.0", diff --git a/workspaces/contact/package.json b/workspaces/contact/package.json index 3d4a40f0..f7819d8e 100644 --- a/workspaces/contact/package.json +++ b/workspaces/contact/package.json @@ -12,7 +12,8 @@ "build": "tsc -b", "prepublishOnly": "npm run build", "test-only": "tsx --test ./test/**/*.spec.ts", - "test": "c8 -r html npm run test-only" + "test": "c8 -r html npm run test-only", + "test:tsd": "npm run build && tsd" }, "files": [ "dist" @@ -36,6 +37,10 @@ "@faker-js/faker": "^9.7.0" }, "dependencies": { - "@nodesecure/npm-types": "^1.2.0" + "@nodesecure/npm-types": "^1.2.0", + "type-fest": "^4.41.0" + }, + "tsd": { + "directory": "test/types" } } diff --git a/workspaces/contact/src/ContactExtractor.class.ts b/workspaces/contact/src/ContactExtractor.class.ts index 3c29403c..7dc26b79 100644 --- a/workspaces/contact/src/ContactExtractor.class.ts +++ b/workspaces/contact/src/ContactExtractor.class.ts @@ -4,10 +4,14 @@ import type { Contact } from "@nodesecure/npm-types"; // Import Internal Dependencies import { UnlitContact, + type EnforcedContact, type IlluminatedContact } from "./UnlitContact.class.js"; -export type { IlluminatedContact }; +export type { + IlluminatedContact, + EnforcedContact +}; export interface ContactExtractorPackageMetadata { author?: Contact; @@ -15,11 +19,11 @@ export interface ContactExtractorPackageMetadata { } export interface ContactExtractorOptions { - highlight: Contact[]; + highlight: EnforcedContact[]; } export class ContactExtractor { - private highlighted: Contact[] = []; + private highlighted: EnforcedContact[] = []; constructor( options: ContactExtractorOptions diff --git a/workspaces/contact/src/UnlitContact.class.ts b/workspaces/contact/src/UnlitContact.class.ts index c84cb8e4..6d553e13 100644 --- a/workspaces/contact/src/UnlitContact.class.ts +++ b/workspaces/contact/src/UnlitContact.class.ts @@ -1,21 +1,27 @@ // Import Third-party Dependencies import type { Contact } from "@nodesecure/npm-types"; +import type { RequireAtLeastOne } from "type-fest"; // Import Internal Dependencies import * as utils from "./utils/index.js"; -export type IlluminatedContact = Contact & { +export type EnforcedContact = RequireAtLeastOne< + Contact, + "name" | "email" +>; + +export type IlluminatedContact = EnforcedContact & { dependencies: string[]; }; export class UnlitContact { - private illuminated: Contact; + private illuminated: EnforcedContact; private extendedName: RegExp | null = null; public dependencies = new Set(); constructor( - contact: Contact + contact: EnforcedContact ) { this.illuminated = structuredClone(contact); this.extendedName = typeof contact.name === "string" ? diff --git a/workspaces/contact/src/utils/compareContact.ts b/workspaces/contact/src/utils/compareContact.ts index 862d399a..77c08f61 100644 --- a/workspaces/contact/src/utils/compareContact.ts +++ b/workspaces/contact/src/utils/compareContact.ts @@ -18,8 +18,8 @@ export interface CompareOptions { * - add options for custom/advanced comparaison */ export function compareContact( - contactA: Contact, - contactB: Contact, + contactA: Partial, + contactB: Partial, options: CompareOptions = Object.create(null) ): boolean { const { compareName = true } = options; diff --git a/workspaces/contact/test/types/ContactExtractor.test-d.ts b/workspaces/contact/test/types/ContactExtractor.test-d.ts new file mode 100644 index 00000000..4a53a75d --- /dev/null +++ b/workspaces/contact/test/types/ContactExtractor.test-d.ts @@ -0,0 +1,16 @@ +// Import Third-party Dependencies +import { + expectType +} from "tsd"; + +// Import Internal Dependencies +import { + ContactExtractor +} from "../../dist/index.js"; + +expectType(new ContactExtractor({ + highlight: [ + { email: "foo" }, + { name: "bar" } + ] +})); diff --git a/workspaces/mama/package.json b/workspaces/mama/package.json index ba525c5f..e23feec7 100644 --- a/workspaces/mama/package.json +++ b/workspaces/mama/package.json @@ -36,8 +36,7 @@ "object-hash": "^3.0.0" }, "devDependencies": { - "@types/object-hash": "^3.0.6", - "tsd": "^0.32.0" + "@types/object-hash": "^3.0.6" }, "tsd": { "directory": "test/types"