From f8aff38a6bdb13070d2b37da0e59aa9d04639d95 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Wed, 18 Sep 2024 15:05:26 +0900 Subject: [PATCH] accepts an IntegrityMetadata like object as input --- src/index.ts | 9 +++++++-- test/integrity-metadata/constructor.js | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7a1fed4..9e7d8b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,12 +51,17 @@ export class IntegrityMetadata implements IntegrityMetadataLike { alg: PrioritizedHashAlgorithm; val: string; opt: string[]; - constructor(integrity: string | null | undefined) { + constructor(integrity: IntegrityMetadataLike | string | null | undefined) { + const integrityString = + typeof integrity === "object" && integrity !== null + ? IntegrityMetadata.stringify(integrity) + : String(integrity ?? "").trim(); + const { alg = "", val = "", opt, - } = IntegrityMetadataRegex.exec((integrity ?? "").trim())?.groups ?? {}; + } = IntegrityMetadataRegex.exec(integrityString)?.groups ?? {}; Object.assign(this, { alg, diff --git a/test/integrity-metadata/constructor.js b/test/integrity-metadata/constructor.js index f628ee4..a221514 100644 --- a/test/integrity-metadata/constructor.js +++ b/test/integrity-metadata/constructor.js @@ -50,7 +50,7 @@ test("accepts options", function () { }); }); -test.todo("accepts an IntegrityMetadata like object as input", function () { +test("accepts an IntegrityMetadata like object as input", function () { const integrityMetadata = new IntegrityMetadata({ alg: "sha256", val: "MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=",