Skip to content

Commit

Permalink
Deduplicate tags when using deepDataMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemulley committed Jul 4, 2021
1 parent b047fc7 commit d5d110c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ class TemplateData {
} else if (data.tags === null) {
data.tags = [];
}

// Deduplicate tags
data.tags = [...new Set(data.tags)];
}

return data;
Expand Down
20 changes: 20 additions & 0 deletions test/TemplateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,26 @@ test("Data Cascade Tag Merge (Deep merge)", async (t) => {
t.deepEqual(data.tags.sort(), ["tagA", "tagB", "tagC", "tagD"]);
});

test("Data Cascade Tag Merge (Deep Merge - Deduplication)", async (t) => {
let eleventyConfig = new TemplateConfig();
// Default changed in 1.0
// eleventyConfig.userConfig.setDataDeepMerge(true);
let dataObj = new TemplateData("./test/stubs/", eleventyConfig);
await dataObj.cacheData();

let tmpl = getNewTemplate(
"./test/stubs/data-cascade/template.njk",
"./test/stubs/",
"./dist",
dataObj,
null,
eleventyConfig
);

let data = await tmpl.getData();
t.deepEqual(data.tags.sort(), ["tagA", "tagB", "tagC", "tagD"]);
});

test("Data Cascade Tag Merge (Shallow merge)", async (t) => {
let eleventyConfig = new TemplateConfig();
// Default changed in 1.0
Expand Down
4 changes: 2 additions & 2 deletions test/stubs/data-cascade/template.11tydata.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
parent: {
child: 2,
datafile: true
datafile: true,
},
datafile: true,
tags: ["tagC", "tagD"]
tags: ["tagC", "tagD", "tagD"],
};

0 comments on commit d5d110c

Please sign in to comment.