update getPrioritizedHashAlgorithm to return supported hash algorithm if unsupported one is provided

This commit is contained in:
Nebel 2024-09-18 14:21:27 +09:00
parent d7ab327e56
commit 9b64464d81
Signed by: nebel
GPG key ID: 79807D08C6EF6460
2 changed files with 15 additions and 15 deletions

View file

@ -22,8 +22,15 @@ export function getPrioritizedHashAlgorithm(
b: HashAlgorithm, b: HashAlgorithm,
): PrioritizedHashAlgorithm { ): PrioritizedHashAlgorithm {
if (a === b) return ""; if (a === b) return "";
if (!(a in supportedHashAlgorithms)) return "";
if (!(b in supportedHashAlgorithms)) return ""; if (!(a in supportedHashAlgorithms)) {
return b in supportedHashAlgorithms ? b : "";
}
if (!(b in supportedHashAlgorithms)) {
return a in supportedHashAlgorithms ? a : "";
}
return a < b ? b : a; return a < b ? b : a;
} }

View file

@ -14,21 +14,14 @@ test("if both hash algorithms are not supported, return the empty string", funct
assert.strictEqual(getPrioritizedHashAlgorithm("md5", "sha1"), ""); assert.strictEqual(getPrioritizedHashAlgorithm("md5", "sha1"), "");
}); });
test.todo( test("if one of the hash algorithms is unsupported, return the supported hash algorithm", function () {
"if one of the hash algorithms is unsupported, return the supported hash algorithm", assert.strictEqual(getPrioritizedHashAlgorithm("md5", "sha256"), "sha256");
function () { });
assert.strictEqual(getPrioritizedHashAlgorithm("md5", "sha256"), "sha256");
},
);
test("if both strings are empty, return the empty string", function () { test("if both strings are empty, return the empty string", function () {
assert.strictEqual(getPrioritizedHashAlgorithm("", ""), ""); assert.strictEqual(getPrioritizedHashAlgorithm("", ""), "");
}); });
test, test("if either is an empty string, it return the supported hash algorithm", function () {
todo( assert.strictEqual(getPrioritizedHashAlgorithm("sha256", ""), "sha256");
"if either is an empty string, it return the supported hash algorithm", });
function () {
assert.strictEqual(getPrioritizedHashAlgorithm("sha256", ""), "sha256");
},
);