Refactor RevertModal to use shared nip71.getDTagValueFromTags

Replaced the duplicated `extractDTagValue` method in `RevertModal.js` with the standard `getDTagValueFromTags` utility from `js/nostr/nip71.js`.

Verified by adding a new unit test suite `tests/nostr/nip71-d-tag.test.mjs` ensuring the utility covers all edge cases.

This improves code health by reducing duplication and centralizing NIP-71 logic.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-26 17:36:43 +00:00
1 parent 9913e484ab
commit 30fbb8d81a
2 files changed
+63 -18

No files matched your search

+3 -18
View File
@@ -1,3 +1,4 @@
import { getDTagValueFromTags } from "../../nostr/nip71.js";
import { Nip71FormManager } from "./nip71FormManager.js";
import { createModalAccessibility } from "./modalAccessibility.js";
import {
@@ -354,7 +355,7 @@ export class RevertModal {
if (this.subtitle) {
const subtitleParts = [];
const dTagValue = this.extractDTagValue(video.tags);
const dTagValue = getDTagValueFromTags(video.tags);
if (dTagValue) {
subtitleParts.push(`d=${dTagValue}`);
}
@@ -604,7 +605,7 @@ export class RevertModal {
typeof version.rawMagnet === "string" ? version.rawMagnet.trim() : "";
const displayMagnet = magnet || rawMagnet;
const isPrivate = version.isPrivate === true;
const dTagValue = this.extractDTagValue(version.tags);
const dTagValue = getDTagValueFromTags(version.tags);
const nip71Metadata = this.buildNip71DisplayMetadata(version);
@@ -1488,21 +1489,5 @@ export class RevertModal {
references,
};
}
extractDTagValue(tags) {
if (!Array.isArray(tags)) {
return "";
}
for (const tag of tags) {
if (!Array.isArray(tag) || tag.length < 2) {
continue;
}
if (tag[0] === "d" && typeof tag[1] === "string") {
return tag[1];
}
}
return "";
}
}
+60
View File
@@ -0,0 +1,60 @@
import test from "node:test";
import assert from "node:assert/strict";
import { getDTagValueFromTags } from "../../js/nostr/nip71.js";
test("getDTagValueFromTags handles invalid inputs", (t) => {
assert.equal(getDTagValueFromTags(null), "");
assert.equal(getDTagValueFromTags(undefined), "");
assert.equal(getDTagValueFromTags("not an array"), "");
assert.equal(getDTagValueFromTags({}), "");
});
test("getDTagValueFromTags handles empty tags array", (t) => {
assert.equal(getDTagValueFromTags([]), "");
});
test("getDTagValueFromTags returns empty string if no d-tag present", (t) => {
const tags = [
["e", "eventId"],
["p", "pubkey"],
["t", "hashtag"],
];
assert.equal(getDTagValueFromTags(tags), "");
});
test("getDTagValueFromTags returns correct value for single d-tag", (t) => {
const tags = [
["e", "eventId"],
["d", "my-identifier"],
["p", "pubkey"],
];
assert.equal(getDTagValueFromTags(tags), "my-identifier");
});
test("getDTagValueFromTags returns first value for multiple d-tags", (t) => {
const tags = [
["d", "first-identifier"],
["d", "second-identifier"],
];
assert.equal(getDTagValueFromTags(tags), "first-identifier");
});
test("getDTagValueFromTags skips malformed d-tags", (t) => {
const tags = [
["d"], // too short
["d", null], // invalid value type
["d", 123], // invalid value type
"not an array tag",
["d", "valid-identifier"],
];
assert.equal(getDTagValueFromTags(tags), "valid-identifier");
});
test("getDTagValueFromTags skips empty string d-tag value", (t) => {
// nip71 implementation: if (typeof tag[1] === "string" && tag[1]) { ... }
const tags = [
["d", ""],
["d", "next-valid"],
];
assert.equal(getDTagValueFromTags(tags), "next-valid");
});