add iterator and size getter

This commit is contained in:
Nebel 2024-10-02 17:51:38 +09:00
parent f7ac85b913
commit c085fcbe8f
Signed by: nebel
GPG key ID: 79807D08C6EF6460
3 changed files with 61 additions and 0 deletions

View file

@ -173,6 +173,16 @@ export class IntegrityMetadataSet {
}
}
*[Symbol.iterator](): Generator<IntegrityMetadata> {
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);
}

View file

@ -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], []);
});

View file

@ -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);
});