Skip to content

Commit

Permalink
Update canvas.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 29, 2025
1 parent feb43a1 commit 0b209e2
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/scan/src/new-outlines/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ const TOTAL_FRAMES = 45;
const PRIMARY_COLOR = '115,97,230';
// const SECONDARY_COLOR = '128,128,128';

function sortEntry(prev: [number, string[]], next: [number, string[]]): number {
return next[0] - prev[0];
}

function getSortedEntries(
countByNames: Map<number, string[]>,
): [number, string[]][] {
const entries = [...countByNames.entries()];
return entries.sort(sortEntry);
}

function getLabelTextPart([count, names]: [number, string[]]): string {
let part = `${names.slice(0, MAX_PARTS_LENGTH).join(', ')} ×${count}`;
if (part.length > MAX_LABEL_LENGTH) {
Expand All @@ -31,7 +42,7 @@ export const getLabelText = (outlines: ActiveOutline[]): string => {
}

const countByNames = new Map<number, string[]>();
for (const [name, count] of nameByCount.entries()) {
for (const [name, count] of nameByCount) {
const names = countByNames.get(count);
if (names) {
names.push(name);
Expand All @@ -41,9 +52,7 @@ export const getLabelText = (outlines: ActiveOutline[]): string => {
}

// TODO(Alexis): Optimize
const partsEntries = Array.from(countByNames.entries()).sort(
([countA], [countB]) => countB - countA,
);
const partsEntries = getSortedEntries(countByNames);
let labelText = getLabelTextPart(partsEntries[0]);
for (let i = 1, len = partsEntries.length; i < len; i++) {
labelText += ', ' + getLabelTextPart(partsEntries[i]);
Expand Down

0 comments on commit 0b209e2

Please sign in to comment.