From 63b687248bf3f6c70a8df489665b8c81d73e9e9f Mon Sep 17 00:00:00 2001 From: sahmad-merative <135624814+sahmad-merative@users.noreply.github.com> Date: Thu, 5 Oct 2023 22:20:38 +0530 Subject: [PATCH] fix(duplicate-entries): removed duplicate entries (#345) * fix(duplicate-entries): removed duplicate entries * fix(duplicate-entries) : lint fixes --- blocks/blog-home/blog-home.js | 14 ++++++++++---- scripts/scripts.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/blocks/blog-home/blog-home.js b/blocks/blog-home/blog-home.js index 9b1af429..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, + 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()), + (checkedItem) => checkedList.find((item) => item.value && checkedItem.trim() + && item.value.toLowerCase() === checkedItem.trim().toLowerCase()), ); if (found) { card.removeAttribute('aria-hidden'); @@ -499,8 +501,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 +549,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 37bc5f9e..130a2c36 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -758,6 +758,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;