Skip to content

Commit 3d7a389

Browse files
Copilotbookernath
andcommitted
Fix ESLint violations in filterMetadata function
Co-authored-by: bookernath <[email protected]>
1 parent 357f4bf commit 3d7a389

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

core/lib/makeswift/makeswift-page-shim.tsx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,73 @@
1111
import { Page as MakeswiftPage } from '@makeswift/runtime/next';
1212
import { ComponentPropsWithoutRef } from 'react';
1313

14+
type MetadataValue = string | number | boolean | MetadataObject | MetadataValue[] | null | undefined;
15+
type MetadataObject = { [key: string]: MetadataValue };
16+
1417
/**
1518
* Filters metadata to only include meaningful values while preserving structure.
1619
* - Always disables favicon to prefer the store favicon from /favicon.ico
1720
* - Removes falsy values (empty strings, null, undefined) to use BigCommerce defaults
1821
* - Preserves nested objects and meaningful boolean values
22+
* @param metadata - The metadata object to filter
23+
* @returns The filtered metadata object with meaningful values only
1924
*/
20-
function filterMetadata(metadata: any): any {
25+
function filterMetadata(metadata: MetadataValue): MetadataObject | false {
2126
if (metadata === false) {
2227
return false;
2328
}
24-
25-
if (!metadata || typeof metadata !== 'object') {
29+
30+
if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) {
2631
return { favicon: false };
2732
}
2833

29-
const filtered: any = { favicon: false };
34+
const filtered: MetadataObject = { favicon: false };
35+
const entries = Object.entries(metadata);
3036

31-
for (const [key, value] of Object.entries(metadata)) {
37+
const processedEntries = entries.reduce<MetadataObject>((acc, [key, value]) => {
38+
// Always disable favicon to use store favicon
3239
if (key === 'favicon') {
33-
// Always disable favicon to use store favicon
34-
continue;
40+
return acc;
3541
}
3642

43+
// Skip null/undefined values to use BigCommerce defaults
3744
if (value === null || value === undefined) {
38-
// Skip null/undefined values to use BigCommerce defaults
39-
continue;
45+
return acc;
4046
}
4147

48+
// Skip empty strings to use BigCommerce defaults
4249
if (typeof value === 'string' && value.trim() === '') {
43-
// Skip empty strings to use BigCommerce defaults
44-
continue;
50+
return acc;
4551
}
4652

53+
// Preserve arrays as-is
4754
if (Array.isArray(value)) {
48-
// Preserve arrays as-is
49-
filtered[key] = value;
50-
continue;
55+
acc[key] = value;
56+
return acc;
5157
}
5258

59+
// Recursively filter nested objects (but not arrays)
5360
if (typeof value === 'object' && value !== null) {
54-
// Recursively filter nested objects (but not arrays)
5561
const filteredNested = filterMetadata(value);
62+
5663
if (filteredNested !== false && Object.keys(filteredNested).length > 1) {
5764
// Only include nested objects if they have more than just the favicon property
58-
filtered[key] = filteredNested;
65+
acc[key] = filteredNested;
5966
}
60-
continue;
67+
68+
return acc;
6169
}
6270

6371
// Include all other meaningful values (non-empty strings, numbers, booleans)
64-
filtered[key] = value;
65-
}
72+
acc[key] = value;
73+
return acc;
74+
}, {});
6675

67-
return filtered;
76+
return { ...filtered, ...processedEntries };
6877
}
6978

7079
export function MakeswiftPageShim(props: ComponentPropsWithoutRef<typeof MakeswiftPage>) {
7180
const metadata = filterMetadata(props.metadata);
72-
81+
7382
return <MakeswiftPage {...props} metadata={metadata} />;
7483
}

0 commit comments

Comments
 (0)