Fix patch doc add duplicate content type

This commit is contained in:
p10y
2023-06-28 23:34:16 +08:00
parent e3ee455186
commit 4f06d760a3
2 changed files with 37 additions and 0 deletions

View File

@ -47,5 +47,31 @@ describe("content-types-manager", () => {
type: "element",
});
});
it("should not append duplicate content type", () => {
const element = {
type: "element",
name: "xml",
elements: [
{
type: "element",
name: "Types",
elements: [
{
type: "element",
name: "Default",
attributes: {
ContentType: "image/png",
Extension: "png",
},
},
],
},
],
};
appendContentType(element, "image/png", "png");
expect(element.elements.length).toBe(1);
});
});
});

View File

@ -4,6 +4,17 @@ import { getFirstLevelElements } from "./util";
export const appendContentType = (element: Element, contentType: string, extension: string): void => {
const relationshipElements = getFirstLevelElements(element, "Types");
const exist = relationshipElements.some(el =>
el.type === "element"
&& el.name === "Default"
&& el?.attributes?.ContentType === contentType
&& el?.attributes?.Extension === extension
);
if (exist) {
return;
}
// eslint-disable-next-line functional/immutable-data
relationshipElements.push({
attributes: {