From c085fcbe8f1cf9bf954d55d4cbb741db72583033 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Wed, 2 Oct 2024 17:51:38 +0900 Subject: [PATCH] add iterator and size getter --- src/index.ts | 10 ++++++++ test/integrity-metadata-set/iterator.js | 32 +++++++++++++++++++++++++ test/integrity-metadata-set/size.js | 19 +++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 test/integrity-metadata-set/iterator.js create mode 100644 test/integrity-metadata-set/size.js diff --git a/src/index.ts b/src/index.ts index b51beab..26070bb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -173,6 +173,16 @@ export class IntegrityMetadataSet { } } + *[Symbol.iterator](): Generator { + for (const integrityMetadata of this.#set) { + yield integrityMetadata; + } + } + + get size(): number { + return this.#set.length; + } + join(separator = " "): string { return this.#set.map(String).join(separator); } diff --git a/test/integrity-metadata-set/iterator.js b/test/integrity-metadata-set/iterator.js new file mode 100644 index 0000000..7f936ff --- /dev/null +++ b/test/integrity-metadata-set/iterator.js @@ -0,0 +1,32 @@ +import assert from "node:assert"; +import { test } from "node:test"; +import { IntegrityMetadata, IntegrityMetadataSet } from "../../dist/index.js"; + +test("correctly iterate over the set", function () { + const integrityMetadataSet = new IntegrityMetadataSet(` +sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM= +sha384-VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r +sha512-wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ== +`); + + assert.deepEqual( + [...integrityMetadataSet], + [ + new IntegrityMetadata( + "sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM=", + ), + new IntegrityMetadata( + "sha384-VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r", + ), + new IntegrityMetadata( + "sha512-wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ==", + ), + ], + ); +}); + +test("if the empty set, return the empty set", function () { + const integrityMetadataSet = new IntegrityMetadataSet(); + + assert.deepEqual([...integrityMetadataSet], []); +}); diff --git a/test/integrity-metadata-set/size.js b/test/integrity-metadata-set/size.js new file mode 100644 index 0000000..ec0ee52 --- /dev/null +++ b/test/integrity-metadata-set/size.js @@ -0,0 +1,19 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { IntegrityMetadataSet } from "../../dist/index.js"; + +test("return the correct size of the set", function () { + const integrityMetadataSet = new IntegrityMetadataSet(` +sha256-MV9b23bQeMQ7isAGTkoBZGErH853yGk0W/yUx1iU7dM= +sha384-VbxVaw0v4Pzlgrpf4Huq//A1ZTY4x6wNVJTCpkwL6hzFczHHwSpFzbyn9MNKCJ7r +sha512-wVJ82JPBJHc9gRkRlwyP5uhX1t9dySJr2KFgYUwM2WOk3eorlLt9NgIe+dhl1c6ilKgt1JoLsmn1H256V/eUIQ== +`); + + assert.strictEqual(integrityMetadataSet.size, 3); +}); + +test("if the empty set, return 0", function () { + const integrityMetadataSet = new IntegrityMetadataSet(); + + assert.strictEqual(integrityMetadataSet.size, 0); +});