-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Display selected and prioritized category badges for easy select…
…ion in the catalog Fixes: #1041
- Loading branch information
1 parent
c235475
commit 062330e
Showing
5 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { sortTags } from './sort-tags'; | ||
import { ITile } from './Catalog.models'; | ||
|
||
describe('sortTags()', () => { | ||
it('should return empty', () => { | ||
const tiles: ITile[] = []; | ||
const filterTags: string[] = []; | ||
const { sortedTags, overflowIndex } = sortTags(tiles, filterTags); | ||
expect(sortedTags.length).toBe(0); | ||
expect(overflowIndex).toBe(0); | ||
}); | ||
|
||
it('should return empty if filter tag is not really used', () => { | ||
const tiles: ITile[] = []; | ||
const filterTags = ['eip']; | ||
const { sortedTags, overflowIndex } = sortTags(tiles, filterTags); | ||
expect(sortedTags.length).toBe(0); | ||
expect(overflowIndex).toBe(0); | ||
}); | ||
|
||
it('should sort tags', () => { | ||
const tiles: ITile[] = [ | ||
{ | ||
type: 'component', | ||
name: '1', | ||
title: '1', | ||
tags: ['transformation', 'aaa-comp'], | ||
}, | ||
{ | ||
type: 'processor', | ||
name: '2', | ||
title: '2', | ||
tags: ['transformation', 'eip', 'aaa-proc'], | ||
}, | ||
]; | ||
let filterTags: string[] = []; | ||
const { sortedTags, overflowIndex } = sortTags(tiles, filterTags); | ||
expect(sortedTags.length).toBe(4); | ||
expect(sortedTags[0]).toEqual('aaa-proc'); | ||
expect(overflowIndex).toBe(2); | ||
filterTags = ['eip']; | ||
const { sortedTags: t1, overflowIndex: i1 } = sortTags(tiles, filterTags); | ||
expect(t1[0]).toEqual('eip'); | ||
expect(i1).toBe(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ITile } from './Catalog.models'; | ||
|
||
/** Tags to keep from overflowing. */ | ||
const additionalPriorityTags = ['database', 'cloud']; | ||
|
||
/** The number of tags to start using priority and filter tags to calculate overflowing index. */ | ||
const overflowThreshold = 0; | ||
|
||
/** | ||
* Extract all tags from tiles and sort it alphabetically as well as to pull prioritized tags and | ||
* {@link filterTags} up. | ||
* As a result, the prioritized tags and the ones in the filter are always shown. If the added number of | ||
* prioritized tags and {@link filterTags} exceeds {@link overflowThreshold}, all other tags overflow | ||
* behind the "N more" label, Otherwise the {@link overflowThreshold} number of tags are shown and kept | ||
* from overflowing. | ||
* The prioritized tags are either (1) the tags assigned to processors other than "eip", or | ||
* (2) {@link additionalPriorityTags}. | ||
*/ | ||
export const sortTags = (allTiles: ITile[], filterTags: string[]): { sortedTags: string[]; overflowIndex: number } => { | ||
const priorityTags = [...additionalPriorityTags]; | ||
const sortedTags = allTiles | ||
.reduce((acc, tile) => { | ||
tile.tags.forEach((tag) => { | ||
!acc.includes(tag) && acc.push(tag); | ||
tile.type === 'processor' && tag !== 'eip' && !priorityTags.includes(tag) && priorityTags.push(tag); | ||
}); | ||
return acc; | ||
}, [] as string[]) | ||
.sort((a, b) => { | ||
if (filterTags.includes(a) && !filterTags.includes(b)) return -1; | ||
if (filterTags.includes(b) && !filterTags.includes(a)) return 1; | ||
if (priorityTags.includes(a) && !priorityTags.includes(b)) return -1; | ||
if (priorityTags.includes(b) && !priorityTags.includes(a)) return 1; | ||
|
||
const lowerA = a.toLowerCase(); | ||
const lowerB = b.toLowerCase(); | ||
if (lowerA < lowerB) return -1; | ||
if (lowerA > lowerB) return 1; | ||
return 0; | ||
}); | ||
const num = sortedTags.filter((tag) => filterTags.includes(tag) || priorityTags.includes(tag)).length; | ||
const overflowIndex = num > overflowThreshold ? num : overflowThreshold; | ||
return { sortedTags, overflowIndex }; | ||
}; |