Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(duplicate-entries): removed duplicate entries #345

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
Expand Down