Skip to content

Commit db4329f

Browse files
committed
feat: Add array traversal support to sumNestedValue function and update tests
1 parent 8b4a2a0 commit db4329f

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

__tests__/main.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const exampleResponseEnterprise = JSON.parse(sample);
2727
test('createJobSummaryUsage(enterpriseUsage)', async () => {
2828
const summary = await createJobSummaryUsage(exampleResponseEnterprise, 'enterprise');
2929
writeFileSync('./__tests__/mock/sample-output.md', summary.stringify());
30+
console.log('Summary:', summary.stringify());
3031
expect(summary).toBeDefined();
3132
});
3233

@@ -78,6 +79,29 @@ test('sumNestedValue with completely missing path', () => {
7879
expect(sumNestedValue(data, ['a', 'b'])).toBe(0); // Path doesn't exist at all
7980
});
8081

82+
// New test for array traversal
83+
test('sumNestedValue with array traversal', () => {
84+
const data = [
85+
{
86+
a: {
87+
items: [
88+
{ value: 5 },
89+
{ value: 10 }
90+
]
91+
}
92+
},
93+
{
94+
a: {
95+
items: [
96+
{ value: 15 },
97+
{ value: 20 }
98+
]
99+
}
100+
}
101+
];
102+
expect(sumNestedValue(data, ['a', 'items', 'value'])).toBe(50); // Should sum all values in the arrays
103+
});
104+
81105
test('sumNestedValue with exampleResponseEnterprise data', () => {
82106
// Test with real data paths
83107
const totalChatEngagedUsers = sumNestedValue(exampleResponseEnterprise, ['copilot_ide_chat', 'total_engaged_users']);

__tests__/mock/sample-output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1>Copilot Usage for enterprise<br>3/1/2025 - 3/27/2025</h1>
22
<h1>Usage Summary</h1>
3-
<ul><li>Total Code Suggestions: 243,150</li><li>Total Code Acceptances: 57,011</li><li>Acceptance Rate: 23.45%</li><li>Total Lines of Code Accepted: 75,177</li><li>Total Chat Interactions: 2,937</li><li>Total Chat Copy Events: 0</li><li>Total Chat Insertion Events: 0</li></ul>
3+
<ul><li>Total Code Suggestions: 243,150</li><li>Total Code Acceptances: 57,011</li><li>Acceptance Rate: 23.45%</li><li>Total Lines of Code Accepted: 75,177</li><li>Total Chat Interactions: 2,937</li><li>Total Chat Copy Events: 2,989</li><li>Total Chat Insertion Events: 2,989</li></ul>
44

55
```mermaid
66
---

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": false,
66
"scripts": {
77
"build": "tsc && ncc build src/index.ts -o dist --source-map --license LICENSE",
8-
"test": "vitest",
8+
"test": "vitest run",
99
"lint": "eslint src/**/*.ts"
1010
},
1111
"keywords": [

src/job-summary.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,36 @@ const dateFormat = (date: string, options: Intl.DateTimeFormatOptions = {
3939
// Enhanced data aggregation utilities
4040
export const sumNestedValue = <T extends object>(data: T[], path: string[]): number => {
4141
return data.reduce((sum, obj) => {
42-
let current: Record<string, unknown> = obj as Record<string, unknown>;
43-
for (const key of path) {
44-
console.log(`Checking key: ${key}, current value: ${current}`);
45-
if (!current?.[key]) return sum;
46-
current = current[key] as Record<string, unknown>;
47-
}
48-
return sum + (typeof current === 'number' ? current : 0);
42+
let result = 0;
43+
44+
// Helper function to recursively traverse the object/array
45+
const traverse = (current: any, pathIndex: number) => {
46+
// Return if we've reached an invalid path
47+
if (current === undefined || current === null) return;
48+
49+
if (pathIndex >= path.length) {
50+
// We've reached the end of the path, add the value if it's a number
51+
if (typeof current === 'number') {
52+
result += current;
53+
}
54+
return;
55+
}
56+
57+
const key = path[pathIndex];
58+
59+
if (Array.isArray(current)) {
60+
// If current is an array, traverse each element
61+
current.forEach(item => traverse(item, pathIndex));
62+
} else if (typeof current === 'object') {
63+
// If current has the key, traverse deeper
64+
if (key in current) {
65+
traverse(current[key], pathIndex + 1);
66+
}
67+
}
68+
};
69+
70+
traverse(obj, 0);
71+
return sum + result;
4972
}, 0);
5073
};
5174

0 commit comments

Comments
 (0)