From c589c612da77178961add772f74a67f87ed14d9f Mon Sep 17 00:00:00 2001 From: Saad Ahmad Date: Thu, 28 Sep 2023 14:33:04 +0530 Subject: [PATCH 1/2] fix(duplicate-entries): removed duplicate entries --- blocks/blog-home/blog-home.js | 12 ++++++++---- scripts/scripts.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/blocks/blog-home/blog-home.js b/blocks/blog-home/blog-home.js index 9b1af429..d357ea96 100644 --- a/blocks/blog-home/blog-home.js +++ b/blocks/blog-home/blog-home.js @@ -1,5 +1,5 @@ import { - getAllBlogs, createCard, getBlogCategoryPages, createTag, sortArrayOfObjects, + getAllBlogs, createCard, getBlogCategoryPages, createTag, sortArrayOfObjects, removeDuplicateEnteries, } from '../../scripts/scripts.js'; const NUM_CARDS_SHOWN_AT_A_TIME = 6; @@ -193,7 +193,7 @@ export function refreshCards(mode) { if (card.hasAttribute(attribute)) { const filterGroupValues = card.getAttribute(attribute).split(','); const found = filterGroupValues.some( - (checkedItem) => checkedList.find((item) => item.value === checkedItem.trim()), + (checkedItem) => checkedList.find((item) => item.value && checkedItem.trim() && item.value.toLowerCase() === checkedItem.trim().toLowerCase()), ); if (found) { card.removeAttribute('aria-hidden'); @@ -499,8 +499,8 @@ export default async function decorate(block) { // Make a call to get all blog details from the blog index const blogList = await getAllBlogs(category); const categoriesList = await getBlogCategoryPages(); - const topics = new Set(); - const audiences = new Set(); + let topics = new Set(); + let audiences = new Set(); if (blogList.length) { const blogContent = createTag('div', { class: 'blog-content' }); // Get default content in this section and add it to blog-content @@ -547,6 +547,10 @@ export default async function decorate(block) { blogCards.append(blogCard); }); + // remove duplicate enteries + topics = removeDuplicateEnteries(topics, 'set'); + audiences = removeDuplicateEnteries(audiences, 'set'); + // Full card should be clickable blogCards.querySelectorAll('.card-item').forEach((card) => { card.addEventListener('click', () => { diff --git a/scripts/scripts.js b/scripts/scripts.js index 6b359651..bd1a9b0d 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -757,6 +757,38 @@ export async function getPDFsDocuments() { return (result); } +/** + * Remove duplicate enteries from array or set + * @param {Array | Set} arr Array or Set object + * @param {string} type Identifier to check Array or Set. Value: set or array + * @returns {Array | Set} Result + */ +export function removeDuplicateEnteries(arr, type) { + const mapArr = new Map(); + let result = []; + + // Check if the array empty + if (!arr.length && type !== 'set') { + return result; + } + if (!arr.size && type === 'set') { + return new Set([]); + } + + arr.forEach((item) => { + const _key = item.toString().trim().toLowerCase(); + if (!mapArr.has(_key)) { + mapArr.set(_key, item); + } + }); + result = [...mapArr.values()]; + if (type === 'set') { + result = new Set(result); + } + + return result; +} + export function sortArrayOfObjects(arr, property, type) { let result = []; let sortedArray; From 791fe46ba46026c862386cfe721d7015b5675b7d Mon Sep 17 00:00:00 2001 From: Saad Ahmad Date: Thu, 28 Sep 2023 14:41:41 +0530 Subject: [PATCH 2/2] fix(duplicate-entries) : lint fixes --- blocks/blog-home/blog-home.js | 6 ++++-- scripts/scripts.js | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/blocks/blog-home/blog-home.js b/blocks/blog-home/blog-home.js index d357ea96..1992ba55 100644 --- a/blocks/blog-home/blog-home.js +++ b/blocks/blog-home/blog-home.js @@ -1,5 +1,6 @@ import { - getAllBlogs, createCard, getBlogCategoryPages, createTag, sortArrayOfObjects, removeDuplicateEnteries, + getAllBlogs, createCard, getBlogCategoryPages, + createTag, sortArrayOfObjects, removeDuplicateEnteries, } from '../../scripts/scripts.js'; const NUM_CARDS_SHOWN_AT_A_TIME = 6; @@ -193,7 +194,8 @@ export function refreshCards(mode) { if (card.hasAttribute(attribute)) { const filterGroupValues = card.getAttribute(attribute).split(','); const found = filterGroupValues.some( - (checkedItem) => checkedList.find((item) => item.value && checkedItem.trim() && item.value.toLowerCase() === checkedItem.trim().toLowerCase()), + (checkedItem) => checkedList.find((item) => item.value && checkedItem.trim() + && item.value.toLowerCase() === checkedItem.trim().toLowerCase()), ); if (found) { card.removeAttribute('aria-hidden'); diff --git a/scripts/scripts.js b/scripts/scripts.js index bd1a9b0d..f117e665 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -776,9 +776,9 @@ export function removeDuplicateEnteries(arr, type) { } arr.forEach((item) => { - const _key = item.toString().trim().toLowerCase(); - if (!mapArr.has(_key)) { - mapArr.set(_key, item); + const key = item.toString().trim().toLowerCase(); + if (!mapArr.has(key)) { + mapArr.set(key, item); } }); result = [...mapArr.values()];