Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Simplify mask data processing using Object.values and flat #6050

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/masks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ if (typeof window != "undefined") {
return { cn: [], tw: [], en: [] };
})
.then((masks) => {
const { cn = [], tw = [], en = [] } = masks;
return [...cn, ...tw, ...en].map((m) => {
BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m));
return Object.values(masks).flat().map((m) => {
BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m as BuiltinMask));
Comment on lines +33 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ› οΈ Refactor suggestion

Add error handling for malformed mask data.

The current implementation might fail silently with malformed data from the endpoint. Consider adding proper error handling and logging.

Consider this improvement:

-return Object.values(masks).flat().map((m) => {
-  BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m as BuiltinMask));
+// Validate masks object structure
+if (!masks || typeof masks !== 'object') {
+  console.error('[Masks] Invalid masks data structure');
+  return [];
+}
+
+const flattenedMasks = Object.values(masks).flat();
+if (flattenedMasks.length === 0) {
+  console.warn('[Masks] No masks found in the response');
+}
+
+return flattenedMasks.map((m) => {
+  try {
+    BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m as BuiltinMask));
+  } catch (error) {
+    console.error('[Masks] Failed to add mask:', error);
+  }
});
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return Object.values(masks).flat().map((m) => {
BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m as BuiltinMask));
// Validate masks object structure
if (!masks || typeof masks !== 'object') {
console.error('[Masks] Invalid masks data structure');
return [];
}
const flattenedMasks = Object.values(masks).flat();
if (flattenedMasks.length === 0) {
console.warn('[Masks] No masks found in the response');
}
return flattenedMasks.map((m) => {
try {
BUILTIN_MASKS.push(BUILTIN_MASK_STORE.add(m as BuiltinMask));
} catch (error) {
console.error('[Masks] Failed to add mask:', error);
}
});

});
});
}