Skip to content

Commit

Permalink
[ACS-7430] Make name optional in SortableByCategoryItem interface (#9735
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MichalKinas authored May 27, 2024
1 parent f2cfd68 commit a155507
Showing 1 changed file with 54 additions and 56 deletions.
110 changes: 54 additions & 56 deletions lib/core/src/lib/common/services/sort-by-category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,86 +18,84 @@
import { Injectable } from '@angular/core';

export interface SortableByCategoryItem {
name: string;
category?: string;
name?: string;
category?: string;
}

export interface ItemsByCategory<T> {
category: string;
items: T[];
category: string;
items: T[];
}

@Injectable({
providedIn: 'root'
providedIn: 'root'
})
export class SortByCategoryMapperService<T extends SortableByCategoryItem = SortableByCategoryItem> {
private defaultCategories: string[] = [];

private defaultCategories: string[] = [];
mapItems(items: T[], defaultCategories: string[]): ItemsByCategory<T>[] {
this.defaultCategories = defaultCategories;

mapItems(items: T[], defaultCategories: string[]): ItemsByCategory<T>[] {
this.defaultCategories = defaultCategories;
const sortedItems = this.sortItems(items);
const itemsByCategory = this.mapItemsByCategory(sortedItems);
const itemsSortedByCategory = this.sortCategories(itemsByCategory);

const sortedItems = this.sortItems(items);
const itemsByCategory = this.mapItemsByCategory(sortedItems);
const itemsSortedByCategory = this.sortCategories(itemsByCategory);

return itemsSortedByCategory;
}
return itemsSortedByCategory;
}

private mapItemsByCategory(items: T[]): ItemsByCategory<T>[] {
const itemsByCategoryObject: { [category: string]: T[] } = {};
private mapItemsByCategory(items: T[]): ItemsByCategory<T>[] {
const itemsByCategoryObject: { [category: string]: T[] } = {};

items.forEach((item) => {
const category = this.mapItemDefaultCategory(item);
if (!itemsByCategoryObject[category]) {
itemsByCategoryObject[category] = [];
}
items.forEach((item) => {
const category = this.mapItemDefaultCategory(item);
if (!itemsByCategoryObject[category]) {
itemsByCategoryObject[category] = [];
}

itemsByCategoryObject[category].push(item);
});
itemsByCategoryObject[category].push(item);
});

const itemsByCategory: ItemsByCategory<T>[] = Object.keys(itemsByCategoryObject).map((key) => {
const category = key;
return { category, items: itemsByCategoryObject[category] };
});
const itemsByCategory: ItemsByCategory<T>[] = Object.keys(itemsByCategoryObject).map((key) => {
const category = key;
return { category, items: itemsByCategoryObject[category] };
});

return itemsByCategory;
}
return itemsByCategory;
}

private sortItems(items: T[]): T[] {
return items.sort((itemA, itemB) => itemA.name.localeCompare(itemB.name));
}
private sortItems(items: T[]): T[] {
return items.sort((itemA, itemB) => itemA.name.localeCompare(itemB.name));
}

private sortCategories(itemsByCategory: ItemsByCategory<T>[]): ItemsByCategory<T>[] {
return itemsByCategory.sort((itemA, itemB) => {
if (itemB.category === '' && itemA.category === '') {
return 0;
}
private sortCategories(itemsByCategory: ItemsByCategory<T>[]): ItemsByCategory<T>[] {
return itemsByCategory.sort((itemA, itemB) => {
if (itemB.category === '' && itemA.category === '') {
return 0;
}

if (itemA.category === '') {
return 1;
}
if (itemA.category === '') {
return 1;
}

if (itemB.category === '') {
return -1;
}
if (itemB.category === '') {
return -1;
}

return itemA.category.localeCompare(itemB.category);
return itemA.category.localeCompare(itemB.category);
});
}
);
}

private mapItemDefaultCategory(listItem: SortableByCategoryItem): string {
const itemCategory = listItem.category;
private mapItemDefaultCategory(listItem: SortableByCategoryItem): string {
const itemCategory = listItem.category;

if (!this.isDefaultCategory(itemCategory)) {
return (itemCategory ?? '');
}
if (!this.isDefaultCategory(itemCategory)) {
return itemCategory ?? '';
}

return '';
}
return '';
}

private isDefaultCategory(category?: string): boolean {
return category ? this.defaultCategories.includes(category) : false;
}
private isDefaultCategory(category?: string): boolean {
return category ? this.defaultCategories.includes(category) : false;
}
}

0 comments on commit a155507

Please sign in to comment.