Skip to content

Commit 880cea0

Browse files
authored
Merge pull request #108 from ryoppippi/uniq
Refactor: replace Set with es-toolkit uniq for better code readability
2 parents 7c57f24 + 8370251 commit 880cea0

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

‎src/data-loader.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { homedir } from 'node:os';
44
import path, { join } from 'node:path';
55
import process from 'node:process';
66
import { unreachable } from '@core/errorutil';
7-
import { groupBy } from 'es-toolkit'; // TODO: after node20 is deprecated, switch to native Object.groupBy
7+
import { groupBy, uniq } from 'es-toolkit'; // TODO: after node20 is deprecated, switch to native Object.groupBy
88
import { sort } from 'fast-sort';
99
import { createFixture } from 'fs-fixture';
1010
import { isDirectorySync } from 'path-type';
@@ -349,7 +349,7 @@ function extractUniqueModels<T>(
349349
entries: T[],
350350
getModel: (entry: T) => string | undefined,
351351
): string[] {
352-
return [...new Set(entries.map(getModel).filter((m): m is string => m != null && m !== '<synthetic>'))];
352+
return uniq(entries.map(getModel).filter((m): m is string => m != null && m !== '<synthetic>'));
353353
}
354354

355355
/**
@@ -790,10 +790,10 @@ export async function loadSessionData(
790790
);
791791

792792
// Collect all unique versions
793-
const versionSet = new Set<string>();
793+
const versions: string[] = [];
794794
for (const entry of entries) {
795795
if (entry.data.version != null) {
796-
versionSet.add(entry.data.version);
796+
versions.push(entry.data.version);
797797
}
798798
}
799799

@@ -822,7 +822,7 @@ export async function loadSessionData(
822822
projectPath: latestEntry.projectPath,
823823
...totals,
824824
lastActivity: formatDate(latestEntry.timestamp),
825-
versions: Array.from(versionSet).sort(),
825+
versions: uniq(versions).sort(),
826826
modelsUsed,
827827
modelBreakdowns,
828828
};
@@ -865,12 +865,12 @@ export async function loadMonthlyUsageData(
865865
const modelBreakdowns = createModelBreakdowns(modelAggregates);
866866

867867
// Collect unique models
868-
const modelsSet = new Set<string>();
868+
const models: string[] = [];
869869
for (const data of dailyEntries) {
870870
for (const model of data.modelsUsed) {
871871
// Skip synthetic model
872872
if (model !== '<synthetic>') {
873-
modelsSet.add(model);
873+
models.push(model);
874874
}
875875
}
876876
}
@@ -896,7 +896,7 @@ export async function loadMonthlyUsageData(
896896
cacheCreationTokens: totalCacheCreationTokens,
897897
cacheReadTokens: totalCacheReadTokens,
898898
totalCost,
899-
modelsUsed: Array.from(modelsSet),
899+
modelsUsed: uniq(models),
900900
modelBreakdowns,
901901
};
902902

‎src/session-blocks.internal.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { uniq } from 'es-toolkit';
2+
13
/**
24
* Default session duration in hours (Claude's billing block duration)
35
*/
@@ -163,15 +165,15 @@ function createBlock(startTime: Date, entries: LoadedUsageEntry[], now: Date, se
163165
};
164166

165167
let costUSD = 0;
166-
const modelsSet = new Set<string>();
168+
const models: string[] = [];
167169

168170
for (const entry of entries) {
169171
tokenCounts.inputTokens += entry.usage.inputTokens;
170172
tokenCounts.outputTokens += entry.usage.outputTokens;
171173
tokenCounts.cacheCreationInputTokens += entry.usage.cacheCreationInputTokens;
172174
tokenCounts.cacheReadInputTokens += entry.usage.cacheReadInputTokens;
173175
costUSD += entry.costUSD ?? 0;
174-
modelsSet.add(entry.model);
176+
models.push(entry.model);
175177
}
176178

177179
return {
@@ -183,7 +185,7 @@ function createBlock(startTime: Date, entries: LoadedUsageEntry[], now: Date, se
183185
entries,
184186
tokenCounts,
185187
costUSD,
186-
models: Array.from(modelsSet),
188+
models: uniq(models),
187189
};
188190
}
189191

‎src/utils.internal.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import process from 'node:process';
22
import Table from 'cli-table3';
3+
import { uniq } from 'es-toolkit';
34
import pc from 'picocolors';
45
import stringWidth from 'string-width';
56

@@ -243,7 +244,7 @@ function formatModelName(modelName: string): string {
243244
*/
244245
export function formatModelsDisplay(models: string[]): string {
245246
// Format array of models for display
246-
const uniqueModels = [...new Set(models.map(formatModelName))];
247+
const uniqueModels = uniq(models.map(formatModelName));
247248
return uniqueModels.sort().join(', ');
248249
}
249250

0 commit comments

Comments
 (0)