Skip to content

Commit

Permalink
fix(duplicate-entries): removed duplicate entries (#345)
Browse files Browse the repository at this point in the history
* fix(duplicate-entries): removed duplicate entries

* fix(duplicate-entries) : lint fixes
  • Loading branch information
sahmad-merative authored Oct 5, 2023
1 parent c1c2871 commit 63b6872
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
14 changes: 10 additions & 4 deletions blocks/blog-home/blog-home.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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', () => {
Expand Down
32 changes: 32 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 63b6872

Please sign in to comment.